0

so I have my JFrame set to fullsize

temp.setExtendedState(JFrame.MAXIMIZED_BOTH); 

I add 3 panels (borderlayout), in panel1 I have JLabel to display an image using:

img.getScaledInstance(600, 400,
        Image.SCALE_SMOOTH);

panel2 the same , panel3 just some buttons. (I will not post the whole code here), but this code runs great. Now what I want to do is: Jframe is fullsized, and 45% is taken by panel1, 45% by panel2, 10% panel3. If my picture to dsiplay is too big to fit 45% it gets scaled to fit this 45% of JFrame, if it's smaller than it's just displayed. Any ideas ?

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Pirate
  • 77
  • 1
  • 9
  • You mean you want the image to resize when the size of the container changes? – MadProgrammer Nov 26 '15 at 22:38
  • Perhaps [this](http://stackoverflow.com/questions/11959758/java-maintaining-aspect-ratio-of-jpanel-background-image/11959928#11959928) and [this](http://stackoverflow.com/questions/14115950/quality-of-image-after-resize-very-low-java/14116752#14116752) might help – MadProgrammer Nov 26 '15 at 22:39
  • Hi, I will check it out, but just to explain: First I want to divide JFrame which is in fullsize into: 45% , 45%, 10% so u see 1 picture, under it there is another picure and on the botton u have some buttons (10%). If the picture is too big to fit 45% it gets scaled if its OK then its just displayed – Pirate Nov 26 '15 at 22:44
  • Or i can say it like this: the size of container Jpanel1 is always 45% of Jframe, Jpanel 2 also. Jframe is always in fullsize. Now i wana make the picture to fit those 45%. It's ;ike this: I want to display 2 pictures at the same time as big as possible with some controls under it (buttons) and thats it – Pirate Nov 26 '15 at 22:54

1 Answers1

2

You can use Darryl's Shrink Icon. This is an Icon you can add to a JLabel and then add the JLabel to your JPanel.

The Shrink Icon works such that:

  1. if there is not enough space to display the image then it will shrink the image
  2. if there is enough space, then the image is centered in the space available.

We don't hardcode space allocation for components. We let each component take the space it needs. So for your main layout you would use the BorderLayout of the frame.

The you would use:

JPanel controls = new JPanel();
controls.add(...);
frame.add(controls, BorderLayout.PAGE_END);

Then you would create a second panel for your images:

JPanel images = new JPanel( new GridLayout(0, 1) );
images.add( new JLabel(...) );
images.add( new JLabel(...) );
frame.add(images, BorderLayout.CENTER)

Now the image panel will grow/shrink as the size of the frame changes and the labels will also grow/shrink depending on the space available.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • "The image will be reduced if necessary to fit within the bounds of the component;" Ok so this can work, but how to make the component Jpanel1 to take 45% of fullsized Jframe ??? – Pirate Nov 26 '15 at 22:58
  • well i think this is gona work, Ill check it out tomorrow and let You know, Thank You for help – Pirate Nov 26 '15 at 23:17