Results 1 to 12 of 12
Like Tree1Likes
  • 1 Post By Norm

Thread: Need help with assigning deck of card images to array.

  1. #1
    Duffed is offline Banned
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Need help with assigning deck of card images to array.

    Hi all :) Just wanted to say that I am very very new at this stuff and kind of need to know this urgently. I'm wondering on how to assign each card to their own specific image. For now, if I would have left the ImageIcon stuff out.. this would kind of worked i think.. but I need this to be displayed graphically.


    Deck class
    ---------------
    public Deck() {
    deck = new Card[52];
    for (int i = 0; i < deck.length; i++)
    deck[i] = new Card(new CardShape(i / 13 + 1),new CardNumber(i % 13 + 1)); <-- how do i arrange this? the error is here
    usedCards = 0;
    }

    Class class
    ---------------
    public class Card {
    private boolean discarded; //(not sure if i need to use discardings)
    private ImageIcon cimage;
    private CardShape cshape;
    private CardNumber cnumber;

    public Card(CardShape s, CardNumber n, ImageIcon x)
    {
    cshape = s;
    cnumber = n;
    cimage = x;
    }

    public static String getFilename( CardShape cshape, CardNumber cnumber ) {
    return cshape.getValue() + cnumber.getValue() + ".gif";
    }

    public ImageIcon getCardPicture () {
    return cimage;
    }


    ------


    cheerx

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,792
    Rep Power
    25

    Default Re: Need help with assigning deck of card images to array.

    how to assign each card to their own specific image
    Can you explain what the problem is with the code you posted?
    If you get errors, please copy and paste here the full text of the error messages.

  3. #3
    Duffed is offline Banned
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Re: Need help with assigning deck of card images to array.

    Apologies! Here's the error I get:

    no suitable constructor found for Card(CardShape,CardNumber).
    constructor Card.Card(Card) is not applicable (actual and formal argument lists differ in length)
    constructor Card.Card(CardShape,CardNumber,javax.swing.ImageIc on) is not applicable (actual and formal argument lists differ in length)

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,792
    Rep Power
    25

    Default Re: Need help with assigning deck of card images to array.

    no suitable constructor
    The error message describes the problem. Look at the class's constructors and see if there is one that matches the one you are trying to use. If there is not one like that, then either you change the statement that is calling the constructor or you add a new constructor that matches the one you are trying to call.

  5. #5
    Duffed is offline Banned
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Re: Need help with assigning deck of card images to array.

    Okay I think I got it but do you have any suggestions to get this done? Something along the lines of;

    deck[i] = new Card(new CardShape(i / 13 + 1),new CardNumber(i % 13 + 1) newImageIcon(??));

    to ensure each and every card has their own image (2 of hearts have 2h.gif)

    Thanks for the quick reply by the way :)
    Last edited by Duffed; 11-19-2011 at 03:50 AM.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,792
    Rep Power
    25

    Default Re: Need help with assigning deck of card images to array.

    to ensure each and every card has their own image
    You will need 52 different images and a naming convention to allow you to easily address the image you want.
    You seem to have that: (2 of hearts have 2h.gif)
    Then you must build the image file filenames by concatenating the value (eg.2) with the suit letter (eg. h).

  7. #7
    Duffed is offline Banned
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Re: Need help with assigning deck of card images to array.

    Ah yes, but isn't that what I did in my card class:


    public static String getFilename( CardShape cshape, CardNumber cnumber ) {
    return cshape.getValue() + cnumber.getValue() + ".gif";

    how would i apply this to the deck array? very sorry if i'm not making sense because it's literally my third week on java and just completely hammered by info-overload.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,792
    Rep Power
    25

    Default Re: Need help with assigning deck of card images to array.

    You need to specify the filename in the ImageIcon constructor when you call in the Card class's constructor. That is where you have the ?? in post #5.
    You should split the compound statement you are using to call the Card class's constructor up into separate simple calls to the three constructors so you can use the methods of the first two classes (CardShape & CardNumber) when you call the third class's constructor (ImageIcon) just like you are using those methods in the getFileName method.
    Fubarable likes this.

  9. #9
    Duffed is offline Banned
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Re: Need help with assigning deck of card images to array.

    Spent three days on this and still back without a clue :( Can you give some a code example to fix this?

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Need help with assigning deck of card images to array.

    I believe Norm has already told you how to do this. Get the face value of the card, get the suit of the card, concatenate them with .gif and you have your file name. Pass that to the ImageIcon constructor.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Need help with assigning deck of card images to array.

    Quote Originally Posted by Duffed View Post
    Spent three days on this and still back without a clue
    It looks like Norm has given you some good suggestions in post #8. What in particular confuses you about his suggestions?

    Can you give some a code example to fix this?
    That's not how it works here.

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Need help with assigning deck of card images to array.

    Quote Originally Posted by Fubarable View Post
    That's not how it works here.
    I'm guessing everyone is full of turkey and not much is working at all.

Similar Threads

  1. Creating a card class for a Deck of cards
    By adjit in forum New To Java
    Replies: 13
    Last Post: 04-06-2011, 11:20 PM
  2. Card deck arrays?
    By linc186 in forum New To Java
    Replies: 2
    Last Post: 03-16-2011, 10:56 PM
  3. Replies: 2
    Last Post: 12-13-2010, 12:12 PM
  4. Replies: 2
    Last Post: 04-30-2010, 06:13 PM
  5. Assigning Words to an Array
    By ScaryJello in forum New To Java
    Replies: 10
    Last Post: 02-17-2009, 05:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •