Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Swing ImageIcon, where to put images?

I'm following this tutorial for java swing games: http://zetcode.com/tutorials/javagamestutorial/movingsprites/

At this point:

ImageIcon ii = new ImageIcon(this.getClass().getResource());
image = ii.getImage();

I just don't know what kind of path I have to write and where should I save my images (which directory).

Would you help me please? Would you give an example?

like image 1000
Dren Skywalker Avatar asked Jul 28 '13 19:07

Dren Skywalker


People also ask

How do I add an image to a label in Swing?

You have to supply to the JLabel an Icon implementation (i.e ImageIcon ). You can do it trough the setIcon method, as in your question, or through the JLabel constructor: Image image=GenerateImage. toImage(true); //this generates an image file ImageIcon icon = new ImageIcon(image); JLabel thumb = new JLabel(); thumb.

How do you add an image to an icon in Java?

To add icon to a button, use the Icon class, which will allow you to add an image to the button. Icon icon = new ImageIcon("E:\editicon. PNG"); JButton button7 = new JButton(icon);

What is the difference between image and ImageIcon in Java?

Image is an abstract superclass of all classes that represent graphical images. ImageIcon is an implementation of Icon interface that uses Image as its source.


1 Answers

In your src folder, create a folder called "images" or "files" then put the image in there.

Then use this:

ImageIcon(this.getClass().getResource("/images/filename.png"));

If that doesn't work, try this:

ImageIcon(this.getClass().getResource("images/filename.png"));
like image 164
Steven Avatar answered Sep 22 '22 07:09

Steven