Add Cards
Learn to add all the cards to the deck and display a card on the window.
We'll cover the following
Adding cards to the deck
- Go to
Deck
class. - Add a private static final integer array called
VALUES
, containing numbers0
through12
. - Add a private static final string called
FILENAME
, set to “/cards.png”. - Add a private
ArrayList
of typeCard
calledcards
, 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.
- Add a constructor with no parameters.
- Add an
InputStream
calledinput
, initialized by getting the deck’s class and then getting the resource as a stream usingFILENAME
. Use Object’sgetClass()
method, and Class’sgetResourceAsStream()
method. - Add a
BufferedImage
calledcardsImage
, initialized by reading the image frominput
using ImageIO’sread()
method. - 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. - Add a for loop inside the previous loop, with rank as the iteration variable, looping through all elements in
RANKS
using the array’slength
variable. - Inside the inner for loop:
- Create an integer called
x
, initialized to rank multiplied byCARDWIDTH
. - Create an integer called
y
, initialized to suit multiplied byCARDHEIGHT
. - Create an Image object called
image
, initialized to the portion ofcardsImage
at coordinatesx
andy
,CARDWIDTH
wide andCARDHEIGHT
tall using BufferedImage’sgetSubimage()
method. - Create a new Card object called
card
, initialized by calling the Card constructor defined with these four parameters:rank
,suit
,value
, andimage
. For parameter values, use the rank the value from element rank inRANKS
, suit, the value from element rank inVALUES
, and image. - Add
card
tocards
.
- Create an integer called
- Place all the code in the constructor within a
try
block. If the code throws anIOException
:- Create a string called
message
, stating the image file couldn’t be opened. - Display
message
in a message dialog using JOptionPane’sshowMessageDialog()
method.
- Create a string called
...
____________ Deck(________) {
try {
_________ input = ________________;
____________ cardsImage = _______________;
for(_______________) {
for(_______________) {
_______ x = ____________;
_______ y = ____________;
_______ image = __________________;
_______ card = ____________________;
cards.____________;
}
}
}
catch(______________) {
___________ message = ______________________;
JOptionPane.______________________;
}
}
...
Get hands-on with 1200+ tech skills courses.