~ubuntu-branches/ubuntu/precise/classpath/precise

« back to all changes in this revision

Viewing changes to examples/gnu/classpath/examples/CORBA/swing/x5/PlayingDesk.java

  • Committer: Bazaar Package Importer
  • Author(s): Michael Koch
  • Date: 2006-05-27 16:11:15 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20060527161115-h6e39eposdt5snb6
Tags: 2:0.91-3
* Install header files to /usr/include/classpath.
* debian/control: classpath: Conflict with jamvm < 1.4.3 and
  cacao < 0.96 (Closes: #368172).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* PlayingDesk.java --
 
2
 Copyright (C) 2005 Free Software Foundation, Inc.
 
3
 
 
4
 This file is part of GNU Classpath.
 
5
 
 
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)
 
9
 any later version.
 
10
 
 
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.
 
15
 
 
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
 
19
 02110-1301 USA.
 
20
 
 
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
 
24
 combination.
 
25
 
 
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. */
 
37
 
 
38
 
 
39
package gnu.classpath.examples.CORBA.swing.x5;
 
40
 
 
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;
 
50
 
 
51
import javax.swing.JComponent;
 
52
 
 
53
import org.omg.CORBA.ORB;
 
54
 
 
55
/**
 
56
 * Manages actions, related to the game rules and also does all painting.
 
57
 *
 
58
 * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
 
59
 */
 
60
public class PlayingDesk
 
61
  extends JComponent
 
62
  implements MouseListener, State
 
63
{
 
64
  /**
 
65
   * Use serialVersionUID for interoperability.
 
66
   */
 
67
  private static final long serialVersionUID = 1;
 
68
 
 
69
  /**
 
70
   * Indicates that the field point state is the red oval.
 
71
   */
 
72
  public static final int RED = 0;
 
73
 
 
74
  /**
 
75
   * Indicates that the field point state is the black cross.
 
76
   */
 
77
  public static final int BLACK = 1;
 
78
 
 
79
  /**
 
80
   * Indicates that the field point state is the hint, suggested by the fan.
 
81
   */
 
82
  public static final int HINT = 2;
 
83
 
 
84
  /**
 
85
   * The access to the main frame methods.
 
86
   */
 
87
  ClientFrame frame;
 
88
 
 
89
  /**
 
90
   * The access to the player communicator.
 
91
   */
 
92
  PlayerImpl player;
 
93
 
 
94
  /**
 
95
   * The game manager.
 
96
   */
 
97
  GameManager manager;
 
98
 
 
99
  /**
 
100
   * The player ORB.
 
101
   */
 
102
  ORB orb;
 
103
 
 
104
  /**
 
105
   * The player IOR.
 
106
   */
 
107
  String ior;
 
108
 
 
109
  /**
 
110
   * True if the player ORB started ok.
 
111
   */
 
112
  boolean ok;
 
113
 
 
114
  /**
 
115
   * The grid spacing.
 
116
   */
 
117
  static int W = 16;
 
118
 
 
119
  /**
 
120
   * The radius of the dots being painted.
 
121
   */
 
122
  static int R = W / 3;
 
123
 
 
124
  /**
 
125
   * The collection of the red dots.
 
126
   */
 
127
  ArrayList reds = new ArrayList();
 
128
 
 
129
  /**
 
130
   * The collection of the black dots.
 
131
   */
 
132
  ArrayList blacks = new ArrayList();
 
133
 
 
134
  /**
 
135
   * The array of hints.
 
136
   */
 
137
  ArrayList hints = new ArrayList();
 
138
 
 
139
  /**
 
140
   * When the game is completed, obtains the value of the two end points of the
 
141
   * created line.
 
142
   */
 
143
  Point[] endOfGame;
 
144
 
 
145
  public PlayingDesk()
 
146
  {
 
147
    try
 
148
      {
 
149
        player = new PlayerImpl();
 
150
        player.desk = this;
 
151
 
 
152
        OrbStarter.startPlayer(player, this);
 
153
 
 
154
        jbInit();
 
155
      }
 
156
    catch (Exception e)
 
157
      {
 
158
        e.printStackTrace();
 
159
      }
 
160
  }
 
161
 
 
162
  /**
 
163
   * Paint this component.
 
164
   */
 
165
  public void paintComponent(Graphics g)
 
166
  {
 
167
    int w = getWidth();
 
168
    int h = getHeight();
 
169
 
 
170
    g.setColor(Color.white);
 
171
    g.fillRect(0, 0, w, h);
 
172
 
 
173
    drawGrid(w, h, g);
 
174
    drawFigures(g);
 
175
  }
 
176
 
 
177
  /**
 
178
   * Check maybe a game is finished after setting the point N
 
179
   */
 
180
  public Point[] checkFinished(Collection x, Point N)
 
181
  {
 
182
    Iterator iter = x.iterator();
 
183
    Point p;
 
184
 
 
185
    // The victory, if happens, must occur inside these boundaries:
 
186
    int ax = N.x - 5;
 
187
    int bx = N.x + 5;
 
188
 
 
189
    int ay = N.y - 5;
 
190
    int by = N.y + 5;
 
191
 
 
192
    while (iter.hasNext())
 
193
      {
 
194
        p = (Point) iter.next();
 
195
 
 
196
        if (p.x > ax && p.x < bx && p.y > ay && p.y < by)
 
197
          {
 
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) };
 
204
 
 
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) };
 
