Resize JPanel with resampling to preserve content

Give what I wrote JPanel

with many different components on it, is there a way to apply a generic "expanded" ability on a panel so that everything in it stretches proportionally when my window is resized?

That is, if I manually resize my window by 1/4 the size, everything in the panel should also shrink by 1/4 so that the new panel is just an extension of the first. Given that I have not designed individual components internally to do this (there are many), is there an easy way to make the panel behave this way?

UPDATE . To understand the solution you need more clearly, I will describe the contents of the panel:

The panel is a "game" of sorts, with a single zero layout and dozens of ImageIcons that fly across the screen at any time. ImageIcons are pre-loaded PNG files that are already at a constant size. Of course, I could manually resize each ImageIcon and move them relative to the window size, but that would require re-encoding many components.

There are no buttons or text to worry about, so what I'm really looking for is some sort of "post-processed" size where the panel just shrinks whatever is displayed in some chunk (think about resizing the image in Photoshop).

+3


source to share


4 answers


One option is of course to ditch swing and use some third party widget component library that draws itself using any Graphics

. Then you can draw widgets on the image and resize the image, or better yet, apply a transform to the graphic you pass to the library.

If you want to stick with swing, there is a method SwingUtilities.paintComponent

that can be used to draw Panel

on BufferedImage

, which can then be changed. (I used this myself to make some nice transitions between "views" in the game.)



The problem, of course, is that you somehow need to translate the user input appropriately. I don't have a solution for this right now, but the above might help you in some way.

+2


source


Using layout managers instead of absolute positioning widgets will give you this behavior. See the Oracle Tutorials: Using Layout Managers .



Do you really want the fonts to resize when resized? I don't know of a layout manager that can do this for you.

+1


source


You can try overriding the paintChildren()

panel method and resizing the graphic to achieve your desired visible size.

+1


source


You can try J (X) Layer, see http://www.pbjar.org/blogs/jxlayer/jxlayer40/

+1


source







All Articles