0

I got a JLayeredPane with a JLabel containing an Image in it. This looks like this:

JLayeredPane panel = new JLayeredPane();
JLabel label1 = new JLabel();
label1.setIcon(new ImageIcon(image));
label1.setBounds(0, 0, 1300, 900);
panel.add(label1, 0);
frame.add(panel);
frame.setSize(1320,900);
frame.setVisible(true);

And some other images in the JLayeredPane.

It all displays fine. But when I scale the frame in windows the images dont scale. (The images stay the same size and the window just gets bigger with greyspace.) Now my question: What can I do so the images are always filling the frame no matter how i scale it?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
eclipse
  • 2,831
  • 3
  • 26
  • 34
  • You probably need to override `paintComponent()` and call `Image.getScaledInstance()` inside of it. – Dan O Jun 12 '13 at 20:14
  • 1
    Why JLayeredPane? See my answer here: http://stackoverflow.com/questions/9227270/possible-to-layer-imageicons/9227310#9227310, also http://stackoverflow.com/questions/9137404/overlay-a-jbutton-over-jlabel-in-java-swing/9137561#9137561 – rtheunissen Jun 12 '13 at 20:25

2 Answers2

1

Not sure why you are using a JLayeredPane for this.

Maybe you can use Darryl's Stretch Icon class.

camickr
  • 321,443
  • 19
  • 166
  • 288
0

All you really need is JComponent with an Image, and override paintComponent like this:

public class FillImage extends JComponent {
    final Image image;

    public FillImage(final Image image) {
        this.image = image;
    }

    @Override
    public paintComponent(Graphics g) {
       // Additionally, you may set extra hints for better scaling

       // Draw image stretched to fill entire component
       g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
    }
}

Then, make sure the FillImage component is added to a container with a layout that makes it fill all the space available (like BorderLayout in CENTER).

Harald K
  • 26,314
  • 7
  • 65
  • 111