Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Jbutton Image

 
Derek Williams
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am creating a simple memory game with GUI in Eclipse. I have picture files to use for each card. I have 2 questions:
1. What directory do the files need to be stored in?
2. What does the code look like to pull them into a JButon object?
all I can find is this:

ImageIcon card1image = new ImageIcon("picture1.bmp");
JButton card1 = new JButton(card1image);
 
Vanessa Astle
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey!

1.I'm pretty sure you can store the pictures anywhere as long as you put the full directory in the code.

So, ImageIcon anImage = new ImageIcon("/home/ImageFiles/imageForCard.jpg");

But, if you want, you can store the image files in the same folder as your code is located and then you will only have to put the file's name.

So, ImageIcon anImage = new ImageIcon("imageForCard.jpg");

2. If you want the objects on your GUI to be buttons, and you want those buttons to display images, I think the code you have found would be the code to use.

I actually just finished a card (memory/matching) game GUI earlier in the year, and my professor recommended that I use the acm.graphics package. I found it really helpful. Especially because a card game is so graphical, and you will probably care more about the pictures and images than the other SWING compontents.

jtf.acm.org -They've got loads of tutorials and demo's. It's very easy-to-use!!
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the record I tend to prefer the URL version of methods rather than Strings or Files, since you can place the resource on the classpath, use the ClassLoader to get a URL to the resource and pass this in. Then it doesn't matter if it is on the file system, in a JAR, WAR or remote, the code will still work.

Dave
 
You didn't tell me he was so big. Unlike this tiny ad:
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!