forked from jnguye14/MathGame
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGamePanel.java
128 lines (112 loc) · 3.73 KB
/
GamePanel.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
/*** Jordan Nguyen ***/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
@SuppressWarnings("serial")
public class GamePanel extends JPanel
{
protected Model MODEL;
protected JLabel problem, answerPrompt, score, msg, timeLab;
protected JTextField entry;
protected JButton clear; // replay
protected JButton[] buttons;
GamePanel()
{
MODEL = new Model();
// Set JLabel messages from MODEL & center align
problem = new JLabel(MODEL.prb);
score = new JLabel(MODEL.str);
msg = new JLabel(MODEL.msg);
timeLab = new JLabel(MODEL.time);
problem.setHorizontalAlignment(JLabel.CENTER);
score.setHorizontalAlignment(JLabel.CENTER);
msg.setHorizontalAlignment(JLabel.CENTER);
timeLab.setHorizontalAlignment(JLabel.CENTER);
// create area where user types in answer
answerPrompt = new JLabel("Answer: ");
answerPrompt.setHorizontalAlignment(JLabel.RIGHT);
entry = new JTextField("Type...");
entry.setSelectionStart(0);
entry.setSelectionEnd(7);
entry.setHorizontalAlignment(JTextField.CENTER);
clear = new JButton("Clear");
// answer area contained in an HBox
JPanel HBox = new JPanel();
HBox.setLayout(new GridLayout(1,3));
HBox.add(answerPrompt);
HBox.add(entry);
HBox.add(clear);
HBox.setDoubleBuffered(true);
// Timer
MODEL.timer = new Timer(1000, new ActionListener()
{
public void actionPerformed(ActionEvent e)
{ Update("time"); }
});
// Entire Game Area in VBox (messages & answer area)
JPanel VBox = new JPanel();
VBox.setLayout(new GridLayout(5,1));
VBox.setDoubleBuffered(true);
VBox.add(problem);
VBox.add(HBox);
VBox.add(score);
VBox.add(msg);
VBox.add(timeLab);
// Create KeyPad
JPanel ButtonPanel = new JPanel();
ButtonPanel.setLayout(new GridLayout(4,3));
ButtonPanel.setDoubleBuffered(true);
buttons = new JButton[10];
for(int i = 1; i < buttons.length; i++)
{
buttons[i] = new JButton(Integer.toString(i));
ButtonPanel.add(buttons[i]);
}
JLabel filler = new JLabel(" ");
ButtonPanel.add(filler);
buttons[0] = new JButton("0");
ButtonPanel.add(buttons[0]);
// put everything together
add(VBox);
add(ButtonPanel);
} // end of GamePanel() constructor
public void Update(String event)
{
if(event.equals("time"))
{
MODEL.time = "<html>Elapsed Time: <font color =\"GREEN\">"
+ MODEL.watch.getElapsedTimeSecs()
+ "</font> Seconds</html>";
timeLab.setText(MODEL.time);
}
else if(event.equals("invalid"))
{
MODEL.answerInvalid();
msg.setText(MODEL.msg);
}
else if(event.equals("correct"))
{
MODEL.answerCorrect();
score.setText(MODEL.str);
msg.setText(MODEL.msg);
problem.setText(MODEL.prb);
entry.setText("");
entry.requestFocus();
}
else if(event.equals("wrong"))
{
MODEL.answerWrong();
msg.setText(MODEL.msg);
}
else if(event.equals("replay"))
{
MODEL.gameReplay();
problem.setText(MODEL.prb);
score.setText(MODEL.str);
msg.setText(MODEL.msg);
timeLab.setText(MODEL.time);
}
else
System.out.println("Invalid Update Command");
}
} // end of GamePanel class