-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTossSticksPanel.java
189 lines (169 loc) · 5.81 KB
/
TossSticksPanel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class TossSticksPanel
{
private JFrame frame;
private JPanel mainPanel, panel1, panel2;
private JButton toss;
private JLabel tossText;
private JButton endTurn;
private JLabel endTurnText;
private JLabel teamText;
private int numberOfTeams;
private int currentTeam;
private String [] teamNames;
private JLabel totalText, image;
private JLabel displayTotal;
private int total;
public TossSticksPanel( int numTeams )
{
// numberOfTeams = numTeams + 1;
// setUpTeams( numberOfTeams );
teamText = new JLabel();
teamText.setText( "Team " + teamNames[ currentTeam ] );
teamText.setHorizontalAlignment( 0 );
setUpTossButton();
image = new JLabel();
// http://stackoverflow.com/questions/20886415/displaying-image-in-jpanel-from-netbeans-gui-builder
image.setIcon( new javax.swing.ImageIcon( getClass().getResource( "/resources/Yut Sticks.png" ) ) );
image.setHorizontalAlignment( 0 );
panel1 = new JPanel();
panel1.add( image );
panel1.setSize( 300, 400 );
totalText = new JLabel();
totalText.setText( "TOTAL: " + total );
totalText.setHorizontalAlignment( 0 );
setUpEndTurnButton();
//set up layout
mainPanel = new JPanel();
mainPanel.setLayout( new GridLayout( 5, 1 ) );
// Borderlayout gridbaglayout
// mainPanel.setLayout( new GridBagLayout() );
mainPanel.add( teamText );
mainPanel.add( toss );
mainPanel.add( panel1 ); //Sticks go here
mainPanel.add( totalText );
mainPanel.add( endTurn );
frame = new JFrame();
frame.setTitle( "Yunnori Sticks" );
frame.setSize( 350, 450 );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.add( mainPanel );
frame.setVisible( true );
}
public int getTotal()
{
return total;
}
public int getCurrentTeam()
{
return currentTeam;
}
// public String[] getTeamNames()
// {
// return teamNames;
// }
public void nextTeam()
{
if( currentTeam + 1 == numberOfTeams )
currentTeam = 0;
else
currentTeam++;
teamText.setText( "Team " + teamNames[ currentTeam ] );
}
// public void setUpTeams( int numberOfTeams )
// {
// teamNames = new String[ numberOfTeams ];
// for( int i = 0; i < numberOfTeams; i++ )
// {
// if( i == 0 )
// teamNames[ i ] = "A";
// if( i == 1 )
// teamNames[ i ] = "B";
// if( i == 2 )
// teamNames[ i ] = "C";
// if( i == 3 )
// teamNames[ i ] = "D";
// }
// }
public void setUpTossButton()
{
toss = new JButton( "Toss" );
class ButtonListener implements ActionListener
{
public void actionPerformed( ActionEvent event )
{
Stick one = new Stick();
Stick two = new Stick();
Stick three = new Stick();
Stick four = new Stick();
total = calculateTotal( one, two, three, four );
// displayTotal.setText( total + "" );
if( total == 1 )
{
image.setIcon( new javax.swing.ImageIcon( getClass().getResource( "/resources/Do.jpg" ) ) );
total = 1;
}
else if( total == 2 )
{
image.setIcon( new javax.swing.ImageIcon( getClass().getResource( "/resources/Gae.jpg" ) ) );
total = 2;
}
else if( total == 3 )
{
image.setIcon( new javax.swing.ImageIcon( getClass().getResource( "/resources/Geol.jpg" ) ) );
total = 3;
}
else if( total == 4 )
{
image.setIcon( new javax.swing.ImageIcon( getClass().getResource( "/resources/Geol.jpg" ) ) );
total = 4;
}
else if( total == 5 )
{
image.setIcon( new javax.swing.ImageIcon( getClass().getResource( "/resources/Yut.jpg" ) ) );
total = 5;
}
totalText.setText( "TOTAL: " + total );
}
}
ActionListener listener = new ButtonListener();
toss.addActionListener( listener );
}
public void setUpEndTurnButton()
{
endTurn = new JButton( "End Turn" );
class ButtonListener implements ActionListener
{
public void actionPerformed( ActionEvent event )
{
total = 0;
totalText.setText( "TOTAL: " + total );
nextTeam();
}
}
ActionListener listener = new ButtonListener();
endTurn.addActionListener( listener );
}
public int calculateTotal( Stick a, Stick b, Stick c, Stick d )
{
Stick [] group = new Stick[ 4 ];
group[ 0 ] = a;
group[ 1 ] = b;
group[ 2 ] = c;
group[ 3 ] = d;
int t = 0;
for( int i = 0; i < 4; i++ )
{
if( group[ i ].getFlatSideUp() == true )
t++;
}
if( t == 0 )
return 5;
else
return t;
}
}