-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTurnPanel.java
More file actions
206 lines (176 loc) · 7.3 KB
/
Copy pathTurnPanel.java
File metadata and controls
206 lines (176 loc) · 7.3 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class TurnPanel extends JPanel implements MouseListener{
static int divisor = 4;
/*
---------------------------------------
| Back Button |
| Title |
| AIFIrst Button playerFirst button |
---------------------------------------
*/
//Constants for panel layout
static final int SCREEN_WIDTH = 1500/2;
static final int SCREEN_HEIGHT= 1000/2;
static final int BOARD_WIDTH = 700/divisor;
static final int BOARD_HEIGHT = 600/divisor;
static final int UNIT_SIZE = 100/2;
static final int BOARD_X = 125;
static final int BOARD_Y = 0;
static final int GAME_UNITS = (BOARD_WIDTH * BOARD_HEIGHT) / UNIT_SIZE;
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 int idx = 0;
//All Panels
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;
}
};
//all panels
JPanel titlePanel = new JPanel();
JLabel titleLabel = new JLabel("Connect 4");
JLabel SoundLabel = new JLabel();
JLabel MuteLabel = new JLabel();
//All buttons used
JButton AIFirstButton = new JButton("AI First");
JButton playerFirstButton = new JButton("Player First");
JButton SoundButton = new JButton(".");
JButton MuteButton = new JButton("");
Font fileNameFont = new Font("DialogInput", Font.BOLD, 30);
Font titleFont = new Font("DialogInput", Font.BOLD, 75);
static final Color lightBlue = new Color(177, 225, 242);
TurnPanel() {
setPreferredSize(new Dimension(SCREEN_WIDTH, SCREEN_HEIGHT));
setBackground(lightBlue);
setFocusable(true);
setLayout(null);
addMouseListener(this);
addKeyListener(new MyKeyAdapter());
//back menu set up and formatting
backMenuButton.setBorder(BorderFactory.createLineBorder(Color.black));
backMenuButton.setFont(new Font("DialogInput", Font.BOLD, 15));
backMenuButton.setBounds(UNIT_SIZE / 4, UNIT_SIZE / 2, UNIT_SIZE + 20, UNIT_SIZE);
add(backMenuButton);
//Mute button and label setup
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);
//Sound button label and button set up
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);
//title panel set up
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);
//AIFirst button formatting
AIFirstButton.setBorder(BorderFactory.createLineBorder(Color.black));
AIFirstButton.setFont(fileNameFont);
AIFirstButton.setBounds(UNIT_SIZE * 2, UNIT_SIZE*5, UNIT_SIZE*5, UNIT_SIZE*2);
add(AIFirstButton);
//playerFirst button set up
playerFirstButton.setBorder(BorderFactory.createLineBorder(Color.black));
playerFirstButton.setFont(fileNameFont);
playerFirstButton.setBounds(UNIT_SIZE * 8, UNIT_SIZE*5, UNIT_SIZE*5, UNIT_SIZE*2);
add(playerFirstButton);
//backsground 1 and 2 set up
backGround1.setBounds(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
backGround2.setBounds(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
add(backGround1);
}
//every time a keyboard is pressed, the background changes
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) {
}
}