4

I am trying to initialise a deck of cards, and display them (I have the images in .gif). The only problem I've encountered is initialising the deck itself. So far, I've tried to create four arrays (one for each suit) as such:

import java.applet.*;
import java.awt.*;

public class deckOfCards extends Applet
{
    public void init()
    {
        image clubs = new image[13];
        image hearts = new image[13];
        image spades = new image[13];
        image diamonds = new image[13];
    }
}

and then do something like this for each suit:

for( int i = 0; i <= 13; i++ )
{
    clubs[i] = getImage( getDocumentBase(), c(i).gif )
}

(the card files are saved in filenames c1.gif, c2.gif.....c13.gif for each suit)

I get an error saying that symbol "image" can't be found, but doesn't java.awt.image have a class to create the image object and image methods?

4
  • 7
    Its Image, with a capital I. Commented Jan 28, 2013 at 20:43
  • 1
    Your for-loop cannot go upto 13, should be less than that. Commented Jan 28, 2013 at 20:44
  • 1
    I would strongly recommend you make your program more object orientated by creating a Card object which has its own image and attributes, or I smell spaghetti code a' cooking. Commented Jan 28, 2013 at 20:47
  • 1
    I strongly recommend you to find a beginner book of Java, and learn about basics of the language: case-sensitiveness, what's the correct way to declare and use array, naming conventions, learn to read javadoc, and etc. Commented Jan 29, 2013 at 0:57

2 Answers 2

8

image is not a valid class in the AWT package, make the first letter uppercase.

You have some syntax issues:

  • Capital I in Image
  • Missing left-hand-side array brackets
  • Don't go beyond the index of your Image array when looping
  • Quotes needed for getImage call

Java naming conventions indicate that classes start with a capital letter, so too should your class:

public class DeckOfCards extends Applet {

    public void init() {

       Image[] clubs = new Image[13];
       for (int i = 0; i < clubs.length; i++ ) {
            clubs[i] = getImage( getDocumentBase(), "c" + (i + 1) + ".gif");
        }
        ...
    }
}

Also Applet is a museum piece and has been superseded by the lightweight javax.swing.JApplet.

Sign up to request clarification or add additional context in comments.

Comments

1

Thats the way you do it using ArrayList Containter. In practice ArrayList is.. an array, but much more flexible.

ArrayList<Image> arrayName = new ArrayList<Image>();
Image imageName = getImage(getCodeBase(),"direction.jpg");
arrayName.add(imageName);

5 Comments

+1 for the advice, but it doesn't fix the immediate issue of the OP
Yes, it does not. But maybe he's going to decide that he need more flexible data structure in that issue, there are plenty of ways, and I like to inform, and be informed, about alternatives.
I agree, it's nice to provide direction to people. I would, however, IMHO, suggest that you try and address the problem AND provide direction or leave it as a comment - the issue is, some people may choose to down vote the answer because they see it as unhelpful - just saying ;)
" But maybe he's going to decide that he need more flexible data structure" For a deck of 52 cards?
Ok MadProgrammer, i see you point and i will consider it, thanks :) Andrew - we dont know what is he going to do with that deck, do we? :)

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.