home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-07-03 | 5.0 KB | 157 lines |
- package borland.samples.apps.chess.client ;
-
- import java.awt.*;
- import java.util.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.WindowEvent;
- import java.awt.event.ActionListener;
- /**Displays the Dialog when a player has been challenged for a game
- * initiates a message to the server with the players response
- */
- public class WannaPlay extends Dialog implements OkCancelDialog {
- Panel dlgPanel = new Panel();
- Button yes = new Button();
- Button no = new Button();
- String pieceColor;
- String opponent;
- String opponentRating;
- String myRating;
- int gameTime;
- int moveTime;
- String[] parm;
- Label timeControlLabel = new Label();
- GridLayout dialogLayout = new GridLayout();
- Panel ButtonBar = new Panel();
- Label text1 = new Label();
- Label text2 = new Label();
- String timeControl;
- Panel panel1 = new Panel();
- GridLayout gridLayout1 = new GridLayout();
- ChessViewer dlgParent;
- /**Constructor
- *param Frame parent The Frame of this dialogs parent
- *param ChessViewer theapp
- *param String[] parm the parsed message data from the Challenge message
- */
- public WannaPlay(Frame parent,ChessViewer theapp,String [] parm) {
- super(parent,CVS.FRESH_MEAT_,false);
- enableEvents(AWTEvent.WINDOW_EVENT_MASK);
- if (parm.length < 6)
- System.out.println("WannaPlay expected six parameters, found " + parm.length);
- dlgParent = theapp;
- opponent = parm[2];
- opponentRating = parm[3];
- myRating = parm[5];
- this.parm = parm;
- try {
- gameTime = Integer.parseInt(parm[0]);
- moveTime = Integer.parseInt(parm[1]);
- }
- catch (NumberFormatException e) {}
- timeControl = CVS.Insert(CVS.TIME_CONTROL,parm[0],parm[1]);
- jbInit();
- add(dlgPanel);
- timeControlLabel.setText(timeControl);
- Random color = new Random();
- if (opponent.equals(dlgParent.opponentName))
- if (opponent.equals(dlgParent.game.getPlayerW()))
- pieceColor = "white";
- else
- pieceColor = "black";
- else
- if (color.nextInt() > 0)
- pieceColor = "white";
- else
- pieceColor = "black";
-
- if (pieceColor.equals("white")) {
- dlgParent.yourColor = ChessRules.White;
- text2.setText(CVS.YOU_GET_WHITE);
- }
- else {
- dlgParent.yourColor = ChessRules.Black;
- text2.setText(CVS.YOU_GET_BLACK);
- }
- text1.setText(CVS.Insert(CVS.DO_YOU_WANT_TO_PLAY,opponent));
-
-
- }
-
- public void jbInit() {
- dialogLayout.setRows(4);
- dialogLayout.setColumns(1);
- gridLayout1.setHgap(6);
- ButtonBar.setLayout(gridLayout1);
- dlgPanel.setLayout(dialogLayout);
- dlgPanel.add(text1);
- dlgPanel.add(text2);
- timeControlLabel.setText(CVS.TIME_CONTROL);
- text2.setText(CVS.YOU_GET_BLACK);
- text1.setText(CVS.DO_YOU_WANT_TO_PLAY );
- dlgPanel.add(timeControlLabel);
- dlgPanel.add(panel1, null);
- panel1.add(ButtonBar);
- ButtonBar.add(yes);
- ButtonBar.add(no);
- yes.setLabel(CVS.YES);
- yes.addActionListener(new OkButtonListener(this));
- no.setLabel(CVS.NO);
- no.addActionListener(new CancelButtonListener(this));
- }
- /**So System Close acts like pressing No
- */
- protected void processWindowEvent(WindowEvent e) {
- if (e.getID() == WindowEvent.WINDOW_CLOSING)
- cancel(null);
- else
- super.processWindowEvent(e);
- }
-
- /**the OkCancelDialog method that the OkButtonListener calls when its receives an action
- */
- public void ok(ActionEvent evt) {
- dlgParent.gameTimeValue = gameTime;
- dlgParent.playerTime[0] = dlgParent.gameTimeValue * 60000;
- dlgParent.playerTime[1] = dlgParent.playerTime[0];
- dlgParent.lMoveTime = moveTime * 1000;
- parm[0] = pieceColor;
- dlgParent.sendMsg("GameAccept",parm);
- dlgParent.opponentName = opponent;
- if (dlgParent.yourColor == ChessRules.White) {
- dlgParent.newGame(dlgParent.myName,opponent);
- dlgParent.statusLine.setText(CVS.YOUR_MOVE);
- dlgParent.game.setWhiteElo(myRating);
- dlgParent.game.setBlackElo(opponentRating);
- }
- else {
- dlgParent.newGame(opponent,dlgParent.myName);
- dlgParent.statusLine.setText(CVS.Insert(CVS.WAITING_FOR,dlgParent.game.getPlayerW()) );
- dlgParent.game.setWhiteElo(opponentRating);
- dlgParent.game.setBlackElo(myRating);
- }
- dlgParent.game.setSite(CVS.BORLAND_CHESS_CLUB);
- dlgParent.game.setRound("-");
- dlgParent.game.setEvent(CVS.RATED_GAME);
- GregorianCalendar today = new GregorianCalendar();
- dlgParent.game.setDate(String.valueOf(today.get(Calendar.YEAR))+ "." +
- String.valueOf(today.get(Calendar.MONTH)+1) + "." +
- String.valueOf(today.get(Calendar.DAY_OF_MONTH)));
- dispose();
-
- }
-
- /**The OkCancelInterface method that the CancelButtonListener calls when its receives an action
- */
- public void cancel(ActionEvent evt) {
- dlgParent.sendMsg("RefuseChallenge",pieceColor) ;
- dlgParent.opponentName = null;
- // synchronized (dlgParent){
- // dlgParent.notify();
-
- // }
- dispose();
- }
- }
-
-
-