Add Cards

Learn to add all the cards to the deck and display a card on the window.

Adding cards to the deck

  1. Go to Deck class.
  2. Add a private static final integer array called VALUES, containing numbers 0 through 12.
  3. Add a private static final string called FILENAME, set to “/cards.png”.
  4. Add a private ArrayList of type Card called cards, initialized by calling ArrayList’s constructor.
    ...
 
private static final String[] RANKS = {
  "A",
  "2",
  "3",
  "4",
  "5",
  "6",
  "7",
  "8",
  "9",
  "10",
  "J",
  "Q",
  "K"
};
private static final String[] SUITSYMBOLS = {
  "\u2665",
  "\u2666",
  "\u2660",
  "\u2663"
};
private static final int[] VALUES = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
private static final int CARDWIDTH = 30;
private static final int CARDHEIGHT = 50;
private static final String FILENAME = "/cards.png";
  
  private ArrayList<Card> cards = new ArrayList<Card>();

...

In the constructor, create cards of every suit and rank, getting each card’s image from the image file.

  1. Add a constructor with no parameters.
  2. Add an InputStream called input, initialized by getting the deck’s class and then getting the resource as a stream using FILENAME. Use Object’s getClass() method, and Class’s getResourceAsStream() method.
  3. Add a BufferedImage called cardsImage, initialized by reading the image from input using ImageIO’s read() method.
  4. Add a for loop with suit as the iteration variable, looping as many times as the number of elements in SUITSYMBOLS using the array’s length variable.
  5. Add a for loop inside the previous loop, with rank as the iteration variable, looping through all elements in RANKS using the array’s length variable.
  6. Inside the inner for loop:
    • Create an integer called x, initialized to rank multiplied by CARDWIDTH.
    • Create an integer called y, initialized to suit multiplied by CARDHEIGHT.
    • Create an Image object called image, initialized to the portion of cardsImage at coordinates x and y, CARDWIDTH wide and CARDHEIGHT tall using BufferedImage’s getSubimage() method.
    • Create a new Card object called card, initialized by calling the Card constructor defined with these four parameters: rank, suit, value, and image. For parameter values, use the rank the value from element rank in RANKS, suit, the value from element rank in VALUES, and image.
    • Add card to cards.
  7. Place all the code in the constructor within a try block. If the code throws an IOException:
    • Create a string called message, stating the image file couldn’t be opened.
    • Display message in a message dialog using JOptionPane’s showMessageDialog() method.
    ...
 
  ____________ Deck(________) {
    try {
      _________ input = ________________;
      ____________ cardsImage = _______________;
      for(_______________) {
        for(_______________) {
          _______ x = ____________;
          _______ y = ____________;
          _______ image = __________________;
          _______ card = ____________________;
          cards.____________;
        }
      }
    }
    catch(______________) {
      ___________ message = ______________________;
      JOptionPane.______________________;
    }
  }
    ...

Get hands-on with 1200+ tech skills courses.