PDA

View Full Version : ImageIcon, where's my icon?


webguy08
02-13-2010, 04:13 AM
Hi all,

I am trying to create a GUI using javax.swing. I wish to create JButtons which are represented by images of cards, however I can't seem to get it to work.

Here is the relevant code:

for (int i = 0; i < 52; i++) { // 52 cards
ImageIcon icon = new ImageIcon(deck.getCard(i).getImagePath()); // it passes a path, such as "image/Spades1.png"
cardPanel.add(new JButton(icon)); // cardPanel is a JPanel with a GridLayout
}


The images are held in a folder in the 'src' folder under the name 'images'.

I have tried changing the file extensions to .jpeg to see if it is a file format problem, but still no luck. I am so confused as to what is causing the buttons to not be images :(.

Any help is appreciated.

cs_student
02-13-2010, 08:04 PM
A) Make sure that the JButton is actually getting rendered to the frame. Maybe try making it a regular button just to see if it pops up.

B) Remember that the path is relative to the directory where the program is being run. So if the program is being run in ~/cardGame/bin and the images are in ~/cardGame/src/images, then you need to have a path like "../src/images/Spades1.png".

Also, remember that changing the file extension does not do anything to the file itself. It still remains in the same binary format. In order to change the file from a png to a jpeg you need to use something like gimp to change it for you.


cs_student

webguy08
02-14-2010, 01:30 AM
A) Make sure that the JButton is actually getting rendered to the frame. Maybe try making it a regular button just to see if it pops up.

B) Remember that the path is relative to the directory where the program is being run. So if the program is being run in ~/cardGame/bin and the images are in ~/cardGame/src/images, then you need to have a path like "../src/images/Spades1.png".

Also, remember that changing the file extension does not do anything to the file itself. It still remains in the same binary format. In order to change the file from a png to a jpeg you need to use something like gimp to change it for you.


cs_student

A) I already tried that. Kinda goes without saying :D. The JButtons show but the icons don't.

B) The program is being run from src/PACKAGE_NAME/Main and my images are in src/images so I would assume "images/IMG.png" would work :confused:. I've tried the path "../images/IMG.png" but that hasn't worked either. I also tried moving the image into the same package as the program and used the path "IMG.png", but that didn't work either.

Lol I'm not new to computing. I reformatted the images to jpeg and tried it, I didn't just change their extensions.

Thanks for trying though, but I still haven't been able to solve the problem.

cs_student
02-14-2010, 08:31 PM
Have you also determined for sure what directory your in when you run the program. (generally IDE's aren't set up to run it in the src director).

Maybe try creating the absolute path.

Use something like

File dir = File(".");
String path = dir.getCanonicalPath() + <insert relative path here>;



cs_student

webguy08
02-14-2010, 11:56 PM
Thanks that worked!