Reputation: 183
I'm trying to create an applet that loads 52 cards, shuffles them(using random number generator), then displays the top 10 cards of the deck in two rows of five. I have the HTML file with the 52 images of the cards, but is there a way to load the images without having to load each individual image. And how do you load images from the web?
Upvotes: 0
Views: 632
Reputation: 168845
..is there a way to load the images without having to load each individual image
Put them in one huge image and slice it up at run-time. There is an example in this answer.
Search Google images for 'playing+card' and it is likely there will be 2 or more tile sets in the first page of hits.
But then, even faster is to load:
Image
or Shape
of each suit symbolImage
of or Font
for the letters and numbers. Then combine them at start-up to form each of the 52 cards.
It will apparently also require a common 'reverse side' for the cards.
how do you load images from the web?
By URL.
Some alternatives:
Applet.getImage(URL)
Toolkit.getImage(URL)
BufferedImage
use ImageIO.read(URL)
The first two methods are asynchronous & require an ImageObserver
.
Consider doing this as an application launched using Java Web Start. If you proceed with using an applet, go through the links in the applet info. page.
Upvotes: 2
Reputation: 11950
Try this.
Image[] cards = new Image[52]
private void loadImages(){
for (int i=0; i<cards.length; i++){
URL imgUrl = getClass().getResource("image"+(i+1));
cards[i] = getImage(imgUrl);
}
}
Hope this helps
Upvotes: 0