Displaying an ImageIcon

imageicon, java, paint, swing

Solution

You should use

ImageIcon image = new ImageIcon(this.getClass()
                .getResource("org/myproject/mypackage/peanut.jpg"));

Problem

I am trying to display an image on a `JPanel`. I'm using an `ImageIcon` for rendering the image, and the image is in the same directory as the class file. However, the image is not being displayed, and there are no errors occuring. Could anyone please assist in working out what's wrong with my code... ``` package ev; import java.awt.Graphics; import javax.swing.ImageIcon; import javax.swing.JPanel; public class Image extends JPanel { ImageIcon image = new ImageIcon("peanut.jpg"); int x = 10; int y = 10; public void paintComponent(Graphics g) { super.paintComponent(g); image.paintIcon(this, g, x, y); } } ```

Original source