-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAssig5 Phase 1.java
More file actions
133 lines (119 loc) · 3.86 KB
/
Copy pathAssig5 Phase 1.java
File metadata and controls
133 lines (119 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* ----------------------------------------------------------------------------------------------------------------
Nautilus Group
Caleb Allen
Daisy Mayorga
David Harrison
Dustin Whittington
Michael Cline
CST 338
M5: GUI Card Java Program
30 May 2017
PURPOSE
Over several phases, we will be using the classes we wrote from M3 (Card, Hand, and Deck) and adding to those classes
the a GUI framework. To do this we will use some additional classes and create some of our own.
This is the first phase out of three. This first phase will allow us to debug the problem of reading the .gif files
and displaying them on a JFrame without any excess logic or class complexity. These .gif files represent the front
card images and the back card image.
----------------------------------------------------------------------------------------------------------------- */
import javax.swing.*;
import java.awt.*;
public class Assig5
{
// static for the 57 icons and their corresponding labels
// normally we would not have a separate label for each card, but
// if we want to display all at once using labels, we need to.
static final int NUM_CARD_IMAGES = 57; // 52 + 4 jokers + 1 back-of-card image
static Icon[] icon = new ImageIcon[NUM_CARD_IMAGES];
static void loadCardIcons()
{
// build the file names ("AC.gif", "2C.gif", "3C.gif", "TC.gif", etc.)
// in a SHORT loop. For each file name, read it in and use it to
// instantiate each of the 57 Icons in the icon[] array.
int cardNumber = 0;
for (int j = 0; j <= 3; j++)
{
for (int k = 0; k <= 13; k++)
{
icon[cardNumber] = new ImageIcon("images/" + turnIntIntoCardValue(k) + turnIntIntoCardSuit(j) + ".gif");
cardNumber++;
}
}
icon[cardNumber] = new ImageIcon("images/BK.gif");
}
// turns 0 - 13 into "A", "2", "3", ... "Q", "K", "X"
static String turnIntIntoCardValue(int k)
{
switch (k)
{
case 0:
return "A";
case 1:
return "2";
case 2:
return "3";
case 3:
return "4";
case 4:
return "5";
case 5:
return "6";
case 6:
return "7";
case 7:
return "8";
case 8:
return "9";
case 9:
return "T";
case 10:
return "J";
case 11:
return "Q";
case 12:
return "K";
case 13:
return "X";
}
return "A";
}
// turns 0 - 3 into "C", "D", "H", "S"
static String turnIntIntoCardSuit(int j)
{
switch (j)
{
case 0:
return "C";
case 1:
return "D";
case 2:
return "H";
case 3:
return "S";
}
return "S";
}
// a simple main to throw all the JLabels out there for the world to see
public static void main(String[] args)
{
int k;
// prepare the image icon array
loadCardIcons();
// establish main frame in which program will run
JFrame frmMyWindow = new JFrame("Card Room");
frmMyWindow.setSize(1150, 650);
frmMyWindow.setLocationRelativeTo(null);
frmMyWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// set up layout which will control placement of buttons, etc.
FlowLayout layout = new FlowLayout(FlowLayout.CENTER, 5, 20);
frmMyWindow.setLayout(layout);
// prepare the image label array
JLabel[] labels = new JLabel[NUM_CARD_IMAGES];
for (k = 0; k < NUM_CARD_IMAGES; k++)
labels[k] = new JLabel(icon[k]);
// place your 3 controls into frame
for (k = 0; k < NUM_CARD_IMAGES; k++)
frmMyWindow.add(labels[k]);
// show everything to the user
frmMyWindow.setVisible(true);
}
}