1

I'm trying to draw a .png on a JPanel. I imported it using an ImageIcon constructor, and drew it in my custom panel's paintComponent.

My sscce:

package mypackage;

import java.awt.Graphics;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MyPanel extends JPanel {

    static JFrame frame;
    static MyPanel panel;
    static ImageIcon icon;

    public static void main(String[] args) {
        icon = new ImageIcon(MyPanel.class.getResource("MyImage.png"));
        frame = new JFrame();
        panel = new MyPanel();
        frame.setSize(500, 500);
        frame.add(panel);
        frame.setVisible(true);
        frame.repaint();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        icon.paintIcon(panel, g, 100, 100);
    }
}

I expected the image, which is just a couple shapes on a white background, to display at (100, 100) on the panel. Instead, a blank screen:

Blank panel where I expected my image to be drawn

The fact that no error occurs means the program is finding the file properly.

The image is in my Eclipse project in the same package as the class:

Project setup

Why is this happening? How do I fix it?

Community
  • 1
  • 1
snickers10m
  • 1,709
  • 12
  • 28
  • 1
    Don't create the ImageIcon in your paintComponent method. Also try just drawing a circle or something in your panel. – matt Oct 27 '15 at 14:38
  • @matt Moving `icon = new ImageIcon(MyPanel.class.getResource("MyImage.png"));` to `main` and adding `static ImageIcon icon` above `main` does not fix the problem. – snickers10m Oct 27 '15 at 14:40
  • @matt Additionally, adding `((Graphics2D) g).draw(new Ellipse2D.Double(100, 100, 100, 100));` to `paintComponent` successfully draws a circle. – snickers10m Oct 27 '15 at 14:41
  • It seems as though your image icon has not been loaded properly. – matt Oct 27 '15 at 14:41
  • For example, you have the png file in your source. It needs to be in your classpath. – matt Oct 27 '15 at 14:43
  • @Reimeus Apologies. That's not what my question was about, and it has absolutely no effect, but I'll change my code to adhere to a better approach. – snickers10m Oct 27 '15 at 14:44
  • @matt Is there a way I could find out what is in my icon? There aren't any errors occuring and the URL is valid, so I do really think the image is loading. – snickers10m Oct 27 '15 at 14:48
  • The URL being valid doesn't mean much. You can get the height and width and make sure that is correct. You can create a BufferedImage img = ImageIO.read(MyPanel.class.getResourceAsStream("MyImage.png")); – matt Oct 27 '15 at 14:52
  • @matt The width and height are both -1, so you're right. The image isn't loading. If you'd like to post that classpath fact as an answer as well as your workaround with BufferedImage I'd gladly accept it. – snickers10m Oct 27 '15 at 14:55

2 Answers2

3

As the code looks correct, I suggest that the resource is not being loaded correctly.

Place the png file in your classpath. Eg. I would have a directory:

~/ProjectRoot/resources/mypackage/

Then include resources in the classpath. In eclipse you can setup the classpath via

Project -> Properties -> Java Build Path -> Add Class Folder

BufferedImage img = 
    ImageIO.read(MyPanel.class.getResourceAsStream("MyImage.png"));

That throws an exception if the image isn't found. You can use it to make an ImageIcon.

Community
  • 1
  • 1
matt
  • 10,892
  • 3
  • 22
  • 34
2

When you use ImageIcon to read an image from a file, you get no indication whether or not the read was successful.

Here's your GUI where I read an image using ImageIO:

Image on GUI

And here's the image, in the same directory as the Java source:

enter image description here

Here's your code, where I used an ImageIO read to read an Image, and draw the image in the paintComponent method.

package com.ggl.testing;

import java.awt.Graphics;
import java.awt.Image;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class MyPanel extends JPanel {

    private static final long serialVersionUID = -9008812738915944216L;

    private static JFrame frame;
    private static MyPanel panel;
    private static Image image;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                image = getImage();
                frame = new JFrame();
                panel = new MyPanel();
                frame.setSize(500, 500);
                frame.add(panel);
                frame.setVisible(true);
            }
        });
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, 100, 100, MyPanel.this);
    }

    private static Image getImage() {
        Image image = null;
        try {
            image = ImageIO.read(MyPanel.class.getResource("maze.jpg"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return image;
    }
}
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111