-1

I am trying to display a jpeg image to a panel in a JPanel. I have two panels panel, and panel1. I'm trying to display the image in one panel and then remove the panel to display another.

       panel.g2d.drawImage(myimage, 0, 0, null);

It gives me about 50 errors and I can't type them all because I'm writing this on my phone because I don't have Internet right now.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
TonyC
  • 35
  • 8
  • 3
    _because I don't have Internet right now_ How did you write this question here? – BackSlash Aug 22 '13 at 13:42
  • Posted this on phone, @BackSlash. – eatonphil Aug 22 '13 at 13:43
  • Please have a look at this answer for how to [load an image](http://stackoverflow.com/a/9866659/1057230), though if you doing it manually (without using any IDEs), then this [answer](http://stackoverflow.com/a/11372350/1057230) will surely help you :-) – nIcE cOw Aug 22 '13 at 14:09
  • 1
    -1 this exact same question has been asked at least a million times (at least feels like it) - do some research, evaluate the different options and come back if you run into trouble. – kleopatra Aug 22 '13 at 15:05

3 Answers3

4

Why not just use a JLabel instead?

Image image = ImageIO.read(getClass().getResource("/images/MyImage.png"));
JLabel label = new JLabel(new ImageIcon(image));
panel.add(label);
Reimeus
  • 158,255
  • 15
  • 216
  • 276
3

Use Buffered Image with JPanel. Here is an example

try{

        BufferedImage myPicture = ImageIO.read(new File("c://pic.jpg"));

        picLabel = new JLabel(new ImageIcon(myPicture));
        picLabel.setHorizontalAlignment(SwingConstants.CENTER);
                    frame.getContentPane().add(picLabel);

        }catch (IOException e){}
wilty
  • 175
  • 1
  • 1
  • 10
2

To display an image in a panel, override the paintComponent(Graphics) method and draw it there:

public class ImagePanel extends JPanel {

    private Image image;

    public void setImage (Image image) {
        this.image = image;
        revalidate();
        repaint();
    }


    @Override
    protected void paintComponent (Graphics g) {
        super.paintComponent(g);
        if (image != null)
            g.drawImage(image, 0, 0, this);
    }
}

you also should override the getPreferredSize() method to expose how large your image component should be (will be used by the layout manager of the parent container):

@Override
public Dimension getPreferredSize () {
    if (image == null) {
        return super.getPreferredSize();
    }
    Insets insets = getInsets();
    return new Dimension(image.getWidth(this) + insets.left + insets.right, image.getHeight(this) + insets.top + insets.bottom);
}

Edit: JLabel (as pointed out by the other answers) is fine for displaying simple images or icons, but when you need advanced features such as automatic up-/downscaling (with or without keeping proportions), tiling (x, y, both), it's usually better to create a specialized image panel class for that.

Peter Walser
  • 15,208
  • 4
  • 51
  • 78