Javabeginner
Javabeginner

Reputation: 85

How can I make an image the same size as the panel?

I created a simple frame program that includes a image. But the image don't have the same size as the frame. If i enlarge the frame the image size stays the same?

How can i make the image the same size as the frame?

Here is my current code:

    JPanel panel1 = new JPanel();
    ImageIcon image1 = new ImageIcon("C:\\Users\\Dark Mangetsu\\Downloads\\Ceng102_Lab10.1\\image\\flower.jpg");
    JLabel label1 = new JLabel(image1);
    panel1.add(label1);
    Color color1 = new Color (200, 0 ,100);
    panel1.setBorder(BorderFactory.createLineBorder(color1, 3));
    JFrame f = new JFrame("Frame");
    f.setLayout(new BorderLayout(5,5));
    f.add((panel1),BorderLayout.WEST);
    f.setSize(320,200);
    f.setVisible(true);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Upvotes: 2

Views: 4532

Answers (2)

Paul Samsotha
Paul Samsotha

Reputation: 209102

You can paint the image instead of using a label.

ImageIcon icon = new ImageIcon("C:\\Users\\Dark Mangetsu\\Downloads\\Ceng102_Lab10.1\\image\\flower.jpg");
Image image = icon.getImage();
JPanel panel1 = new JPanel() {
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, getWidth(), getHeight());
    }
};

Also not sure, but I think you may want to add the panel to the CENTER and not the west (if you want the image to be centered in the frame).

Also not, if you want a preferredSize for the panel, you will have to override the getPreferredSize() of the panel also.

JPanel panel1 = new JPanel() {
    ...
    @Override
    public Dimension getPreferredSize() {
        return new Dimension(320, 200);
    }
};

Then you can just pack() the frame, which is preferred, instead of setting the size

f.pack();
//f.setSize(320, 200);

enter image description hereenter image description hereenter image description here

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TestBackgroundResize {
    public TestBackgroundResize() {
        JFrame frame = new JFrame();
        frame.setContentPane(createBackgroundPanel());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private JPanel createBackgroundPanel() {
        return new JPanel() {
            BufferedImage image;
            {
                try {
                    image = ImageIO.read(getClass().getResource("/marioblobs/mario.png"));
                } catch (IOException ex) {
                    Logger.getLogger(TestBackgroundResize.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
            }
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(320, 200);
            }
        };
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                new TestBackgroundResize();
            }
        });
    }
}

Upvotes: 4

Stephan
Stephan

Reputation: 43053

I think the code is missing call to pack() method.

Here is a sample code:

public class ImageToPanel {
    public static void main(String[] args) {
        ImageToPanel itp = new ImageToPanel();
        itp.go();
    }

    private void go() {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() { createAndShowGUI(); }
        });
    }

    private void createAndShowGUI() {
        JFrame f =new JFrame();           
        f.setLayout(new BorderLayout());
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        f.setPreferredSize(new Dimension(640,400));

        JLabel label = new JLabel( new ImageIcon("wallpaper.jpg") );
        f.add(label, BorderLayout.CENTER);

        JButton button = new JButton("Quit");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
               System.exit(0);
            }
        });       
        f.add(button, BorderLayout.SOUTH);

        f.pack();
        f.setVisible(true);
    }
}

SCREENSHOT

In the screenshot below, a 1920x1200 wallpaper is constrained seamlessly in a 640x400 frame.

A 1920x1200 image constrained in a 640x400 JFrame

Tested on

  • Windows 7
  • Java 7

EDIT:

ImageIcon image1 = new ImageIcon("wallpaper.jpg");
JLabel label1 = new JLabel(image1);

Color color1 = new Color (200, 0 ,100);
label1.setBorder(BorderFactory.createLineBorder(color1, 3));

JFrame f = new JFrame("Frame");
f.setLayout(new BorderLayout(5,5));
f.add(label1,BorderLayout.WEST);
f.setSize(320,200);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Before resize

enter image description here

After resize

enter image description here

Upvotes: 0

Related Questions