-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMancalaController.java
More file actions
239 lines (204 loc) · 8.45 KB
/
Copy pathMancalaController.java
File metadata and controls
239 lines (204 loc) · 8.45 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
/**
* The class that represents the Controller of the MVC design pattern. Manages interaction between the Model and the View.
* @author: Hung Phan
* @author: Lien Cao
* @author: Wendy Ta
* @version: 1.0 11/28/25
*/
public class MancalaController extends JPanel{
private MancalaModel model;
private MancalaView view;
/**
* Constructs a controller of the game
* @param m the model representing the game state
*/
public MancalaController(MancalaModel m) {
this.model = m;
this.view = new MancalaView(model);
model.addChangeListener(view);
setLayout(new BorderLayout());
add(view, BorderLayout.CENTER);
add(leftPanel(), BorderLayout.WEST); // show "MANACALA B"
add(rightPanel(), BorderLayout.EAST); // show "MANACALA A"
add(topPanel(), BorderLayout.NORTH);
add(bottomPanel(), BorderLayout.SOUTH);
}
/**
* Creates the left panel displaying the label "MANCALA B"
* @return the custom panel
*/
private JPanel leftPanel() {
JPanel leftPanel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
Font font = new Font("Arial", Font.BOLD, 30);
g2.setFont(font);
int textX = 50;
int textY = 70;
String[] text = {"M", "A", "N", "C", "A", "L", "A", " ", "B"};
for(int i = 0; i < text.length; i++) {
g2.drawString(text[i], textX, textY + i * 35);
}
}
};
leftPanel.setPreferredSize(new Dimension(120, 350));
return leftPanel;
}
/**
* Creates the right panel displaying the label "MANCALA A"
* @return the custom panel
*/
private JPanel rightPanel() {
JPanel rightPanel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
Font font = new Font("Arial", Font.BOLD, 30);
g2.setFont(font);
int textX = 50;
int textY = 70;
String[] text = {"M", "A", "N", "C", "A", "L", "A", " ", "A"};
for(int i = 0; i < text.length; i++) {
g2.drawString(text[i], textX, textY + i * 35);
}
}
};
rightPanel.setPreferredSize(new Dimension(120, 350));
return rightPanel;
}
/**
* Creates the top panel with an arrow pointing to "PLAYER B".
* @return the custom panel
*/
private JPanel topPanel() {
JPanel topPanel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
int centerX = getWidth()/3; //get the center position of panel
Point2D.Double start1 = new Point2D.Double(centerX + 15,20);
Point2D.Double end1 = new Point2D.Double(centerX + 30, 30);
Shape segment1 = new Line2D.Double(start1, end1);
g2.draw(segment1);
Point2D.Double end2 = new Point2D.Double(centerX+30, 10);
Shape segment2 = new Line2D.Double(start1, end2);
g2.draw(segment2);
Point2D.Double end3 = new Point2D.Double(centerX+60, 20);
Shape segment3 = new Line2D.Double(start1, end3);
g2.draw(segment3);
// Draw the text next to the arrow
String text = "PLAYER B";
Font font = new Font("Arial", Font.BOLD, 30);
g2.setFont(font);
int textX = 70; // start after the arrow
int textY = 30;
g2.drawString(text, centerX + textX, textY);
}
};
topPanel.setPreferredSize(new Dimension(500, 80));
return topPanel;
}
/**
* Creates the top panel with an arrow pointing to "PLAYER A".
* and control buttons
* @return the custom panel
*/
private JPanel bottomPanel() {
JPanel outerPanel = new JPanel();
outerPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
JButton undoButton = new JButton("UNDO");
undoButton.setBackground(Color.GREEN);
undoButton.setOpaque(true);
undoButton.setPreferredSize(new Dimension(80, 40));
JComboBox<String> styleButton = new JComboBox<>(new String[] {"Default","Inverted","Wood"});
styleButton.setBackground(Color.LIGHT_GRAY);
styleButton.setOpaque(true);
styleButton.setPreferredSize(new Dimension(100, 40));
undoButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
model.undo();
repaint();
}
});
styleButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String selectedStyle = (String) styleButton.getSelectedItem();
if (selectedStyle.equals("Default")) {
BoardStyle style1 = new BoardStyleDefault(); //Set to default style
model.setBoardStyle(style1);
} else if (selectedStyle.equals("Inverted")) {
BoardStyle style2 = new BoardStyle2();
model.setBoardStyle(style2);
} else if (selectedStyle.equals("Wood")) {
BoardStyle style3 = new BoardStyleWood();
model.setBoardStyle(style3);
}
}
});
JPanel innerPanel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
String text = "PLAYER A";
Font font = new Font("Arial", Font.BOLD, 30);
g2.setFont(font);
int textX = 20;
int textY = 30;
int centerX = getWidth()/3; //get the center position of panel
g2.drawString(text, centerX + 20, textY);
FontMetrics fm = g2.getFontMetrics();
int textWidth = fm.stringWidth(text);
int arrowStartX = centerX + textWidth + 10;
int arrowStartY = textY - 10;
Point2D.Double end1 = new Point2D.Double(arrowStartX + 65 , arrowStartY);
Point2D.Double start1 = new Point2D.Double(arrowStartX + 20, arrowStartY);
Shape segment1 = new Line2D.Double(start1, end1);
g2.draw(segment1);
Point2D.Double start2 = new Point2D.Double(arrowStartX + 50, 10);
Shape segment2 = new Line2D.Double(start2, end1);
g2.draw(segment2);
Point2D.Double start3 = new Point2D.Double(arrowStartX + 50, 30);
Shape segment3 = new Line2D.Double(start3, end1);
g2.draw(segment3);
}
};
innerPanel.setPreferredSize(new Dimension(500, 80));
JPanel stonePanel = new JPanel();
JButton button3 = new JButton("3 Stones");
button3.setBackground(Color.LIGHT_GRAY);
button3.setOpaque(true);
button3.setPreferredSize(new Dimension(80, 40));
JButton button4 = new JButton("4 Stones");
button4.setBackground(Color.LIGHT_GRAY);
button4.setOpaque(true);
button4.setPreferredSize(new Dimension(80, 40));
button3.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
model.setNumber(3);
repaint();
}
});
button4.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
model.setNumber(4);
repaint();
}
});
stonePanel.add(button3);
stonePanel.add(button4);
outerPanel.add(innerPanel, BorderLayout.NORTH);
outerPanel.add(undoButton, BorderLayout.SOUTH);
outerPanel.add(styleButton, BorderLayout.EAST);
outerPanel.add(stonePanel, BorderLayout.SOUTH);
return outerPanel;
}
}