2

I want the user to be able to resize the JFrame's width/height, while maintaining the same ratio between its width and height. In other words, I want to force the height to change with the width keeping the same frame shape.

public void componentResized(ComponentEvent arg0)
    {
        int setHeight = arg0.getComponent().getHeight();
        int setWidth = arg0.getComponent().getWidth();
        double newWidth = 0;
        double newHeight = 0;
        {
            if(setHeight != oldHeight)
            {
                heightChanged = true;
            }
            if(setWidth != oldWidth)
            {
                widthChanged = true;
            }
        }
        {
            if(widthChanged == true && heightChanged == false)
            {
                newWidth = setWidth;
                newHeight = setWidth*HEIGHT_RATIO;
            }
            else if(widthChanged == false && heightChanged == true)
            {
                newWidth = setHeight * WIDTH_RATIO;
                newHeight = setHeight;
            }
            else if(widthChanged == true && heightChanged == true)
            {
                newWidth = setWidth;
                newHeight = setWidth*HEIGHT_RATIO;
            }
        }

        int x1 = (int) newWidth;
        int y1 = (int) newHeight;
        System.out.println("W: " + x1 + " H: " + y1);
        Rectangle r = arg0.getComponent().getBounds();
        arg0.getComponent().setBounds(r.x, r.y, x1, y1);
        widthChanged = false;
        heightChanged = false;
        oldWidth = x1;
        oldHeight = y1;
    }
Andrew Kor
  • 154
  • 5
  • 16
  • This is unconventional, but in C++ it can be done a lot nicer. Instead of waiting for it to finish the resizing completely (which you will notice happening with Java), it can be done automatically. The only problem is converting the program to work with C++. You won't be able to achieve the same level of smoothness without it though. http://stackoverflow.com/questions/2448738/how-to-force-a-window-to-maintain-a-certain-width-height-ratio-when-resized – Obicere Nov 08 '13 at 18:25

2 Answers2

5

Have a look at J frame resizing in an aspect ratio

I think this is what you need.

@Override
public void componentResized(ComponentEvent arg0) {
    int W = 4;  
    int H = 3;  
    Rectangle b = arg0.getComponent().getBounds();
    arg0.getComponent().setBounds(b.x, b.y, b.width, b.width*H/W);

}
Community
  • 1
  • 1
Syd Kerckhove
  • 783
  • 5
  • 19
  • 2
    This works fine for what he asked, but in the case of a changing height, this will fail. You would need to get the new width, new height and compare that ratio to the wanted ratio, then determine which to change – Obicere Nov 08 '13 at 18:26
2

Try using a componentEvent to signal when the frame is resized. When the frame is resized keep track of which dimension (x or y) was changed. Use the getSize() method to get the frame dimensions and then get .getWidth() or .getHeight() to get the new size the frame was adjusted to. Use the new size of the changed dimension to calculate the desired size for the other dimension and set the size of the frame using .setSize(int width, int height).

This might have problems if you change both the width and height in the same resizing, if so you can always base the height off the width or vice versa

JustWannaFly
  • 301
  • 1
  • 11