View Javadoc

1   package net.sourceforge.simplegamenet.chess;
2   
3   import java.awt.event.ActionEvent;
4   import java.awt.event.ActionListener;
5   import javax.swing.*;
6   import net.sourceforge.simplegamenet.specs.gui.GamePanel;
7   import net.sourceforge.simplegamenet.util.proportionlayout.ProportionConstraints;
8   import net.sourceforge.simplegamenet.util.proportionlayout.ProportionLayout;
9   
10  public class ChessPanel extends GamePanel implements ActionListener {
11  
12      private ChessPlayerClient chessPlayerClient;
13  
14      private ChessPlayFieldComponent chessPlayFieldComponent;
15  
16      private ImageIcon piecesImageIcon;
17      private ImageIcon squaresImageIcon;
18  
19      private JLabel playFieldStateLabel = new JLabel("State:");
20      private JLabel playFieldStateValueLabel = new JLabel("Game not yet started");
21      private JLabel participantAtTurnLabel = new JLabel("At turn:");
22      private JLabel participantAtTurnValueLabel = new JLabel("none");
23      private JLabel lastMoveLabel = new JLabel("Last move:");
24      private JLabel lastMoveValueLabel = new JLabel("none");
25      private JButton offerRemiseButton = new JButton("Offer remise");
26      private JButton yieldButton = new JButton("Yield");
27  
28      public ChessPanel(ChessPlayerClient chessPlayerClient,
29                        JPanel chatClientPanel) {
30          super();
31          this.chessPlayerClient = chessPlayerClient;
32          piecesImageIcon = new ImageIcon(getClass().getResource("Pieces.png"));
33          squaresImageIcon = new ImageIcon(getClass().getResource("Squares.png"));
34  
35          ProportionLayout layout = new ProportionLayout();
36          layout.appendColumn(0, ProportionLayout.NO_PROPORTION); // 0
37          layout.appendColumn(10);                                // 1 empty
38          layout.appendColumn(0, ProportionLayout.NO_PROPORTION); // 2
39          layout.appendColumn(10);                                // 3 empty
40          layout.appendColumn(0, 1.0);                            // 4
41          layout.appendRow(0, ProportionLayout.NO_PROPORTION); // 0
42          layout.appendRow(10);                                // 1 empty
43          layout.appendRow(0, ProportionLayout.NO_PROPORTION); // 2
44          layout.appendRow(10);                                // 3 empty
45          layout.appendRow(0, ProportionLayout.NO_PROPORTION); // 4
46          layout.appendRow(10);                                // 5 empty
47          layout.appendRow(0, ProportionLayout.NO_PROPORTION); // 6
48          layout.appendRow(10);                                // 7 empty
49          layout.appendRow(0, ProportionLayout.NO_PROPORTION); // 8
50          layout.appendRow(10);                                // 9 empty
51          layout.appendRow(0, 1.0);                            // 10 empty
52          setLayout(layout);
53  
54          chessPlayFieldComponent = new ChessPlayFieldComponent(chessPlayerClient, this,
55                  piecesImageIcon.getImage(),
56                  squaresImageIcon.getImage());
57          setEnabled(false);
58          add(chessPlayFieldComponent,
59                  new ProportionConstraints(0, 0, 1, 11, 0, 10, ProportionConstraints.CENTER,
60                          ProportionConstraints.HORIZONTAL));
61          add(playFieldStateLabel, new ProportionConstraints(2, 0));
62          add(playFieldStateValueLabel, new ProportionConstraints(4, 0));
63          add(participantAtTurnLabel, new ProportionConstraints(2, 2));
64          add(participantAtTurnValueLabel, new ProportionConstraints(4, 2));
65          add(lastMoveLabel, new ProportionConstraints(2, 4));
66          add(lastMoveValueLabel, new ProportionConstraints(4, 4));
67          offerRemiseButton.addActionListener(this);
68          add(offerRemiseButton, new ProportionConstraints(2, 6, 3, 1, 4, 6));
69          yieldButton.addActionListener(this);
70          add(yieldButton, new ProportionConstraints(2, 8, 3, 1, 4, 8));
71          add(chatClientPanel, new ProportionConstraints(2, 10, 3, 1, 4, 10));
72      }
73  
74      public void setPieceOwnerSight(int pieceOwnerSight) {
75          chessPlayFieldComponent.setPieceOwnerSight(pieceOwnerSight);
76      }
77  
78      public void handleTurn() {
79          setEnabled(true);
80          participantAtTurnValueLabel.setText("you");
81      }
82  
83      public void remiseOffered() {
84          //-
85      }
86  
87      public boolean getRemiseOfferAnswer() {
88          return JOptionPane.showConfirmDialog(getTopLevelAncestor(),
89                  "Do you want to accept remise?", "Remise offered",
90                  JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE)
91                  == JOptionPane.YES_OPTION;
92      }
93  
94      public void setRemiseOfferAnswer(boolean remiseOfferAccepted) {
95          if (remiseOfferAccepted) {
96              JOptionPane.showMessageDialog(getTopLevelAncestor(),
97                      "Your remise offer has been accepted.",
98                      "Remise offer", JOptionPane.ERROR_MESSAGE);
99          } else {
100             JOptionPane.showMessageDialog(getTopLevelAncestor(),
101                     "Your remise offer has been declined.",
102                     "Remise offer", JOptionPane.ERROR_MESSAGE);
103         }
104     }
105 
106     public void setEnabled(boolean enabled) {
107         super.setEnabled(enabled);
108         chessPlayFieldComponent.setEnabled(enabled);
109         offerRemiseButton.setEnabled(enabled);
110         yieldButton.setEnabled(enabled);
111     }
112 
113     public void makeMove(int sourceX, int sourceY, int destinationX, int destinationY) {
114         if (chessPlayerClient.isReplaceChoiceNeeded(sourceX, sourceY,
115                 destinationX, destinationY)) {
116             Object[] possibleChoices = {"Queen", "Bishop", "Knight", "Rook"};
117             String choice = (String) JOptionPane.showInputDialog(getTopLevelAncestor(),
118                     "Select a piece to replace the pawn:",
119                     "Select a piece",
120                     JOptionPane.PLAIN_MESSAGE,
121                     null,
122                     possibleChoices,
123                     "Queen");
124             int chosenPieceType = ChessPieceType.QUEEN;
125             if (choice == null) {
126                 return;
127             } else if (choice.equals("Queen")) {
128                 chosenPieceType = ChessPieceType.QUEEN;
129             } else if (choice.equals("Bishop")) {
130                 chosenPieceType = ChessPieceType.BISHOP;
131             } else if (choice.equals("Knight")) {
132                 chosenPieceType = ChessPieceType.KNIGHT;
133             } else if (choice.equals("Rook")) {
134                 chosenPieceType = ChessPieceType.ROOK;
135             } else {
136                 return;
137             }
138             chessPlayerClient.makeMove(sourceX, sourceY, destinationX, destinationY,
139                     chosenPieceType);
140         } else {
141             chessPlayerClient.makeMove(sourceX, sourceY, destinationX, destinationY);
142         }
143         setEnabled(false);
144     }
145 
146     public void actionPerformed(ActionEvent actionEvent) {
147         if (actionEvent.getSource() == offerRemiseButton) {
148             if (chessPlayerClient.isOfferRemiseAllowed()) {
149                 if (JOptionPane.showConfirmDialog(getTopLevelAncestor(),
150                         "Do you want to offer remise?", "Offer remise",
151                         JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE)
152                         == JOptionPane.YES_OPTION) {
153                     chessPlayerClient.offerRemise();
154                     setEnabled(false);
155                 }
156             } else {
157                 JOptionPane.showMessageDialog(getTopLevelAncestor(),
158                         "You can only offer remise once during a turn.",
159                         "Offer remise not possible", JOptionPane.ERROR_MESSAGE);
160             }
161         } else if (actionEvent.getSource() == yieldButton) {
162             if (JOptionPane.showConfirmDialog(getTopLevelAncestor(),
163                     "Do you want to yield this game?", "Yield game",
164                     JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE)
165                     == JOptionPane.YES_OPTION) {
166                 chessPlayerClient.yieldGame();
167                 setEnabled(false);
168             }
169         }
170     }
171 
172     public void setPlayFieldState(int playFieldState) {
173         switch (playFieldState) {
174             case ChessPlayFieldState.NORMAL_STATE:
175                 playFieldStateValueLabel.setText("Normal");
176                 break;
177             case ChessPlayFieldState.CHECK:
178                 playFieldStateValueLabel.setText("Check");
179                 break;
180             case ChessPlayFieldState.CHECKMATE:
181                 playFieldStateValueLabel.setText("Checkmate");
182                 break;
183             case ChessPlayFieldState.STALEMATE:
184                 playFieldStateValueLabel.setText("Remise by stalemate");
185                 break;
186             case ChessPlayFieldState.REMISE:
187                 playFieldStateValueLabel.setText("Remise");
188                 break;
189             case ChessPlayFieldState.YIELDED:
190                 playFieldStateValueLabel.setText("Yielded");
191                 break;
192         }
193     }
194 
195     public void setOtherParticipantAtTurn(String nickname) {
196         participantAtTurnValueLabel.setText(nickname);
197     }
198 
199     public void setWinnerThisPlayer() {
200         setEnabled(false);
201         participantAtTurnValueLabel.setText("you");
202     }
203 
204     public void setWinner(String nickname) {
205         setEnabled(false);
206         participantAtTurnValueLabel.setText(nickname);
207     }
208 
209     public void setNoWinner() {
210         setEnabled(false);
211         participantAtTurnValueLabel.setText("none");
212     }
213 
214     public void setLastMove(int sourceX, int sourceY, int destinationX, int destinationY) {
215         lastMoveValueLabel.setText((char) ('A' + sourceX)
216                 + Integer.toString(ChessGridSize.GRID_HEIGHT - sourceY) + " -> "
217                 + (char) ('A' + destinationX)
218                 + Integer.toString(ChessGridSize.GRID_HEIGHT - destinationY));
219     }
220 
221     public void playFieldUpdated(int[][] pieceTypeGrid, int[][] pieceOwnerGrid) {
222         chessPlayFieldComponent.playFieldUpdated(pieceTypeGrid, pieceOwnerGrid);
223     }
224 
225 }