home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 October A / Pcwk10a98.iso / Inprise / TRIAL / JBUILDER / JSAMPLES.Z / WannaPlay.java < prev    next >
Text File  |  1998-05-08  |  6KB  |  179 lines

  1. /*
  2.  * Copyright (c) 1997-1998 Borland International, Inc. All Rights Reserved.
  3.  * 
  4.  * This SOURCE CODE FILE, which has been provided by Borland as part
  5.  * of a Borland product for use ONLY by licensed users of the product,
  6.  * includes CONFIDENTIAL and PROPRIETARY information of Borland.  
  7.  *
  8.  * USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS 
  9.  * OF THE LICENSE STATEMENT AND LIMITED WARRANTY FURNISHED WITH
  10.  * THE PRODUCT.
  11.  *
  12.  * IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD BORLAND, ITS RELATED
  13.  * COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY CLAIMS
  14.  * OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR DISTRIBUTION
  15.  * OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES ARISING OUT OF
  16.  * OR RESULTING FROM THE USE, MODIFICATION, OR DISTRIBUTION OF PROGRAMS
  17.  * OR FILES CREATED FROM, BASED ON, AND/OR DERIVED FROM THIS SOURCE
  18.  * CODE FILE.
  19.  */
  20. package borland.samples.apps.chess.client ;
  21.  
  22. import java.awt.*;
  23. import java.util.*;
  24. import java.awt.event.ActionEvent;
  25. import java.awt.event.WindowEvent;
  26. import java.awt.event.ActionListener;
  27. /**Displays the Dialog when a player has been challenged for a game
  28. * initiates a message to the server with the players response
  29. */
  30. public class WannaPlay extends Dialog implements OkCancelDialog
  31. {
  32.   ResourceBundle res = ResourceBundle.getBundle("borland.samples.apps.chess.client.Res");
  33.   Panel dlgPanel = new Panel();
  34.   Button yes = new Button();
  35.   Button no = new Button();
  36.   String pieceColor;
  37.   String opponent;
  38.   String opponentRating;
  39.   String myRating;
  40.   int gameTime;
  41.   int moveTime;
  42.   String[] parm;
  43.   Label timeControlLabel = new Label();
  44.   GridLayout dialogLayout = new GridLayout();
  45.   Panel buttonBar = new Panel();
  46.   Label text1 = new Label();
  47.   Label text2 = new Label();
  48.   String timeControl;
  49.   Panel panel1 = new Panel();
  50.   GridLayout gridLayout1 = new GridLayout();
  51.   ChessViewer dlgParent;
  52.   /**Constructor
  53.   *param Frame parent The Frame of this dialogs parent
  54.   *param ChessViewer theapp
  55.   *param String[] parm the parsed message data from the Challenge message
  56.   */
  57.   public WannaPlay(Frame parent,ChessViewer theapp,String [] parm) {
  58.     super(parent,"",false);
  59.     setTitle(res.getString("FRESH_MEAT_"));
  60.     enableEvents(AWTEvent.WINDOW_EVENT_MASK);
  61.     if (parm.length < 6)
  62.       System.out.println("WannaPlay expected six parameters, found " + parm.length);
  63.     dlgParent =  theapp;
  64.     opponent = parm[2];
  65.     opponentRating = parm[3];
  66.     myRating = parm[5];
  67.     this.parm = parm;
  68.     try {
  69.       gameTime = Integer.parseInt(parm[0]);
  70.       moveTime = Integer.parseInt(parm[1]);
  71.     }
  72.     catch (NumberFormatException e) {}
  73.     timeControl = CVS.Insert(res.getString("TIME_CONTROL"),parm[0],parm[1]);
  74.     jbInit();
  75.     add(dlgPanel);
  76.     timeControlLabel.setText(timeControl);
  77.     Random color = new Random();
  78.     if (opponent.equals(dlgParent.opponentName))
  79.       if (opponent.equals(dlgParent.game.getPlayerW()))
  80.         pieceColor = "white";
  81.       else
  82.         pieceColor = "black";
  83.     else
  84.       if (color.nextInt() > 0)
  85.         pieceColor = "white";
  86.       else
  87.         pieceColor = "black";
  88.  
  89.     if (pieceColor.equals("white"))  {
  90.       dlgParent.yourColor = ChessRules.White;
  91.       text2.setText(res.getString("YOU_GET_WHITE"));
  92.     }
  93.     else {
  94.       dlgParent.yourColor = ChessRules.Black;
  95.       text2.setText(res.getString("YOU_GET_BLACK"));
  96.     }
  97.     text1.setText(CVS.Insert(res.getString("DO_YOU_WANT_TO_PLAY"),opponent));
  98.  
  99.  
  100.   }
  101.  
  102.   private void jbInit() {
  103.     dialogLayout.setRows(4);
  104.     dialogLayout.setColumns(1);
  105.     gridLayout1.setHgap(6);
  106.     buttonBar.setLayout(gridLayout1);
  107.     dlgPanel.setLayout(dialogLayout);
  108.     dlgPanel.add(text1);
  109.     dlgPanel.add(text2);
  110.     timeControlLabel.setText(res.getString("TIME_CONTROL"));
  111.     text2.setText(res.getString("YOU_GET_BLACK"));
  112.     text1.setText(res.getString("DO_YOU_WANT_TO_PLAY") );
  113.     dlgPanel.add(timeControlLabel);
  114.     dlgPanel.add(panel1, null);
  115.     panel1.add(buttonBar);
  116.     buttonBar.add(yes);
  117.     buttonBar.add(no);
  118.     yes.setLabel(res.getString("YES"));
  119.     yes.addActionListener(new OkButtonListener(this));
  120.     no.setLabel(res.getString("NO"));
  121.     no.addActionListener(new CancelButtonListener(this));
  122.   }
  123.   /**So System Close acts like pressing No
  124.   */
  125.   protected void processWindowEvent(WindowEvent e)  {
  126.     if (e.getID() == WindowEvent.WINDOW_CLOSING)
  127.       cancel(null);
  128.     else
  129.       super.processWindowEvent(e);
  130.   }
  131.  
  132.   /**the OkCancelDialog method that the OkButtonListener calls when its receives an action
  133.   */
  134.   public void ok(ActionEvent evt) {
  135.     dlgParent.gameTimeValue = gameTime;
  136.     dlgParent.playerTime[0] = dlgParent.gameTimeValue * 60000;
  137.     dlgParent.playerTime[1] = dlgParent.playerTime[0];
  138.     dlgParent.lMoveTime = moveTime * 1000;
  139.     parm[0] = pieceColor;
  140.     dlgParent.sendMsg("GameAccept",parm);
  141.     dlgParent.opponentName = opponent;
  142.     if (dlgParent.yourColor == ChessRules.White) {
  143.       dlgParent.newGame(dlgParent.myName,opponent);
  144.       dlgParent.statusLine.setText(res.getString("YOUR_MOVE"));
  145.       dlgParent.game.setWhiteElo(myRating);
  146.       dlgParent.game.setBlackElo(opponentRating);
  147.     }
  148.     else {
  149.       dlgParent.newGame(opponent,dlgParent.myName);
  150.       dlgParent.statusLine.setText(CVS.Insert(res.getString("WAITING_FOR"),dlgParent.game.getPlayerW()) );
  151.       dlgParent.game.setWhiteElo(opponentRating);
  152.       dlgParent.game.setBlackElo(myRating);
  153.     }
  154.     dlgParent.game.setSite(res.getString("BORLAND_CHESS_CLUB"));
  155.     dlgParent.game.setRound("-");
  156.     dlgParent.game.setEvent(res.getString("RATED_GAME"));
  157.     GregorianCalendar today = new GregorianCalendar();
  158.     dlgParent.game.setDate(String.valueOf(today.get(Calendar.YEAR))+ "." +
  159.                            String.valueOf(today.get(Calendar.MONTH)+1) + "." +
  160.                            String.valueOf(today.get(Calendar.DAY_OF_MONTH)));
  161.     dispose();
  162.  
  163.   }
  164.  
  165.   /**The OkCancelInterface method that the CancelButtonListener calls when its receives an action
  166.   */
  167.   public void cancel(ActionEvent evt) {
  168.     dlgParent.sendMsg("RefuseChallenge",pieceColor) ;
  169.     dlgParent.opponentName = null;
  170.     //  synchronized (dlgParent){
  171.     //    dlgParent.notify();
  172.  
  173.     // }
  174.     dispose();
  175.   }
  176. }
  177.  
  178.  
  179.