Vision Component Size restrictions

Is it possible to limit the width and height of a vision component?
I tried using the JComponent method setMaximumSize(), but that doesn't seem to accomplish anything.

I'm curious about the Y behind this X problem, but...

Possibly, you could implement your own component listener that sees the change in size value and immediately resets it to the maximum value?

1 Like

The Y is that I want to create a flowline component that is 3 pixels high at all times. If it was accidentally adjusted to less or greater than 3 pixels, it would automatically resize itself to that height.

This is what I have going at the moment, but it doesn't resize my component:

public class FlowlineComponent extends AbstractVisionComponent implements ComponentListener {
:
:
public FlowlineComponent() {
    setOpaque(false);
    setPreferredSize(new Dimension(100, 3));
    setFont(new Font("Ebrima", Font.BOLD, 17));
    ComponentListener l = this;
    addComponentListener(l);
}
:
:
@Override
public void componentResized(ComponentEvent e) {
    if (this.getSize().height > 3){
        Dimension maxSize = new Dimension(this.getSize().width,3);
        this.setSize(maxSize);
        this.setMaximumSize(maxSize);
    }
    System.out.println("This.size = " + this.getSize());
    System.out.println("This.maxSize = " + this.getMaximumSize());
}

You are going to suffer with Vision's various layout operations. Use a nested component and constrain that component's dimensions. Paint anything outside that nested component with some background.

If someone in the designer forgets to use an anchored layout, it'll look funny but still work.

1 Like

That's not quite what I want.
If the angle of the flowline is changed, I want the size of the component to change as well.
Let's say the flowline has a length of 100 and an angle of 0°. If the angle is changed to 45°, the component size should update to a width and height of 71.

That is not a Swing-friendly behavior. Or rather, that is not a Vision UI Layout-friendly behavior. Components should take fixed dimensions in the designer from the designer UI, expect to be scaled at runtime when relatvie layout is applied, and perform all other sizing behavior internally. That is, within the environment's imposed bounds.

Consider mocking up the behavior you want with the PaintableCanvas. Whatever you end up with for drawing operations to get exactly the image you want can then be converted to real java in your custom component.

1 Like

Understood.

If I put a rectangle on a vision window and change the angle on it, it does exactly what I want to do. It updates the x,y,width & height to keep the rectangle's size the same and keep its center in the same position.

And from what I recall from poking around, Vision has a bunch of special cases for its shape objects. Perhaps if you subclassed one of those, you could get away with similarly pathological behavior.

I'll keep poking around to see what I can find. Any further ideas would be appreciated.

Perhaps if you explained how you expect this component (and others you are making) to be used in your application(s), we could offer more focused advice.

I want to check out how to subclass Vision shape objects to see if that gets me any further.
If you can give me an example on how to do that, that would be helpful.

No different from subclassing AbstractVisionComponent as your code snippet shows. (I've not ever subclassed the shapes--I don't recall any of their class names.)

The component would never resize in real time, only during development.
So I would throw a flowline on the vision window, give it an angle and stretch it to the desired length. The width of the flowline is always to remain the same, no matter the component size.
I simply want to prevent the component from being larger than it needs to be, without the user having to be extra careful.

If the component has the wrong size:
image

it should automatically shrink:
image

Don't do that with your Vision component. Do that with a nested component, as I mentioned above. Vision, at runtime, is going to resize your component, outside your control. Control the size of whatever you draw inside your component.

I guess that's what I'm doing already. The width of the flowline is already locked to 3 pixels.

So I've been looking at using AbstractVisionShape instead of AbstractVisionComponent.
I've been struggling with initializing it however. It keeps giving me errors on the getShapeBounds part of it.
An example on how to use this would be great.

Hopefully someone who has done that will pipe up.

I guess no one has used AbstractVisionShape?