CuriousTim
CuriousTim

Reputation: 183

Importing Images in a Java Applet

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

Answers (2)

Andrew Thompson
Andrew Thompson

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:

  1. Card backdrop
  2. Image or Shape of each suit symbol
  3. Image 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:

  1. Applet.getImage(URL)
  2. Toolkit.getImage(URL)
  3. Or for a synchronously loaded 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

Sri Harsha Chilakapati
Sri Harsha Chilakapati

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

Related Questions