View Javadoc

1   package net.sourceforge.simplegamenet.dice;
2   
3   import java.awt.event.ActionEvent;
4   import java.awt.event.ActionListener;
5   import javax.swing.*;
6   import net.sourceforge.simplegamenet.util.proportionlayout.ProportionConstraints;
7   import net.sourceforge.simplegamenet.util.proportionlayout.ProportionLayout;
8   
9   public class DicePlayPanel extends JPanel implements ActionListener, DicePacketCodes {
10  
11      private DicePlayerClient dicePlayerClient;
12      private DicePlayDiceComponent[] dice = new DicePlayDiceComponent[5];
13      private JButton rollButton = new JButton("ROLL!");
14      private JLabel timesRolledLabel = new JLabel("", (int) CENTER_ALIGNMENT);
15      private int timesRolled = -1;
16  
17      public DicePlayPanel(DicePlayerClient dicePlayerClient) {
18          this.dicePlayerClient = dicePlayerClient;
19          ProportionLayout layout = new ProportionLayout();
20          layout.appendColumn(10, 1.0);                           // 0
21          layout.appendColumn(50, ProportionLayout.NO_PROPORTION);// 1
22          layout.appendColumn(10, 1.0);                           // 2
23          layout.appendColumn(50, ProportionLayout.NO_PROPORTION);// 3
24          layout.appendColumn(10, 1.0);                           // 4
25          layout.appendColumn(50, ProportionLayout.NO_PROPORTION);// 5
26          layout.appendColumn(10, 1.0);                           // 6
27          layout.appendColumn(50, ProportionLayout.NO_PROPORTION);// 7
28          layout.appendColumn(10, 1.0);                           // 8
29          layout.appendColumn(50, ProportionLayout.NO_PROPORTION);// 9
30          layout.appendColumn(10, 1.0);                           // 10
31          layout.appendRow(5, 1.0);
32          layout.appendRow(0);
33          layout.appendRow(50, ProportionLayout.NO_PROPORTION);
34          layout.appendRow(0);
35          layout.appendRow(20, ProportionLayout.NO_PROPORTION);
36          layout.appendRow(10);
37          layout.appendRow(5, 1.0);
38          setLayout(layout);
39  
40          for (int i = 0; i < dice.length; i++) {
41              dice[i] = new DicePlayDiceComponent(this, 0, false);
42              add(dice[i], new ProportionConstraints((i * 2) + 1, 1));
43          }
44          add(timesRolledLabel, new ProportionConstraints(5, 4));
45          add(rollButton, new ProportionConstraints(3, 6, 5, 1));
46          rollButton.addActionListener(this);
47          rollButton.setEnabled(false);
48          boolean[] selectedDice = {false, false, false, false, false};
49          DicePacket dicePacket = new DicePacket(TURN_ROLL_DICE, selectedDice);
50          dicePlayerClient.sendDicePacket(dicePacket);
51      }
52  
53      /***
54       * Invoked when an action occurs.
55       */
56      public void actionPerformed(ActionEvent actionEvent) {
57          boolean[] selectedDice = {false, false, false, false, false};
58          for (int i = 0; i < dice.length; i++) {
59              if (dice[i].getDiceSelected()) {
60                  selectedDice[i] = true;
61              }
62          }
63          DicePacket dicePacket = new DicePacket(TURN_ROLL_DICE, selectedDice);
64          dicePlayerClient.sendDicePacket(dicePacket);
65      }
66  
67      public void diceClicked(DicePlayDiceComponent clickedDice) {
68          for (int i = 0; i < dice.length; i++) {
69              if (dice[i] == clickedDice) {
70                  DicePacket dicePacket = new DicePacket(TURN_HOLD_DICE, new Integer(i));
71                  dicePlayerClient.sendDicePacket(dicePacket);
72              }
73          }
74      }
75  
76      public DicePlayDiceComponent getDice(int diceNumber) {
77          return dice[diceNumber];
78      }
79  
80      public void resetDice() {
81          setDiceUnclicked();
82          boolean[] selectedDice = {false, false, false, false, false};
83          DicePacket dicePacket = new DicePacket(TURN_ROLL_DICE, selectedDice);
84          dicePlayerClient.sendDicePacket(dicePacket);
85      }
86  
87      public void setDiceUnclicked() {
88          for (int i = 0; i < dice.length; i++) {
89              dice[i].resetDice();
90          }
91      }
92  
93      public void setFirstTimesRolled() {
94          timesRolledLabel.setText("2 rolls left.");
95          timesRolled = 0;
96      }
97  
98      public void setTimesRolled() {
99          if (timesRolled > 1) {
100             rollButton.setEnabled(false);
101             timesRolledLabel.setText("No rolls left.");
102             timesRolled = 0;
103         } else if (timesRolled < 1) {
104             timesRolledLabel.setText("2 rolls left.");
105             timesRolled++;
106         } else {
107             timesRolledLabel.setText("1 roll left.");
108             timesRolled++;
109         }
110     }
111 
112     public void setEnabled(boolean enabled) {
113         rollButton.setEnabled(enabled);
114     }
115 
116 }