2

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);
Java Man
  • 1,854
  • 3
  • 21
  • 43
Javabeginner
  • 85
  • 2
  • 8
  • Get the dimension of your frame as below dim.height = image.getHeight(); //instantiate dim before use dim.width = image.getWidth(); then set the same size as preferred size for your image panel like panel1.setPreferedSize(dim); – Karibasappa G C Apr 25 '14 at 09:27

2 Answers2

4

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();
            }
        });
    }
}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Yes, if i enlarge the frame the image also enlarge and if i reduce so does the image. Something like the image is the background of the frame. Exactly like that thanks. Now is it possible that i can get that in a shorter way? because i didnt understand well this program. – Javabeginner Apr 25 '14 at 11:23
  • Sorry but I tried to make it as short as possible. You are only concerned about the `createBackgroundPanel()` method, where the panel is created. You can see my note from my answer for a little explanation. Or see [Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html) for more details on painting – Paul Samsotha Apr 25 '14 at 11:26
0

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

Stephan
  • 41,764
  • 65
  • 238
  • 329
  • Yes but when i use pack the frame indeed get the same size as the image but only at the openning. If i Enlarge or Reduce the size of the frame the image size stays the same. I was looking for something like the image grow if i enlarge the frame and reduce if i reduce the frame. Is that possible? – Javabeginner Apr 25 '14 at 10:22
  • @user3479440 Don't use a panel in your code. Just add the JLabel to the JFrame directly. – Stephan Apr 25 '14 at 10:32