0

I have lately been wanting to construct a solitaire or black jack game using java. When I started to code I ran into a problem about efficiently importing all the image filles for the cards. I immediately looked it up but I did not get any answers. Wy question is why cant you name images like this:

for (int num = 0; num1 < 40; num++ ){
    Image names[num] = getImage ( getDocumentBase (), "c" + (num +1) + ".gif" );
}

If you find any other problems please let me know. Thank you in advance. this is the full code:

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Color;

public class CardsJavaProgram extends Applet {
public void init(){
    String names [ ] = { "c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9", "c10", "s1", "s2", "s3", "s4", "s5", "s6", "s7", "s8", "s9", "s10", "d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8", "d9", "d10", "h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8", "h9", "h10"};
    for (int num = 0; num1 < 40; num++ ){
        Image names[num] = getImage ( getDocumentBase (), "c" + (num +1) + ".gif" );
    }
}
public void paint( Graphics screen ){

    int x = 10;
    for (int num5 = 0; num5 < 5; num5++ ){
        screen.drawImage( names[ (int) (Math.random () * 39)], x, 10, 100, 100, this );
        x = x + 100;
    }
}
}

P.S. I know that the kings queens and jacks are not being imported.

1
  • The string array and image array have the same name names Commented Oct 21, 2012 at 7:04

1 Answer 1

2

First you have to declare the array (at class level to make it available to multiple methods):

public class CardsJavaProgram extends Applet { 
    private Image[] images = new Image[40];

You also should change the varibable name (e.g. images), since you have other variable with the same name.

Then you can fill in the array:

for (int num = 0; num1 < 40; num++ ){   
    images[num] = //...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.