Meaningless Drivel is fun!
The moose likes Swing / AWT / SWT and the fly likes Aspect ratio of Components Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Aspect ratio of Components" Watch "Aspect ratio of Components" New topic

Aspect ratio of Components

Andrew Cho
Ranch Hand

Joined: Jun 29, 2011
Posts: 50
Hey all,

Does anyone know of a good way to keep a certain aspect ratio of a Component, when the Container is resized?

Much appreciated,
Walter Gabrielsen Iii
Ranch Hand

Joined: Apr 09, 2011
Posts: 158
I would take the longest side of the original component and try to fit it inside the shortest side of the new, resized, area (you need to take a measurement of your original component's size first, before it is resized.).


(shortest side of new) / (longest side of old)
= multiplier for both of the component's dimensions.

Or, as applied, how much to scale a 32x32 icon to fill a 640x480 screen?
480 (the shortest size of the new space)
480/32 = 15
(so multiply each side by 15)
15*32 = 480, or 480x480 -- it fits as much as possible inside the shortest side of my new 640x480 space, while keeping the original size's aspect ratio.
Darryl Burke

Joined: May 03, 2008
Posts: 5115

I would start by separating the responsibilities. A code example explains this in less words than I would take to describe it.Play around with the parameters passed to the ProportionalPanel's constructor and the limit of the loop index and see the difference.

Note that for practical usage, the layout in which the proportional panel is housed plays a role.

luck, db
There are no new questions, but there may be new answers.
Andrew Cho
Ranch Hand

Joined: Jun 29, 2011
Posts: 50
Hey Darryl,
Thanks for your input, however, I'm not sure that I understand what this code does. It seems as if it just resizes the JLabels normally...
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

Yes... what you want is for the layout manager to resize the labels automatically as the container is resized, correct?

So therefore you want a layout manager which can preserve the aspect ratio of your labels.

None of the built-in layout managers do that, at least not that I know of. So your choices are two: search the web for layout managers which do what you want, or write a layout manager which does what you want.
Andrew Cho
Ranch Hand

Joined: Jun 29, 2011
Posts: 50
Thanks everyone,
I just went ahead and wrote my own.
Took a while since it was my first, but well worth it as it does exactly what I want.

Thanks again,
I agree. Here's the link:
subject: Aspect ratio of Components
It's not a secret anymore!