Question

I have made a GUI using Swing and layeredPanes, specifying dimensions and (x,y) co-ordinates of about 60 items. The JFrame dimensions I set are (1080, 720) but I need now to rescale the whole lot to fit (800, 600).

Is there a quick way of resizing the whole screen so all images and text retain their position and relative size without going through every component and re-specifying its setBounds(x, y, width, height)?

Was it helpful?

Solution

You could add a ComponentListener to your JLayeredPane, and then in the

void componentResized(ComponentEvent e)

you could rescale everything based on the current height and width to maintain a specific ratio. You could just do some math some simple math to get the difference ratio between say, 720 and your current width, then adjust the width of each of your components to their actual width times this ratio.

Then do the same for height. This way everything would maintain aspect ratio.

I would also store each component in a List to access them more easily.

EDIT: I just re-read your question:

without going through every component and re-specifying

I forgot about this while answering, and I don't think there is a simple way of doing it without going through all of them. But there are simple ways of looping through them tho!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top