home *** CD-ROM | disk | FTP | other *** search
/ BUG 15 / BUGCD1998_06.ISO / aplic / jbuilder / jsamples.z / WannaPlay.java < prev    next >
Encoding:
Java Source  |  1997-07-03  |  5.0 KB  |  157 lines

  1. package borland.samples.apps.chess.client ;
  2.  
  3. import java.awt.*;
  4. import java.util.*;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.WindowEvent;
  7. import java.awt.event.ActionListener;
  8. /**Displays the Dialog when a player has been challenged for a game
  9. * initiates a message to the server with the players response
  10. */
  11. public class WannaPlay extends Dialog implements OkCancelDialog {
  12.   Panel dlgPanel = new Panel();
  13.   Button yes = new Button();
  14.   Button no = new Button();
  15.   String pieceColor;
  16.   String opponent;
  17.   String opponentRating;
  18.   String myRating;
  19.   int gameTime;
  20.   int moveTime;
  21.   String[] parm;
  22.   Label timeControlLabel = new Label();
  23.   GridLayout dialogLayout = new GridLayout();
  24.   Panel ButtonBar = new Panel();
  25.   Label text1 = new Label();
  26.   Label text2 = new Label();
  27.   String timeControl;
  28.   Panel panel1 = new Panel();
  29.   GridLayout gridLayout1 = new GridLayout();
  30.   ChessViewer dlgParent;
  31.   /**Constructor
  32.   *param Frame parent The Frame of this dialogs parent
  33.   *param ChessViewer theapp
  34.   *param String[] parm the parsed message data from the Challenge message
  35.   */
  36.   public WannaPlay(Frame parent,ChessViewer theapp,String [] parm) {
  37.     super(parent,CVS.FRESH_MEAT_,false);
  38.     enableEvents(AWTEvent.WINDOW_EVENT_MASK);
  39.     if (parm.length < 6)
  40.       System.out.println("WannaPlay expected six parameters, found " + parm.length);
  41.     dlgParent =  theapp;
  42.     opponent = parm[2];
  43.     opponentRating = parm[3];
  44.     myRating = parm[5];
  45.     this.parm = parm;
  46.     try {
  47.       gameTime = Integer.parseInt(parm[0]);
  48.       moveTime = Integer.parseInt(parm[1]);
  49.     }
  50.     catch (NumberFormatException e) {}
  51.     timeControl = CVS.Insert(CVS.TIME_CONTROL,parm[0],parm[1]);
  52.     jbInit();
  53.     add(dlgPanel);
  54.     timeControlLabel.setText(timeControl);
  55.     Random color = new Random();
  56.     if (opponent.equals(dlgParent.opponentName))
  57.       if (opponent.equals(dlgParent.game.getPlayerW()))
  58.         pieceColor = "white";
  59.       else
  60.         pieceColor = "black";
  61.     else
  62.       if (color.nextInt() > 0)
  63.         pieceColor = "white";
  64.       else
  65.         pieceColor = "black";
  66.  
  67.     if (pieceColor.equals("white"))  {
  68.       dlgParent.yourColor = ChessRules.White;
  69.       text2.setText(CVS.YOU_GET_WHITE);
  70.     }
  71.     else {
  72.       dlgParent.yourColor = ChessRules.Black;
  73.       text2.setText(CVS.YOU_GET_BLACK);
  74.     }
  75.     text1.setText(CVS.Insert(CVS.DO_YOU_WANT_TO_PLAY,opponent));
  76.  
  77.  
  78.   }
  79.  
  80.   public void jbInit() {
  81.     dialogLayout.setRows(4);
  82.     dialogLayout.setColumns(1);
  83.     gridLayout1.setHgap(6);
  84.     ButtonBar.setLayout(gridLayout1);
  85.     dlgPanel.setLayout(dialogLayout);
  86.     dlgPanel.add(text1);
  87.     dlgPanel.add(text2);
  88.     timeControlLabel.setText(CVS.TIME_CONTROL);
  89.     text2.setText(CVS.YOU_GET_BLACK);
  90.     text1.setText(CVS.DO_YOU_WANT_TO_PLAY );
  91.     dlgPanel.add(timeControlLabel);
  92.     dlgPanel.add(panel1, null);
  93.     panel1.add(ButtonBar);
  94.     ButtonBar.add(yes);
  95.     ButtonBar.add(no);
  96.     yes.setLabel(CVS.YES);
  97.     yes.addActionListener(new OkButtonListener(this));
  98.     no.setLabel(CVS.NO);
  99.     no.addActionListener(new CancelButtonListener(this));
  100.   }
  101.   /**So System Close acts like pressing No
  102.   */
  103.   protected void processWindowEvent(WindowEvent e)  {
  104.     if (e.getID() == WindowEvent.WINDOW_CLOSING)
  105.       cancel(null);
  106.     else
  107.       super.processWindowEvent(e);
  108.   }
  109.  
  110.   /**the OkCancelDialog method that the OkButtonListener calls when its receives an action
  111.   */
  112.   public void ok(ActionEvent evt) {
  113.     dlgParent.gameTimeValue = gameTime;
  114.     dlgParent.playerTime[0] = dlgParent.gameTimeValue * 60000;
  115.     dlgParent.playerTime[1] = dlgParent.playerTime[0];
  116.     dlgParent.lMoveTime = moveTime * 1000;
  117.     parm[0] = pieceColor;
  118.     dlgParent.sendMsg("GameAccept",parm);
  119.     dlgParent.opponentName = opponent;
  120.     if (dlgParent.yourColor == ChessRules.White) {
  121.       dlgParent.newGame(dlgParent.myName,opponent);
  122.       dlgParent.statusLine.setText(CVS.YOUR_MOVE);
  123.       dlgParent.game.setWhiteElo(myRating);
  124.       dlgParent.game.setBlackElo(opponentRating);
  125.     }
  126.     else {
  127.       dlgParent.newGame(opponent,dlgParent.myName);
  128.       dlgParent.statusLine.setText(CVS.Insert(CVS.WAITING_FOR,dlgParent.game.getPlayerW()) );
  129.       dlgParent.game.setWhiteElo(opponentRating);
  130.       dlgParent.game.setBlackElo(myRating);
  131.     }
  132.     dlgParent.game.setSite(CVS.BORLAND_CHESS_CLUB);
  133.     dlgParent.game.setRound("-");
  134.     dlgParent.game.setEvent(CVS.RATED_GAME);
  135.     GregorianCalendar today = new GregorianCalendar();
  136.     dlgParent.game.setDate(String.valueOf(today.get(Calendar.YEAR))+ "." +
  137.                            String.valueOf(today.get(Calendar.MONTH)+1) + "." +
  138.                            String.valueOf(today.get(Calendar.DAY_OF_MONTH)));
  139.     dispose();
  140.  
  141.   }
  142.  
  143.   /**The OkCancelInterface method that the CancelButtonListener calls when its receives an action
  144.   */
  145.   public void cancel(ActionEvent evt) {
  146.     dlgParent.sendMsg("RefuseChallenge",pieceColor) ;
  147.     dlgParent.opponentName = null;
  148.     //  synchronized (dlgParent){
  149.     //    dlgParent.notify();
  150.  
  151.     // }
  152.     dispose();
  153.   }
  154. }
  155.  
  156.  
  157.