211
 
 
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) };
 
218
 
 
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) };
 
225
          }
 
226
      }
 
227
    return null;
 
228
  }
 
229
 
 
230
  /**
 
231
   * Called when the "end of the game" situation is detected.
 
232
   */
 
233
  public void drawFinishLine(int xa, int ya, int xb, int yb, Graphics g)
 
234
  {
 
235
    g.setColor(Color.blue);
 
236
 
 
237
    int hW = W / 2;
 
238
    g.drawLine(xa * W + hW, ya * W + hW, xb * W + hW, yb * W + hW);
 
239
  }
 
240
 
 
241
  /**
 
242
   * Check for the presence of the given point in the collection.
 
243
   */
 
244
  public final boolean pointPresent(int x, int y, Collection in)
 
245
  {
 
246
    Iterator iter = in.iterator();
 
247
    Point p;
 
248
    while (iter.hasNext())
 
249
      {
 
250
        p = (Point) iter.next();
 
251
        if (p.x == x && p.y == y)
 
252
          return true;
 
253
      }
 
254
    return false;
 
255
  }
 
256
 
 
257
  public void drawGrid(int w, int h, Graphics g)
 
258
  {
 
259
    g.setColor(Color.lightGray);
 
260
 
 
261
    // Draw vertical lines:
 
262
    for (int x = 0; x < w; x += W)
 
263
      {
 
264
        g.drawLine(x, 0, x, h);
 
265
      }
 
266
 
 
267
    // Draw horizontal lines:
 
268
    for (int y = 0; y < h; y += W)
 
269
      {
 
270
        g.drawLine(0, y, w, y);
 
271
      }
 
272
      
 
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);    
 
276
  }
 
277
 
 
278
  public void drawFigures(Graphics g)
 
279
  {
 
280
    g.setColor(Color.red);
 
281
    drawDots(reds, g, RED);
 
282
 
 
283
    g.setColor(Color.black);
 
284
    drawDots(blacks, g, BLACK);
 
285
 
 
286
    g.setColor(Color.lightGray);
 
287
    drawDots(hints, g, HINT);
 
288
 
 
289
    if (endOfGame != null)
 
290
      drawFinishLine(endOfGame[0].x, endOfGame[0].y, endOfGame[1].x,
 
291
        endOfGame[1].y, g);
 
292
  }
 
293
 
 
294
  public Point makePoint(int x, int y)
 
295
  {
 
296
    return new Point(x / W, y / W);
 
297
  }
 
298
 
 
299
  /**
 
300
   * Draw a collection of dots (the collor must be set before calling the
 
301
   * method).
 
302
   */
 
303
  public void drawDots(Collection dots, Graphics g, int mode)
 
304
  {
 
305
    Iterator iter = dots.iterator();
 
306
    int x;
 
307
    int y;
 
308
 
 
309
    int hW = W / 2;
 
310
    int RR = R * 2;
 
311
    int hR = R / 2;
 
312
    Point p;
 
313
    while (iter.hasNext())
 
314
      {
 
315
        p = (Point) iter.next();
 
316
        x = p.x * W + hW;
 
317
        y = p.y * W + hW;
 
318
 
 
319
        if (mode == RED)
 
320
          g.drawOval(x - R, y - R, RR, RR);
 
321
        else if (mode == BLACK)
 
322
          {
 
323
            g.drawLine(x - R, y - R, x + R, y + R);
 
324
            g.drawLine(x - R, y + R, x + R, y - R);
 
325
          }
 
326
        else
 
327
          {
 
328
            // Hint.
 
329
            g.drawOval(x - hR, y - hR, R, R);
 
330
          }
 
331
      }
 
332
  }
 
333
 
 
334
  private void jbInit()
 
335
    throws Exception
 
336
  {
 
337
    addMouseListener(this);
 
338
  }
 
339
 
 
340
  public void mouseClicked(MouseEvent e)
 
