Hello,
So I am working on a programming assignment that plays a two player blackjack game. I have pretty much all the functionality of the game worked out. Everything was working correctly when I the cards where drawn by my paintComponent() method. However, as soon as I loaded .jpg card images the cards stopped matching up with values of the players' and dealer's hands. The best I can figure it is that the images are not loading into the array in the order I think they are. I fiddled around with it for awhile now and I could really use a fresh pair of eyes. Below are my methods to load the images, assign the image in the array to a card, and the paintComponent(). Also I uploaded a zip of the entire project. Thanks in advance.
private void loadImages() throws IOException{
String prefix = "images/";
String[] suits = { "b", "c", "d", "h", "s" };
String[] values = { "-1","-2","-4","-5","-6","-7","-8","-9","-10","-11","-12","-13",};
String ext = ".jpg";
int k = 0;
for(int i = 0;i < 53; i++) {
if(i == 0){
String path = prefix + suits[0] + ext;
URL url = getClass().getResource(path);
images[i] = ImageIO.read(url);
}
else if (i <=13){
String path = prefix + suits[1] + values[k] + ext;
URL url = getClass().getResource(path);
images[i] = ImageIO.read(url);
k++;
if(k==12){
k = 0;
}
}
else if (i <=26){
String path = prefix + suits[2] + values[k] + ext;
URL url = getClass().getResource(path);
images[i] = ImageIO.read(url);
k++;
if(k==12){
k = 0;
}
}
else if (i <=39){
String path = prefix + suits[3] + values[k] + ext;
URL url = getClass().getResource(path);
images[i] = ImageIO.read(url);
k++;
if(k==12){
k = 0;
}
}
else if (i <=52){
String path = prefix + suits[4] + values[k] + ext;
URL url = getClass().getResource(path);
images[i] = ImageIO.read(url);
k++;
if(k==12){
k = 0;
}
}
}
}
public void paintComponent(Graphics g) {
// The paint method shows the message at the bottom of the of the applet and controls cards drawn
super.paintComponent(g); // fill with background color.
g.setFont(bigFont);
g.setColor(Color.white);
g.drawString(message, 10, getSize().height - 10);
g.drawString(message2, 10, getSize().height - 40);
// Draw labels for the two sets of cards.
g.drawString("Dealer's Cards:", 400, 13);
g.drawString("Player 1's Cards:", 80, 510);
g.drawString("Player 2's Cards:", 580, 510);
g.setColor(Color.green);
// Draw dealer's cards. Draw first card face down if
// the game is still in progress, It will be revealed
// when the game ends.
g.setFont(smallFont);
if (gameInProgress)
g.drawImage(images[0], 400, 30, null);//drawCard(g, null, 400, 30);
else
g.drawImage(cardImageSelect(dealerHand.getCard(0)), 400, 30, null);//drawCard(g, dealerHand.getCard(0), 400, 30);
for (int i = 1; i < dealerHand.getCardCount(); i++)
g.drawImage(cardImageSelect(dealerHand.getCard(i)), 412 + i * 45, 30, null);
// Draw the user's cards.
for (int i = 0; i < player1Hand.getCardCount(); i++)
g.drawImage(cardImageSelect(player1Hand.getCard(i)), 80 + i * 45, 540, null);
for (int i = 0; i < player2Hand.getCardCount(); i++)
g.drawImage(cardImageSelect(player2Hand.getCard(i)), 580 + i * 45, 540, null);
} // end paint();
public Image cardImageSelect(Card c){
if(c.getSuitAsString() == "Clubs"){
switch ( c.getValueAsString() ) {
case "Ace": return images[1];
case "2": return images[2];
case "3": return images[3];
case "4": return images[4];
case "5": return images[5];
case "6": return images[6];
case "7": return images[7];
case "8": return images[8];
case "9": return images[9];
case "10": return images[10];
case "Jack": return images[11];
case "Queen": return images[12];
default: return images[13];
}
}
else if(c.getSuitAsString() == "Diamonds"){
switch ( c.getValueAsString() ) {
case "Ace": return images[14];
case "2": return images[15];
case "3": return images[16];
case "4": return images[17];
case "5": return images[18];
case "6": return images[19];
case "7": return images[20];
case "8": return images[21];
case "9": return images[22];
case "10": return images[23];
case "Jack": return images[24];
case "Queen": return images[25];
default: return images[26];
}
}
else if(c.getSuitAsString() == "Hearts"){
switch ( c.getValueAsString() ) {
case "Ace": return images[27];
case "2": return images[28];
case "3": return images[29];
case "4": return images[30];
case "5": return images[31];
case "6": return images[32];
case "7": return images[33];
case "8": return images[34];
case "9": return images[35];
case "10": return images[36];
case "Jack": return images[37];
case "Queen": return images[38];
default: return images[39];
}
}
else if(c.getSuitAsString() == "Spades"){
switch ( c.getValueAsString() ) {
case "Ace": return images[40];
case "2": return images[41];
case "3": return images[42];
case "4": return images[43];
case "5": return images[44];
case "6": return images[45];
case "7": return images[46];
case "8": return images[47];
case "9": return images[48];
case "10": return images[49];
case "Jack": return images[50];
case "Queen": return images[51];
default: return images[52];
}
}
else
return null;
}