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 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
| //////////////////////////////////////////////
//
// GO java applet, Ver 1.1
//
// Author: Chenyang Xu, chenyang@jhu.edu
// http://iacl.ece.jhu.edu/~chenyang
//
//////////////////////////////////////////////
import java.applet.Applet;
import java.awt.*;
import java.util.Vector;
public class go extends Applet {
int AppletWidth, AppletHeight,
BoardLeft, BoardTop, BoardSize, Delta,
mouseMoveX, mouseMoveY,
StoneColor, START;
boolean REMOVE;
Vector StoneList = new Vector();
int[][] BoardMap = new int[19][19];
Image offscreenImg;
Graphics offscreenG;
Button Start = new Button("Start");
Button Finish = new Button("Finish");
Checkbox Remove = new Checkbox("Remove", null, false);
public void init() {
START = 0;
REMOVE = false;
add(Start);
add(Remove);
add(Finish);
Remove.disable();
Finish.disable();
Dimension d = size();
AppletHeight = d.height;
AppletWidth = d.width;
offscreenImg = createImage(AppletWidth, AppletHeight);
offscreenG = offscreenImg.getGraphics();
if (AppletHeight < AppletWidth)
BoardSize = (int) (0.9 * AppletHeight);
else
BoardSize = (int) (0.9 * AppletWidth);
Delta = BoardSize/18;
BoardLeft = (AppletWidth - BoardSize)/2;
BoardTop = (AppletHeight - BoardSize)/2 + Delta/2;
StoneList.removeAllElements();
mouseMoveX = -10;
mouseMoveY = -10;
StoneColor = 0; // 0 --> black, 1 --> white
// initialize the BoardMap array to zeros
for (int i=0; i<19; i++)
for (int j=0; j<19; j++)
BoardMap[i][j] = 0;
repaint();
}
public void paint(Graphics g) {
Color BoardColor;
int x0, y0, x1, y1, n, i, j, stone_color;
Integer tempInt;
// Draw the background of the applet
offscreenG.setColor(Color.gray);
offscreenG.fillRect(0, 0, AppletWidth, AppletHeight);
// Paint the GO board
BoardColor = new Color(230,205,80); // Draw the board background
offscreenG.setColor(BoardColor);
offscreenG.fillRect(BoardLeft, BoardTop, BoardSize-1, BoardSize-1);
offscreenG.setColor(Color.black); // Draw the board grids
for (i=0; i<= 18; i++) {
x0 = BoardLeft; y0 = i*Delta + BoardTop;
x1 = 18*Delta + BoardLeft; y1 = i*Delta + BoardTop;
offscreenG.drawLine(x0, y0, x1, y1); // draw the row
x0 = i*Delta + BoardLeft; y0 = BoardTop;
x1 = i*Delta + BoardLeft; y1 = 18*Delta + BoardTop;
offscreenG.drawLine(x0, y0, x1, y1); // draw the column
}
for (i=0; i<3; i++) // Draw the board markers
for (j=0; j<3; j++) {
x0 = (3 + i*6) * Delta + BoardLeft - 2 ;
y0 = (3 + j*6) * Delta + BoardTop - 2;
offscreenG.fillRect(x0, y0, 5, 5);
}
// Paint the stone list
for (n=0; n < StoneList.size(); n+=3) {
tempInt = (Integer) StoneList.elementAt(n);
i = tempInt.intValue();
tempInt = (Integer) StoneList.elementAt(n+1);
j = tempInt.intValue();
tempInt = (Integer) StoneList.elementAt(n+2);
stone_color = tempInt.intValue();
if (stone_color==0) { // Draw the black stone
offscreenG.setColor(Color.black);
offscreenG.fillOval((int)((i-0.5)*Delta) + BoardLeft,
(int)((j-0.5)*Delta) + BoardTop, Delta, Delta);
} else { // Draw the white stone
offscreenG.setColor(Color.white);
offscreenG.fillOval((int)((i-0.5)*Delta) + BoardLeft + 1,
(int)((j-0.5)*Delta) + BoardTop + 1, Delta-2, Delta-2);
offscreenG.setColor(Color.black);
offscreenG.drawOval((int)((i-0.5)*Delta) + BoardLeft + 1,
(int)((j-0.5)*Delta) + BoardTop + 1, Delta-2, Delta-2);
}
// paint a marker on the current stone
if (n == StoneList.size()-3) {
if (stone_color==0) { // Draw the black marker
offscreenG.setColor(Color.white);
offscreenG.fillRect(i*Delta + BoardLeft - 1,
j*Delta + BoardTop - 1, 3, 3);
} else { // Draw the white marker
offscreenG.setColor(Color.black);
offscreenG.fillRect(i*Delta + BoardLeft -1,
j*Delta + BoardTop - 1, 3, 3);
}
}
}
// Paint the stone moving with the mouse
if (START!=0 && mouseMoveX > BoardLeft-3 && mouseMoveY > BoardTop-3 &&
mouseMoveX < BoardLeft+BoardSize+3
&& mouseMoveY < BoardTop+BoardSize+3) {
if (!REMOVE) { // plot stone if REMOVE state is false
if (StoneColor==0) { // Draw the black stone
offscreenG.setColor(Color.black);
offscreenG.fillOval(mouseMoveX-Delta/2 + 1,
mouseMoveY-Delta/2 + 1, Delta-2, Delta-2);
} else { // Draw the white stone
offscreenG.setColor(Color.white);
offscreenG.fillOval(mouseMoveX-Delta/2 + 1,
mouseMoveY-Delta/2 + 1, Delta-2, Delta-2);
offscreenG.setColor(Color.black);
offscreenG.drawOval(mouseMoveX-Delta/2,
mouseMoveY-Delta/2, Delta, Delta);
}
} else { // draw the removing cross symbol
x0 = mouseMoveX-Delta/2 + 2;
y0 = mouseMoveY-Delta/2 + 2;
x1 = x0 + Delta - 4;
y1 = y0 + Delta - 4;
offscreenG.setColor(Color.red);
offscreenG.drawLine(x0, y0, x1, y1);
offscreenG.drawLine(x0+1, y0, x1, y1-1);
offscreenG.drawLine(x0, y0+1, x1-1, y1);
offscreenG.drawLine(x0, y1, x1, y0);
offscreenG.drawLine(x0, y1-1, x1-1, y0);
offscreenG.drawLine(x0+1, y1, x1, y0+1);
}
}
g.drawImage(offscreenImg, 0, 0, this);
}
public Point stoneCoord(int x, int y) {
int i, j;
Point p = new Point(0,0);
p.x = (int) (((float)(x)-BoardLeft)/Delta+0.5);
p.y = (int) (((float)(y)-BoardTop)/Delta+0.5);
return p;
}
public boolean mouseDown(Event e, int x, int y) {
Point coord = stoneCoord(x,y);
int MapVal, index, i, j;
Integer tempInt;
boolean found;
if (START==1 && x > BoardLeft-3 && y > BoardTop-3 &&
x < BoardLeft+BoardSize+3 && y < BoardTop+BoardSize+3) {
// add stone while in play
MapVal = BoardMap[coord.y][coord.x];
if (REMOVE && MapVal != 0) {
// handle removing stones
found = false;
int n = 0;
while (!found && n < StoneList.size()) {
tempInt = (Integer) StoneList.elementAt(n);
i = tempInt.intValue();
tempInt = (Integer) StoneList.elementAt(n+1);
j = tempInt.intValue();
if (i==coord.x && j==coord.y)
found = true;
n += 3;
}
// under no circumstance, found must be true when finish
// the above loop unless there is a bug in the program.
// So I don't deal with exceptions here.
StoneList.removeElementAt(n-3);
StoneList.removeElementAt(n-3);
StoneList.removeElementAt(n-3);
BoardMap[coord.y][coord.x] = 0;
repaint();
} else if (!REMOVE && MapVal == 0) {
// handle adding stones
BoardMap[coord.y][coord.x] = 1;
StoneList.addElement(new Integer(coord.x));
StoneList.addElement(new Integer(coord.y));
StoneList.addElement(new Integer(StoneColor));
StoneColor = 1 - StoneColor;
repaint();
}
}
return true;
}
public boolean mouseMove(Event e, int x, int y) {
mouseMoveX = x;
mouseMoveY = y;
repaint();
return true;
}
public boolean action(Event evt, Object arg) {
if (evt.target instanceof Button)
handleButton((String)arg);
else if (evt.target instanceof Checkbox)
handleCheckbox(evt);
return true;
}
void handleButton(String bname) {
if (bname.equals("Start")) {
START = 1;
Start.disable();
Remove.enable();
Finish.enable();
// initialize the BoardMap array to zeros
for (int i=0; i<19; i++)
for (int j=0; j<19; j++)
BoardMap[i][j] = 0;
repaint();
} else if (bname.equals("Finish")) {
StoneList.removeAllElements();
START = 0;
REMOVE = false;
StoneColor = 0;
Start.enable();
Remove.setState(false);
Remove.disable();
Finish.disable();
repaint();
}
else; // handle exceptions
}
void handleCheckbox(Event evt) {
Checkbox currentCheckbox = (Checkbox)evt.target;
if (currentCheckbox.getLabel() == "Remove") {
REMOVE = Remove.getState();
repaint();
}
}
public void update(Graphics g) {
paint(g);
}
} |
Partager