341
  {
 
342
    try
 
343
      {
 
344
        int state = player.get_current_state();
 
345
 
 
346
        // Check if the state is correct.
 
347
        if (state == I_WAIT_FOR_YOUR_MOVE)
 
348
          {
 
349
            frame.talk(Color.black,
 
350
              "It is now time for our partner's move, not ours. Please wait.");
 
351
          }
 
352
        else if (state == DISCONNECTED)
 
353
          {
 
354
            frame.talk(Color.black,
 
355
              "We are not connected to the playing partner yet.");
 
356
          }
 
357
        else if (state == I_HAVE_LOST)
 
358
          {
 
359
            frame.talk(Color.black,
 
360
              "We have already lost this battle, but why not to try again?");
 
361
          }
 
362
        else if (state == I_HAVE_WON)
 
363
          {
 
364
            frame.talk(Color.black,
 
365
              "The victory is ours, nothing more to do here.");
 
366
          }
 
367
        else if (player.partner == null)
 
368
          frame.talk(Color.black, "No other player so far.");
 
369
        else
 
370
          {
 
371
            int x = e.getX();
 
372
            int y = e.getY();
 
373
 
 
374
            if (x>frame.DESK_SIZE.width ||
 
375
                y>frame.DESK_SIZE.height)
 
376
            {
 
377
              frame.talk(Color.black,"Outside the game area.");
 
378
              return;
 
379
            }
 
380
 
 
381
            Point p = makePoint(x, y);
 
382
 
 
383
            // Ignore clicks on the occupied cells.
 
384
            if (pointPresent(p.x, p.y, reds)
 
385
              || (pointPresent(p.x, p.y, blacks)))
 
386
              {
 
387
                frame.talk(Color.black,
 
388
                  "This is against the rules, select the unoccupied cell.");
 
389
                return;
 
390
              }
 
391
 
 
392
            reds.add(p);
 
393
 
 
394
            endOfGame = checkFinished(reds, p);
 
395
            repaint();
 
396
 
 
397
            if (endOfGame != null)
 
398
              {
 
399
                frame.talk(Color.red, "Our move " + p.x + "-" + p.y
 
400
                  + " and we win!");
 
401
                player.set_current_state(I_HAVE_WON);
 
402
              }
 
403
            else
 
404
              {
 
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);
 
408
              }
 
409
 
 
410
            player.partner.receive_move(p.x, p.y, endOfGame);
 
411
          }
 
412
      }
 
413
    catch (RemoteException ex)
 
414
      {
 
415
        // We will print the exception because this is a demo application
 
416
        // that may be modified for learning purposes.
 
417
        ex.printStackTrace();
 
418
      }
 
419
  }
 
420
 
 
421
  /**
 
422
   * Handle the move of the other playing side.
 
423
   */
 
424
  public void friendsMove(int x, int y, Point[] victory)
 
425
  {
 
426
    try
 
427
      {
 
428
        int state = player.get_current_state();
 
429
        if (state != I_WAIT_FOR_YOUR_MOVE || pointPresent(x, y, blacks))
 
430
          {
 
431
            stateFailed("Move " + x + "-" + y);
 
432
          }
 
433
        else
 
434
          {
 
435
            blacks.add(new Point(x, y));
 
436
 
 
437
            if (victory != null)
 
438
              {
 
439
                frame.talk(Color.red,
 
440
                  " We have lost this time, unfortunately..");
 
441
                player.set_current_state(I_HAVE_LOST);
 
442
                endOfGame = victory;
 
443
              }
 
444
            else
 
445
              {
 
446
                frame.talk(Color.black, "Partner goes " + x + "-" + y
 
447
                  + ". Your move?");
 
448
                player.set_current_state(I_THINK);
 
449
              }            
 
450
            repaint();            
 
451
          }
 
452
      }
 
453
    catch (RemoteException rex)
 
454
      {
 
455
        rex.printStackTrace();
 
456
      }
 
457
  }
 
458
 
 
459
  /**
 
460
   * Prepare for the new game.
 
461
   */
 
462
  public void reset()
 
463
  {
 
464
    blacks.clear();
 
465
    reds.clear();
 
466
    hints.clear();
 
467
    endOfGame = null;
 
468
    repaint();
 
469
  }
 
470
 
 
471
  public void mouseEntered(MouseEvent m)
 
472
  {
 
473
    // Nothing to do.
 
474
  }
 
475
 
 
476
  public void mousePressed(MouseEvent m)
 
477
  {
 
478
    // Nothing to do.
 
479
  }
 
480
 
 
481
  public void mouseReleased(MouseEvent m)
 
482
  {
 
483
    // Nothing to do.
 
484
  }
 
485
 
 
486
  public void mouseExited(MouseEvent m)
 
487
  {
 
488
    // Nothing to do.
 
489
  }
 
490
 
 
491
  /**
 
492
   * The systems detected the error conditions. The game cannot continue (the
 
493
   * chat is still possible).
 
494
   */
 
495
  public void stateFailed(String reason)
 
496
  {
 
497
    try
 
498
      {
 
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);
 
504
      }
 
505
    catch (RemoteException ex)
 
506
      {
 
507
        // We will print the exception because this is a demo application
 
508
        // that may be modified for learning purposes.
 
509
        ex.printStackTrace();
 
510
      }
 
511
  }
 
512
}
 
 
b'\\ No newline at end of file'