I have created a method which fills an array with 52 objects. Each object has a String suit, String face, int value, and a buffered image. Each object gets its image from a larger image and a getSubImage method. I am trying to make a GUI which displays a card from the deck, but I don't know how to reference the objects in the array. The standard array[num] does not work for the ______in the statement
JLabel cardLabel = new JLabel(new ImageIcon(_______.cardImage))
I think I need to call the buffered image by name, but I'm just not sure what to use. Here is my full code (Card_Class is my class for constructing the card objects):
package com.company;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import static javax.swing.WindowConstants.EXIT_ON_CLOSE;
public class Deck {
private Card_Class[] deckOfCards;
private int currentCard;
public Deck() throws IOException {
String[] faces = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
String[] suits = {"Diamonds", "Clubs", "Hearts", "Spades"};
deckOfCards = new Card_Class[52];
currentCard = 0;
final int width = 10;
final int height = 17;
final int rows =4;
final int columns = 13;
BufferedImage bigImage = ImageIO.read(new File("AllCards.png"));
BufferedImage tempCardImage;
for(int suit=0;suit <4; suit++){
for(int face=0;face<13;face++){
tempCardImage = bigImage.getSubimage(
face*width+(face*8),
suit*height+(suit*14),
width,
height);
deckOfCards[(face+(suit*13))] = new Card_Class(suits[suit],faces[face],(13*suit+face),tempCardImage);
}
}
}
public void displayDeck(){
for(Card_Class card: deckOfCards)
System.out.println(card);
}
public static void main(String[] args) throws IOException {
Deck theDeck = new Deck();
JFrame window = new JFrame("Display for a card object");
window.setSize(400, 600);
window.setDefaultCloseOperation(EXIT_ON_CLOSE);
window.setVisible(true);
JPanel contentPane = new JPanel(new BorderLayout());
JLabel cardLabel = new JLabel(new ImageIcon(_______.cardImage));
cardLabel.setSize(300, 400);
contentPane.add(cardLabel);
window.add(contentPane);
}
}