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?
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.
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);
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.
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"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With