-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserInterface.java
214 lines (174 loc) · 6.04 KB
/
UserInterface.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
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
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.Timer;
import java.util.*;
import sun.audio.*;
/** Class that renders the game environment */
public class UserInterface
extends JPanel {
//////////////////////////////// fields //////////////////////////////////////
// images
private static BufferedImage background;
private static BufferedImage frog;
private static BufferedImage frogEnemy;
private static BufferedImage lilypad;
// quantities
private static int numOfFrogs;
private static int numOfLilypads;
private static int lilypadLength;
// parameter constants
private static final int BG_SIZE = 1020;
private static final int FROG_LENGTH = 97;
private static final int FROG_HEIGHT = 59;
private static final int LILYPAD_HEIGHT = 58;
private static final int START_Y_LILYPAD = 520;
private static final int START_Y_FROG = 500;
// data structures
private static Queue<Integer> lilyLocationData;
private static int[] frogLocationData;
private static List<Lilypad> lilypadList;
// Jframe
private static JFrame jf;
// Reset Button
private JButton resetButton;
////////////////////////////// constructors ///////////////////////////////////
public UserInterface(double frogCount) {
// initialize images
try{
background = ImageIO.read(getClass().getResource("bg2.jpg"));
frog = ImageIO.read(getClass().getResource("frog.png"));
lilypad = ImageIO.read(getClass().getResource("lilypad3.png"));
frogEnemy = ImageIO.read(getClass().getResource("frog2.png"));
}catch (IOException e){e.printStackTrace();}
// initialize quantities
numOfFrogs = (int)(frogCount);
numOfLilypads = numOfFrogs + 1;
//initialize data structures
lilyLocationData = new ArrayQueue<Integer>();
frogLocationData = new int[numOfLilypads];
lilypadList = new LinkedList<Lilypad>();
for(int i = 0; i < numOfLilypads; i++) {
lilypadList.add(new Lilypad<Frog>());
}
// create LinkedList of lilypad stacks.
for (int i = 0; i != numOfLilypads; i++) {
if (i != (numOfLilypads/2)) {
if (i > (numOfLilypads/2)) {
lilypadList.get(i).push(new Frog(false));
}
else {
lilypadList.get(i).push(new Frog(true));
}
}
}
// Set JFrame parameters
jf = new JFrame();
jf.setSize(BG_SIZE,650);
jf.setLocationRelativeTo(null);
jf.setResizable(true);
jf.setTitle("Frog Puzzle");
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
jf.add(this);
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
if ((y >= 500) && (y <= 550)) {
new Frog(x, frogLocationData, lilypadList, jf);
repaint();
}
}
public void mousePressed(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
});
/* Reset Button ActionListener*/
ActionListener actionListener = new ActionListener(){
public void actionPerformed(ActionEvent event){
refresh(frogCount);
repaint();
}
};
/*JButton*/
resetButton = new JButton("Reset Puzzle");
getRootPane().setDefaultButton(resetButton);
resetButton.addActionListener(actionListener);
/*Panel for the reset button*/
JPanel panel = new JPanel();
panel.add(resetButton);
/*Add panel to Frame*/
add(panel);
}
/////////////////////////////// methods ////////////////////////////////////
// re-initialize variables for reset
public static void refresh(double frogCount) {
numOfFrogs = (int)(frogCount);
numOfLilypads = numOfFrogs + 1;
lilyLocationData = new ArrayQueue<Integer>();
frogLocationData = new int[numOfLilypads];
lilypadList = new LinkedList<Lilypad>();
for(int i = 0; i < numOfLilypads; i++) {
lilypadList.add(new Lilypad<Frog>());
}
for (int i = 0; i != numOfLilypads; i++) {
if (i != (numOfLilypads/2)) {
if (i > (numOfLilypads/2)) {
lilypadList.get(i).push(new Frog(false));
}
else {
lilypadList.get(i).push(new Frog(true));
}
}
}
}
// method that paints the components into the JPanel
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(background, 0,0, getWidth(), getHeight(), null);
drawLilypads(g);
drawFrogs(g);
}/*END paint()*/
// method that draws the lilypads (location depends on number of frogs and background size).
public static void drawLilypads(Graphics g) {
int division = BG_SIZE/numOfLilypads;
lilypadLength = BG_SIZE/(numOfLilypads + 1);
int lilySpace = division - lilypadLength;
int i = 0;
int startX = lilySpace/2;
while (i != numOfLilypads) {
g.drawImage(lilypad, startX, START_Y_LILYPAD, lilypadLength, LILYPAD_HEIGHT, null);
lilyLocationData.add(startX);
startX += lilySpace + lilypadLength;
i++;
}
}
// method that draws the frogs (location depends on the lilypads' locations) and collects their location data
public static void drawFrogs(Graphics g) {
int i = 0;
while (i != numOfLilypads) {
int startX = (lilyLocationData.remove() + (lilypadLength/2)) - (FROG_LENGTH/2);
if (!lilypadList.get(i).isEmpty()) {
if (!((Frog)lilypadList.get(i).peek()).frogSide) {
g.drawImage(frogEnemy, startX, START_Y_FROG, FROG_LENGTH, FROG_HEIGHT, null);
frogLocationData[i] = startX;
}
else {
g.drawImage(frog, startX, START_Y_FROG, FROG_LENGTH, FROG_HEIGHT, null);
frogLocationData[i] = startX;
}
}
else {
frogLocationData[i] = startX;
}
i++;
}
}
}/*END UserInterface
class*/