-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainPanel.java
More file actions
183 lines (163 loc) · 5.05 KB
/
MainPanel.java
File metadata and controls
183 lines (163 loc) · 5.05 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
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.Timer;
/**
* Main Panel
*
* An extension of JPanel, this creates the main panel that the
* rest of the buttons, panels, and text areas are held in.
*
* @author Quinten Houck
* @version 8/1/2021
*
*/
public class MainPanel extends JPanel{
Image background;
Data data;
private int myTimerDelay;
private final Timer myTimer;
GamePanel gp;
JTextArea score;
JButton resetButton;
JButton tutButton;
JButton nextButton;
JButton backButton;
public MainPanel(Data data) {
super();
this.data = data;
this.setPreferredSize(new Dimension(700, 700));
this.setLayout(null);
this.setBackground(Color.WHITE);
this.setVisible(true);
//Starts the repaint timer and sets for 200 milliseconds//
myTimerDelay = 200;
myTimer = new Timer(myTimerDelay, gameTimer);
myTimer.start();
//Instantiates the game panel//
gp = new GamePanel(data);
gp.setBounds(50, 75, 600, 600);
this.add(gp);
//Button that resets the game and brings the score back to 0//
resetButton = new JButton("Reset");
resetButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
reset(data, gp);
}
});
resetButton.setVisible(true);
resetButton.setBounds(300, 20, 100, 40);
this.add(resetButton);
//Text Area that displays the current score//
score = new JTextArea();
score.setOpaque(false);
score.setEditable(false);
Font font = new Font("Arial", Font.BOLD, 30);
score.setFont(font);
score.setText("Score: " + data.getCount());
score.setVisible(true);
score = data.getScore();
score.setBounds(110, 20, 150, 40);
this.add(score);
//Button that brings the user to the tutorial screen//
tutButton = new JButton();
tutButton.setFont(font);
tutButton.setText("?");
tutButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
data.setTutorial(true);
data.settPage(true);
}
});
tutButton.setVisible(true);
tutButton.setBounds(410, 20, 60, 40);
this.add(tutButton);
//Sends the user to the next frame of the tutorial screen//
nextButton = new JButton();
font = new Font("Arial", Font.PLAIN , 20);
nextButton.setFont(font);
nextButton.setText("Next");
nextButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
data.settPage(false);
}
});
nextButton.setVisible(false);
nextButton.setBounds(475, 640, 100, 40);
this.add(nextButton);
//Returns the user from the next screen//
backButton = new JButton();
font = new Font("Arial", Font.PLAIN , 20);
backButton.setFont(font);
backButton.setText("Back");
backButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(data.gettPage()) {
data.setTutorial(false);
} else {
data.settPage(true);
}
}
});
backButton.setVisible(false);
backButton.setBounds(30, 640, 80, 40);
this.add(backButton);
}
//Where the timer calls//
ActionListener gameTimer = new ActionListener() {
@Override
public void actionPerformed(ActionEvent theEvent) {
redraw();
}
};
//Reset function that clears all squares and sets score to 0//
public static void reset(Data data, GamePanel gp) {
data.setCount(0);
data.setLastX(-1);
data.setLastY(-1);
data.reset();
gp.reset();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(background, 0, 0, 700, 700, this);
}
//Determines what should be painted or visible//
public void redraw() {
if (data.getTutorial()) {
gp.setVisible(false);
score.setVisible(false);
resetButton.setVisible(false);
tutButton.setVisible(false);
backButton.setVisible(true);
if (data.gettPage()) {
background = Images.getImage("sprites/GridGameWords.PNG");
nextButton.setVisible(true);
} else {
background = Images.getImage("sprites/GridGameBackground.PNG");
nextButton.setVisible(false);
}
} else {
background = null;
gp.setVisible(true);
resetButton.setVisible(true);
score.setVisible(true);
nextButton.setVisible(false);
tutButton.setVisible(true);
backButton.setVisible(false);
score.setText("Score: " + data.getCount());
}
this.repaint();
}
}