-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSaveGamePanel.java
More file actions
194 lines (162 loc) · 6.89 KB
/
SaveGamePanel.java
File metadata and controls
194 lines (162 loc) · 6.89 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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JPanel;
public class SaveGamePanel extends JPanel implements MouseListener{
static final int SCREEN_WIDTH = 1500/2;
static final int SCREEN_HEIGHT= 1000/2;
static final int BOARD_WIDTH = 700/2;
static final int BOARD_HEIGHT = 600/2;
static final int UNIT_SIZE = 100/2;
static final int BOARD_X = 600/2;
static final int BOARD_Y = 300/2;
static final int GAME_UNITS = (BOARD_WIDTH*BOARD_HEIGHT)/UNIT_SIZE;
//create necessary labels and buttons
JLabel titleLabel = new JLabel("Connect 4");
JLabel instrLabel = new JLabel("Please enter the name of your save file below:");
JLabel SoundLabel = new JLabel();
JLabel MuteLabel = new JLabel();
JTextField textField = new JTextField();
JButton enterButton = new JButton("Enter");
JButton returnGameButton = new JButton("Return to Game");
JButton SoundButton = new JButton(".");
JButton MuteButton = new JButton("");
Font titleFont = new Font("Monospaced", Font.BOLD, 75);
Font instrFont = new Font("DialogInput", Font.BOLD, 20);
Font buttonFont = new Font("Monospaced", Font.BOLD, 15);
static int idx = 0;
static int MULTIPLIER = 2;
static final int OFFSET = 35;
int clicks = 0;
static final Color COLOURS[] = {Color.red, Color.ORANGE, Color.yellow, Color.green, Color.cyan, Color.blue, new Color(75,0,130), };
static final Color lightBlue = new Color(177, 225, 242);
JPanel titlePanel = new JPanel();
JPanel instructionPanel = new JPanel();
JPanel backGround1 = new JPanel() {
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(lightBlue);
g.fillRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
//draw emptyCircles
int index = idx;
for (int j = 0; j < SCREEN_HEIGHT / UNIT_SIZE; j++) {
for (int i = 0; i < SCREEN_WIDTH / UNIT_SIZE; i++) {
index = (index+1)% COLOURS.length;
Color colour = COLOURS[index];
g.setColor(colour);
g.fillOval(i * UNIT_SIZE * MULTIPLIER-OFFSET, j * UNIT_SIZE * MULTIPLIER-OFFSET, UNIT_SIZE*MULTIPLIER, UNIT_SIZE*MULTIPLIER);
g.setColor(Color.BLACK);
g.drawOval(i * UNIT_SIZE * MULTIPLIER-OFFSET, j * UNIT_SIZE * MULTIPLIER-OFFSET, UNIT_SIZE*MULTIPLIER, UNIT_SIZE*MULTIPLIER);
}
}
idx+=6;
}
};
JButton backMenuButton = new JButton("<--");
JPanel backGround2 = new JPanel() {
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(lightBlue);
g.fillRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
//draw emptyCircles
int tokenCount = 1, index = idx;
for (int j = 0; j < SCREEN_HEIGHT / UNIT_SIZE; j++) {
for (int i = 0; i < SCREEN_WIDTH / UNIT_SIZE; i++) {
index = (index+1)% COLOURS.length;
Color colour = COLOURS[index];
//Color colour = (tokenCount%2 == 0? Color.red: Color.yellow);
g.setColor(colour);
g.fillOval(i * UNIT_SIZE * MULTIPLIER-OFFSET, j * UNIT_SIZE * MULTIPLIER-OFFSET, UNIT_SIZE*MULTIPLIER, UNIT_SIZE*MULTIPLIER);
g.setColor(Color.BLACK);
g.drawOval(i * UNIT_SIZE * MULTIPLIER-OFFSET, j * UNIT_SIZE * MULTIPLIER-OFFSET, UNIT_SIZE*MULTIPLIER, UNIT_SIZE*MULTIPLIER);
tokenCount++;
}
}
idx+=6;
}
};
SaveGamePanel(){
this.setPreferredSize(new Dimension(SCREEN_WIDTH, SCREEN_HEIGHT));
this.setBackground(Color.white);
this.setFocusable(true);
this.setLayout(null);
this.addKeyListener(new MyKeyAdapter());
returnGameButton.setBounds(10, 10, UNIT_SIZE*4, UNIT_SIZE/2);
returnGameButton.setFont(buttonFont);
this.add(returnGameButton);
MuteLabel.setIcon(new ImageIcon("Mute.jpg"));
MuteButton.add(MuteLabel);
MuteButton.setBackground(Color.WHITE);
MuteButton.setBounds(UNIT_SIZE*13+10, UNIT_SIZE/2, UNIT_SIZE+25, UNIT_SIZE);
add(MuteButton);
MuteButton.setVisible(false);
SoundLabel.setIcon(new ImageIcon("Sound.jpg"));
SoundButton.add(SoundLabel);
SoundButton.setBackground(Color.WHITE);
SoundButton.setBounds(UNIT_SIZE*13+10, UNIT_SIZE/2, UNIT_SIZE+25, UNIT_SIZE);
add(SoundButton);
titlePanel.setBorder(BorderFactory.createLineBorder(Color.black));
titlePanel.setBackground(Color.white);
titlePanel.setBounds(UNIT_SIZE*2, UNIT_SIZE, UNIT_SIZE*11, UNIT_SIZE*2+10);
titleLabel.setFont(titleFont);
titlePanel.add(titleLabel);
add(titlePanel);
textField.setFont(instrFont);
textField.setBounds(UNIT_SIZE*4 + UNIT_SIZE/2, UNIT_SIZE*5,UNIT_SIZE*4, UNIT_SIZE);
this.add(textField);
enterButton.setBounds(UNIT_SIZE*8 + UNIT_SIZE/2, UNIT_SIZE*5, UNIT_SIZE*2, UNIT_SIZE);
enterButton.setFont(buttonFont);
this.add(enterButton);
instrLabel.setFont(instrFont);
instructionPanel.add(instrLabel);
instructionPanel.setBorder(BorderFactory.createLineBorder(Color.black));
instructionPanel.setBounds(UNIT_SIZE+UNIT_SIZE/2, UNIT_SIZE*4, UNIT_SIZE*12, UNIT_SIZE*2 + UNIT_SIZE/2);
this.add(instructionPanel);
//backsground 1 and 2 set up
backGround1.setBounds(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
backGround2.setBounds(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
add(backGround1);
}
//detects input from the keyboard
public class MyKeyAdapter extends KeyAdapter {
public void keyPressed(KeyEvent e){
if (clicks%2 == 0) {
remove(backGround1);
add(backGround2);
}
else {
add(backGround1);
remove(backGround2);
}
clicks++;
repaint();
revalidate();
}
}
//on click, change the background.
public void mouseClicked(MouseEvent e) {
if (clicks%2 == 0) {
remove(backGround1);
add(backGround2);
}
else {
add(backGround1);
remove(backGround2);
}
clicks++;
repaint();
revalidate();
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}