Reputation: 3011
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?
Upvotes: 0
Views: 2112
Reputation: 27054
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
).
Upvotes: 0
Reputation: 324088
Not sure why you are using a JLayeredPane for this.
Maybe you can use Darryl's Stretch Icon class.
Upvotes: 1