2
Copyright (C) 2005 Free Software Foundation, Inc.
4
This file is part of GNU Classpath.
6
GNU Classpath is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2, or (at your option)
11
GNU Classpath is distributed in the hope that it will be useful, but
12
WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
General Public License for more details.
16
You should have received a copy of the GNU General Public License
17
along with GNU Classpath; see the file COPYING. If not, write to the
18
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21
Linking this library statically or dynamically with other modules is
22
making a combined work based on this library. Thus, the terms and
23
conditions of the GNU General Public License cover the whole
26
As a special exception, the copyright holders of this library give you
27
permission to link this library with independent modules to produce an
28
executable, regardless of the license terms of these independent
29
modules, and to copy and distribute the resulting executable under
30
terms of your choice, provided that you also meet, for each linked
31
independent module, the terms and conditions of the license of that
32
module. An independent module is a module which is not derived from
33
or based on this library. If you modify this library, you may extend
34
this exception to your version of the library, but you are not
35
obligated to do so. If you do not wish to do so, delete this
36
exception statement from your version. */
39
package gnu.classpath.examples.CORBA.swing.x5;
41
import java.awt.Color;
42
import java.awt.Graphics;
43
import java.awt.Point;
44
import java.awt.event.MouseEvent;
45
import java.awt.event.MouseListener;
46
import java.rmi.RemoteException;
47
import java.util.ArrayList;
48
import java.util.Collection;
49
import java.util.Iterator;
51
import javax.swing.JComponent;
53
import org.omg.CORBA.ORB;
56
* Manages actions, related to the game rules and also does all painting.
58
* @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
60
public class PlayingDesk
62
implements MouseListener, State
65
* Use serialVersionUID for interoperability.
67
private static final long serialVersionUID = 1;
70
* Indicates that the field point state is the red oval.
72
public static final int RED = 0;
75
* Indicates that the field point state is the black cross.
77
public static final int BLACK = 1;
80
* Indicates that the field point state is the hint, suggested by the fan.
82
public static final int HINT = 2;
85
* The access to the main frame methods.
90
* The access to the player communicator.
110
* True if the player ORB started ok.
120
* The radius of the dots being painted.
122
static int R = W / 3;
125
* The collection of the red dots.
127
ArrayList reds = new ArrayList();
130
* The collection of the black dots.
132
ArrayList blacks = new ArrayList();
135
* The array of hints.
137
ArrayList hints = new ArrayList();
140
* When the game is completed, obtains the value of the two end points of the
149
player = new PlayerImpl();
152
OrbStarter.startPlayer(player, this);
163
* Paint this component.
165
public void paintComponent(Graphics g)
170
g.setColor(Color.white);
171
g.fillRect(0, 0, w, h);
178
* Check maybe a game is finished after setting the point N
180
public Point[] checkFinished(Collection x, Point N)
182
Iterator iter = x.iterator();
185
// The victory, if happens, must occur inside these boundaries:
192
while (iter.hasNext())
194
p = (Point) iter.next();
196
if (p.x > ax && p.x < bx && p.y > ay && p.y < by)
198
// Check the vertical line down
199
if (pointPresent(p.x, p.y + 1, x))
200
if (pointPresent(p.x, p.y + 2, x))
201
if (pointPresent(p.x, p.y + 3, x))
202
if (pointPresent(p.x, p.y + 4, x))
203
return new Point[] { p, new Point(p.x, p.y + 4) };
205
// Check the horizontal line left
206
if (pointPresent(p.x + 1, p.y, x))
207
if (pointPresent(p.x + 2, p.y, x))
208
if (pointPresent(p.x + 3, p.y, x))
209
if (pointPresent(p.x + 4, p.y, x))
210
return new Point[] { p, new Point(p.x + 4, p.y) };
212
// Check the diagonal line right down.
213
if (pointPresent(p.x + 1, p.y + 1, x))
214
if (pointPresent(p.x + 2, p.y + 2, x))
215
if (pointPresent(p.x + 3, p.y + 3, x))
216
if (pointPresent(p.x + 4, p.y + 4, x))
217
return new Point[] { p, new Point(p.x + 4, p.y + 4) };
219
// Check the diagonal line left down.
220
if (pointPresent(p.x - 1, p.y + 1, x))
221
if (pointPresent(p.x - 2, p.y + 2, x))
222
if (pointPresent(p.x - 3, p.y + 3, x))
223
if (pointPresent(p.x - 4, p.y + 4, x))
224
return new Point[] { p, new Point(p.x - 4, p.y + 4) };
231
* Called when the "end of the game" situation is detected.
233
public void drawFinishLine(int xa, int ya, int xb, int yb, Graphics g)
235
g.setColor(Color.blue);
238
g.drawLine(xa * W + hW, ya * W + hW, xb * W + hW, yb * W + hW);
242
* Check for the presence of the given point in the collection.
244
public final boolean pointPresent(int x, int y, Collection in)
246
Iterator iter = in.iterator();
248
while (iter.hasNext())
250
p = (Point) iter.next();
251
if (p.x == x && p.y == y)
257
public void drawGrid(int w, int h, Graphics g)
259
g.setColor(Color.lightGray);
261
// Draw vertical lines:
262
for (int x = 0; x < w; x += W)
264
g.drawLine(x, 0, x, h);
267
// Draw horizontal lines:
268
for (int y = 0; y < h; y += W)
270
g.drawLine(0, y, w, y);
273
g.setColor(Color.gray);
274
g.drawRect(0,0, frame.DESK_SIZE.width, frame.DESK_SIZE.height);
275
g.drawRect(0,0, frame.DESK_SIZE.width+3, frame.DESK_SIZE.height+3);
278
public void drawFigures(Graphics g)
280
g.setColor(Color.red);
281
drawDots(reds, g, RED);
283
g.setColor(Color.black);
284
drawDots(blacks, g, BLACK);
286
g.setColor(Color.lightGray);
287
drawDots(hints, g, HINT);
289
if (endOfGame != null)
290
drawFinishLine(endOfGame[0].x, endOfGame[0].y, endOfGame[1].x,
294
public Point makePoint(int x, int y)
296
return new Point(x / W, y / W);
300
* Draw a collection of dots (the collor must be set before calling the
303
public void drawDots(Collection dots, Graphics g, int mode)
305
Iterator iter = dots.iterator();
313
while (iter.hasNext())
315
p = (Point) iter.next();
320
g.drawOval(x - R, y - R, RR, RR);
321
else if (mode == BLACK)
323
g.drawLine(x - R, y - R, x + R, y + R);
324
g.drawLine(x - R, y + R, x + R, y - R);
329
g.drawOval(x - hR, y - hR, R, R);
334
private void jbInit()
337
addMouseListener(this);
340
public void mouseClicked(MouseEvent e)
344
int state = player.get_current_state();
346
// Check if the state is correct.
347
if (state == I_WAIT_FOR_YOUR_MOVE)
349
frame.talk(Color.black,
350
"It is now time for our partner's move, not ours. Please wait.");
352
else if (state == DISCONNECTED)
354
frame.talk(Color.black,
355
"We are not connected to the playing partner yet.");
357
else if (state == I_HAVE_LOST)
359
frame.talk(Color.black,
360
"We have already lost this battle, but why not to try again?");
362
else if (state == I_HAVE_WON)
364
frame.talk(Color.black,
365
"The victory is ours, nothing more to do here.");
367
else if (player.partner == null)
368
frame.talk(Color.black, "No other player so far.");
374
if (x>frame.DESK_SIZE.width ||
375
y>frame.DESK_SIZE.height)
377
frame.talk(Color.black,"Outside the game area.");
381
Point p = makePoint(x, y);
383
// Ignore clicks on the occupied cells.
384
if (pointPresent(p.x, p.y, reds)
385
|| (pointPresent(p.x, p.y, blacks)))
387
frame.talk(Color.black,
388
"This is against the rules, select the unoccupied cell.");
394
endOfGame = checkFinished(reds, p);
397
if (endOfGame != null)
399
frame.talk(Color.red, "Our move " + p.x + "-" + p.y
401
player.set_current_state(I_HAVE_WON);
405
frame.talk(Color.black, "Our move " + p.x + "-" + p.y
406
+ ". Waiting for the other side move...");
407
player.set_current_state(I_WAIT_FOR_YOUR_MOVE);
410
player.partner.receive_move(p.x, p.y, endOfGame);
413
catch (RemoteException ex)
415
// We will print the exception because this is a demo application
416
// that may be modified for learning purposes.
417
ex.printStackTrace();
422
* Handle the move of the other playing side.
424
public void friendsMove(int x, int y, Point[] victory)
428
int state = player.get_current_state();
429
if (state != I_WAIT_FOR_YOUR_MOVE || pointPresent(x, y, blacks))
431
stateFailed("Move " + x + "-" + y);
435
blacks.add(new Point(x, y));
439
frame.talk(Color.red,
440
" We have lost this time, unfortunately..");
441
player.set_current_state(I_HAVE_LOST);
446
frame.talk(Color.black, "Partner goes " + x + "-" + y
448
player.set_current_state(I_THINK);
453
catch (RemoteException rex)
455
rex.printStackTrace();
460
* Prepare for the new game.
471
public void mouseEntered(MouseEvent m)
476
public void mousePressed(MouseEvent m)
481
public void mouseReleased(MouseEvent m)
486
public void mouseExited(MouseEvent m)
492
* The systems detected the error conditions. The game cannot continue (the
493
* chat is still possible).
495
public void stateFailed(String reason)
499
player.receive_chat(ChatConstants.REMOTE_PLAYER,
500
"Wrong move, game cannot continue (our state was "
501
+ player.get_current_state() + ")");
502
frame.talk(Color.red, "The remote side violates communicating rules.");
503
player.set_current_state(State.ERROR);
505
catch (RemoteException ex)
507
// We will print the exception because this is a demo application
508
// that may be modified for learning purposes.
509
ex.printStackTrace();
b'\\ No newline at end of file'