The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
November 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Multiple Threads

Posted by Hiran on November 30, 2001 at 10:23 PM

I have a class that deals with the images for a deck of playing cards (both the front and back images) for the game of solitaire.
Here is the code so far for this class:


package project.solitaire;
import java.awt.*;
import java.net.*;

public class Images
{ private Image[] frontImages = new Image[Constants.NUM_CARDS_IN_DECK];
private Image[] backImages = new Image[1];
private Toolkit toolKit = Toolkit.getDefaultToolkit();
private boolean[] frontImagesLoadingStatus = new boolean[Constants.NUM_CARDS_IN_DECK];
private boolean[] backImagesLoadingStatus = new boolean[1];

public Images()
{
}

/** Returns the loading status of the image specified. If true,
all, that image has been loaded.
@param side The side (face or back) of the card (see getCardImage)
@param suit The suit of the card.
@param rank The rank of the card.
@return boolean The loading status of the card (true for fully loaded,
false otherwise). */
public boolean getLoadingStatus(int side, int suit, int rank)
{ boolean status = false;
int elementNum = suit * 13 + rank;
if (side == 1)
{ status = frontImagesLoadingStatus[elementNum];
} else if (side == 2)
{ status = frontImagesLoadingStatus[elementNum];
}
return status;
}

/** Gets a certain image to display for the cards.
@param side A value specifiying whether the image is for the front of
the card (face) or back. A 1 specifies front, a 2 specifies back.
@param suit The suit of the card.
@param rank The rank of the card.
@return Image The image for the card. */
public Image getCardImage(int side, int suit, int rank)
{ Image imageToReturn = toolKit.getImage("");
int elementNum = suit * 13 + rank;
if (side == 1)
{ imageToReturn = frontImages[elementNum];
} else if (side == 2)
{ imageToReturn = backImages[elementNum];
}
return imageToReturn;
}

/** Initializes the images used for the cards. */
private void initImages(int suit, int rank)
{ int elementNum = suit * 13 + rank;
frontImages[elementNum] = toolKit.getImage(getPath(suit, rank));
frontImagesLoadingStatus[elementNum] = true;
}

/** Returns the full path for the card's face image.
@param suit The suit of the card.
@param rank The rank of the card.
@return String The full path for the card's face image. */
private String getPath(int suit, int rank)
{ String suitPath = getSuitPath(suit);
String rankPath = getRankPath(rank);
String fullPath = Constants.BASE_PATH + rankPath + suitPath + ".gif";
return fullPath;
}

/** Returns the rank section of the path for the card's face image.
@param rank The rank of the card.
@return String The rank section of the path for the card's face image. */
private String getRankPath(int rank)
{ String rankPath = "";
if (rank == 0)
{ rankPath = "a";
} else if (rank == 1)
{ rankPath = "2";
} else if (rank == 2)
{ rankPath = "3";
} else if (rank == 3)
{ rankPath = "4";
} else if (rank == 4)
{ rankPath = "5";
} else if (rank == 5)
{ rankPath = "6";
} else if (rank == 6)
{ rankPath = "7";
} else if (rank == 7)
{ rankPath = "8";
} else if (rank == 8)
{ rankPath = "9";
} else if (rank == 9)
{ rankPath = "t";
} else if (rank == 10)
{ rankPath = "j";
} else if (rank == 11)
{ rankPath = "q";
} else if (rank == 12)
{ rankPath = "k";
}
return rankPath;
}

/** Returns the suit section of the path for the card's face image.
@param suit The suit of the card.
@return String The suit section of the path for the card's face image. */
private String getSuitPath(int suit)
{ String suitPath = "";
if (suit == 0)
{ suitPath = "c";
} else if (suit == 1)
{ suitPath = "h";
} else if (suit == 2)
{ suitPath = "s";
} else if (suit == 3)
{ suitPath = "d";
}
return suitPath;
}
}


I have it set up so that the method initImage takes in the particular suit and rank and creates the image for that card and puts it in the proper element in the array. (As you can see, I haven't implemented code to deal with the images for the back of the cards, but that shouldn't be too hard). I also have two methods getLoadingStatus which returns the loading status of the image (true means that the image is fully loaded) and get getCardImage which returns the image associated with that card. The class that draws the cards calls the getLoadingStatus method and if it returns true, draws the image using the getCardImage method and Graphic's drawImage method, otherwise an outline is drawn with the name of the card ("rank of suit"). I want to call the initImage method for each card as a seperate Thread when the GUI is being created. That way, each card should be loaded by the time the cards are being drawn. My question is: how do I do this? I know that I can get Images to extend Thread (and override the run method by placing one in Images) and then when I create an Images object, I just call the start method. But how do I do this for each card? The method initImages takes in the suit and rank of the card, but if I override the run method (and in the overriden method call initImages), how am I going to specify the suit and rank? Essentially, I want something like this:

Images cardImage = new Images();
for (int i=0; i<4; i++)
{ for (int j=0; j<13; j++)
{ cardImage.start(i, j);
}
}

Again, the problem is that I can't pass objects because the run method doesn't take parameters. And if I have something like this:

(This is in the Images class)
public void run()
{ for (int i=0; i<4; i++)
{ for (int j=0; j<13; j++)
{ initImages(i, j);
}
}
}

I'd still be trying to load all the images in one thread, which will not work, because the cards will be drawn before all the images load. (And there is only one paint method that paints the cards at the beginning when the GUI is created and then all other calls to change the graphics of the cards are calls to repaint, so if the images aren't ready when the paint method is called, they (the images) will never get drawn). I know this is long, but if you can help, please do. Thanks in advance.
Hiran
PS. If there is a better way to load all the images (52+ images) quickly (maybe by buffering them somehow) so that they appear instantly when the game loads, can you please let me know? Thnx!



Replies:
  • loading Chin Loong December 01, 2001 at 12:16 AM (3)
    • Will that do? Hiran December 01, 2001 at 1:32 PM (2)
      • thread Chin Loong December 01, 2001 at 2:37 PM (1)
        • oopsie Chin Loong December 01, 2001 at 2:40 PM (0)

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us