-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApplet.java
executable file
·247 lines (209 loc) · 6.69 KB
/
Applet.java
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package quartoplus;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import javax.swing.DefaultListModel;
import javax.swing.JApplet;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
public class Applet extends JApplet
{
final static int BLANC = -1;
final static int NOIR = 1;
final static int VIDE = 0;
final private static int LARGEUR = 4;
final private static int HAUTEUR = 4;
final private static int TAILLEPIONS = 40;
private static final long serialVersionUID = 1L;
private JList brdList;
private Board displayBoard;
private JScrollPane scrollPane;
private DefaultListModel listModel;
private Frame myFrame;
static int cpt = 0;
public void init() {
System.out.println("Initialisation BoardApplet" + cpt++);
buildUI(getContentPane());
}
public void buildUI(Container container) {
setBackground(Color.white);
Piece[][] temp = new Piece[HAUTEUR][LARGEUR];
for (int i = 0; i < HAUTEUR; i++)
for (int j = 0; j < LARGEUR; j++)
temp[i][j] = Piece.zzzz;
displayBoard = new Board("Coups", temp);
listModel = new DefaultListModel();
listModel.addElement(displayBoard);
brdList = new JList(listModel);
brdList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
brdList.setSelectedIndex(0);
scrollPane = new JScrollPane(brdList);
Dimension d = scrollPane.getSize();
scrollPane.setPreferredSize(new Dimension(200, d.height));
brdList.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(KeyEvent e) {
brdList_keyPressed(e);
}
});
brdList.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
brdList_mouseClicked(e);
}
});
container.add(displayBoard, BorderLayout.CENTER);
container.add(scrollPane, BorderLayout.EAST);
}
public void update(Graphics g, Insets in) {
Insets tempIn = in;
g.translate(tempIn.left, tempIn.top);
paint(g);
}
public void paint(Graphics g) {
displayBoard.paint(g);
}
public void addBoard(String move, Piece[][] pieces) {
Board tempEntrop = new Board(move, pieces);
listModel.addElement(new Board(move, pieces));
brdList.setSelectedIndex(listModel.getSize() - 1);
brdList.ensureIndexIsVisible(listModel.getSize() - 1);
displayBoard = tempEntrop;
update(myFrame.getGraphics(), myFrame.getInsets());
}
public void setMyFrame(Frame f) {
myFrame = f;
}
void brdList_keyPressed(KeyEvent e) {
int index = brdList.getSelectedIndex();
if (e.getKeyCode() == KeyEvent.VK_UP && index > 0)
displayBoard = (Board) listModel.getElementAt(index - 1);
if (e.getKeyCode() == KeyEvent.VK_DOWN && index < (listModel.getSize() - 1))
displayBoard = (Board) listModel.getElementAt(index + 1);
update(myFrame.getGraphics(), myFrame.getInsets());
}
void brdList_mouseClicked(MouseEvent e) {
displayBoard = (Board) listModel.getElementAt(brdList.getSelectedIndex());
update(myFrame.getGraphics(), myFrame.getInsets());
}
// Sous classe qui dessine le plateau de jeu
class Board extends JPanel {
private static final long serialVersionUID = 1L;
Piece[][] boardState;
String move;
// The string will be the move details
// and the array the details of the board after the move has been applied.
public Board(String mv, Piece[][] pieces) {
boardState = pieces;
move = mv;
}
public void drawBoard(Graphics g) {
// First draw the lines
// Board
int bx = 30;
int by = 30;
boolean alt = true;
// axis labels
g.setColor(new Color(0, 0, 0));
for (int i = 1; i <= LARGEUR; i++) {
g.drawString("" + (char) ('A' + i - 1), i * TAILLEPIONS + 10, 20);
}
for (int i = 1; i <= HAUTEUR; i++) {
g.drawString("" + i, 5, i * TAILLEPIONS + 10);
}
// draw the squares
Color c1 = new Color(210, 210, 210);
Color c2 = new Color(190, 190, 190);
for (int i = 0; i < TAILLEPIONS * LARGEUR; i += TAILLEPIONS) {
for (int j = 0; j < TAILLEPIONS * HAUTEUR; j += TAILLEPIONS) {
g.setColor(alt ? c1 : c2);
alt = !alt;
g.fillRect(bx + i, by + j, TAILLEPIONS, TAILLEPIONS);
}
alt = !alt;
}
// Draw the pieces by referencing boardState array
for (int i = 0; i < LARGEUR; i++) {
for (int j = 0; j < HAUTEUR; j++) {
switch (boardState[j][i]) {
case bgpc:
drawingpion("bgpc",g,bx,by,i,j);
break;
case bgpr:
drawingpion("bgpr",g,bx,by,i,j);
break;
case bgtc:
drawingpion("bgtc",g,bx,by,i,j);
break;
case bgtr:
drawingpion("bgtr",g,bx,by,i,j);
break;
case bppc:
drawingpion("bppc",g,bx,by,i,j);
break;
case bppr:
drawingpion("bppr",g,bx,by,i,j);
break;
case bptc:
drawingpion("bptc",g,bx,by,i,j);
break;
case bptr:
drawingpion("bptr",g,bx,by,i,j);
break;
case rgpc:
drawingpion("rgpc",g,bx,by,i,j);
break;
case rgpr:
drawingpion("rgpr",g,bx,by,i,j);
break;
case rgtc:
drawingpion("rgtc",g,bx,by,i,j);
break;
case rgtr:
drawingpion("rgtr",g,bx,by,i,j);
break;
case rppc:
drawingpion("rppc",g,bx,by,i,j);
break;
case rppr:
drawingpion("rppr",g,bx,by,i,j);
break;
case rptc:
drawingpion("rptc",g,bx,by,i,j);
break;
case rptr:
drawingpion("rptr",g,bx,by,i,j);
break;
}
}
}
}
public void drawingpion(String text, Graphics g, int bx, int by, int i, int j){
FontMetrics fm = g.getFontMetrics();
double textWidth = fm.getStringBounds(text, g).getWidth();
g.setColor(Color.orange);
g.fillOval(bx + TAILLEPIONS * i + 6, by + TAILLEPIONS * j + 6, TAILLEPIONS - 8,
TAILLEPIONS - 8);
// Put text into circle
// What is the job of getstringbounds
g.setColor(Color.white);
g.drawString(text, (int) (bx+ TAILLEPIONS * i + 23 - textWidth/2),(int) (by+ TAILLEPIONS * j + 19 + fm.getMaxAscent() / 2));
}
public void paint(Graphics g) {
drawBoard(g);
}
public void update(Graphics g) {
drawBoard(g);
}
public String toString() {
return move;
}
}
}