Skip to content

Commit 084174a

Browse files
committed
GUI Enhancement
1 parent f172380 commit 084174a

File tree

10 files changed

+72
-46
lines changed

10 files changed

+72
-46
lines changed

.idea/misc.xml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
File renamed without changes.

README.md

-4
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,5 @@ click on its own so you can just play with the mouse.
4545

4646
![ScreenShot](Screenshot%202.png)
4747

48-
![ScreenShot](gameplay%201.png)
49-
50-
![ScreenShot](gameplay%202.png)
51-
5248

5349

Screenshot 1.png

20 KB
Loading

Screenshot 2.png

17.7 KB
Loading

gameplay 1.png

-27.2 KB
Binary file not shown.

gameplay 2.png

-27.2 KB
Binary file not shown.
-968 Bytes
Binary file not shown.

src/GUI.java

+70-40
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,56 @@
66
public class GUI extends JFrame implements ActionListener
77
{
88
public static int clickCounter = 0;
9+
private static final int xOffset = 100;
10+
private static final int yOffset = 50;
11+
private static final int yPadding = 40;
12+
private static final float xScaling = 0.7f;
13+
private static final float yScaling = 0.95f;
914
public static boolean gameOver = false;
1015
public static int[] clicks = {0, 0, 0, 0};
1116
public static final int SCREEN_WIDTH = 1000;
12-
public static final int SCREEN_HEIGHT = 680;
17+
public static final int SCREEN_HEIGHT = 720;
1318
public static final int CIRCLE_SIZE = 40;
1419
ArrayList<Integer> moves;
15-
JButton b_easyButton =new JButton("Easy");
16-
JButton b_mediumButton =new JButton("Medium");
17-
JButton b_hardButton =new JButton("Hard");
20+
JButton b_easyButton = new JButton("Easy");
21+
JButton b_mediumButton = new JButton("Medium");
22+
JButton b_hardButton = new JButton("Hard");
1823

1924

20-
21-
GUI() {
25+
GUI()
26+
{
2227
this.setResizable(false);
2328
this.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
2429
this.getContentPane().setBackground(Color.DARK_GRAY);
2530
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
2631
this.setLayout(null);
2732
this.setVisible(true);
2833
clickCounter = 0;
29-
b_easyButton.setBounds(600,10,100,40);
34+
b_easyButton.setBounds(580, 10, 100, 40);
3035
b_easyButton.setFont(new Font("Arial", Font.PLAIN, 15));
3136
b_easyButton.addActionListener(this);
3237
this.add(b_easyButton);
33-
b_mediumButton.setBounds(750,10,100,40);
38+
39+
b_mediumButton.setBounds(730, 10, 100, 40);
3440
b_mediumButton.addActionListener(this);
3541
this.add(b_mediumButton);
36-
b_hardButton.setBounds(900,10,100,40);
42+
43+
b_hardButton.setBounds(880, 10, 100, 40);
3744
b_hardButton.addActionListener(this);
3845
b_hardButton.setFont(new Font("Arial", Font.PLAIN, 15));
39-
4046
this.add(b_hardButton);
4147

42-
addMouseListener(new MouseAdapter() {
48+
addMouseListener(new MouseAdapter()
49+
{
4350
@Override
44-
public void mouseClicked(MouseEvent mouseEvent) {
45-
if(gameOver) return;
51+
public void mouseClicked(MouseEvent mouseEvent)
52+
{
53+
if (gameOver) return;
4654

4755
Point lastClick = new Point(mouseEvent.getX(), mouseEvent.getY());
4856
Point actualPoint = new Point(invMap(lastClick));
49-
if (Utilities.find(StateManager.curr.redMarbles, Graph.map(actualPoint.x, actualPoint.y))) {
57+
if (Utilities.find(StateManager.curr.redMarbles, Graph.map(actualPoint.x, actualPoint.y)))
58+
{
5059

5160
clicks[0] = actualPoint.x;
5261
clicks[1] = actualPoint.y;
@@ -55,12 +64,14 @@ public void mouseClicked(MouseEvent mouseEvent) {
5564
ArrayList<StateManager.Move> allmoves = GameManager.getPlayerMove();
5665
moves = new ArrayList<>();
5766

58-
for (StateManager.Move m : allmoves) {
67+
for (StateManager.Move m : allmoves)
68+
{
5969
if (m.x1 == clicks[0] && m.y1 == clicks[1]) moves.add(Graph.map(m.x2, m.y2));
6070
}
6171

6272
reDraw();
63-
} else if (find(moves, Graph.map(actualPoint.x, actualPoint.y))) {
73+
} else if (find(moves, Graph.map(actualPoint.x, actualPoint.y)))
74+
{
6475

6576
clicks[2] = actualPoint.x;
6677
clicks[3] = actualPoint.y;
@@ -83,47 +94,61 @@ public void showWinText(int status)
8394
{
8495
if (status == -1)
8596
{
86-
gameOver =true;
97+
gameOver = true;
8798
JLabel l_uWin = new JLabel("You Win");
8899
l_uWin.setFont(new Font("Arial", Font.PLAIN, 24));
89100
l_uWin.setForeground(Color.PINK);
90-
l_uWin.setBounds(20,20,100,30);
101+
l_uWin.setBounds(20, 20, 100, 30);
91102
this.add(l_uWin);
92-
}
93-
else if (status == 1)
103+
} else if (status == 1)
94104
{
95-
gameOver =true;
105+
gameOver = true;
96106
JLabel l_aiWins = new JLabel("AI Wins");
97107
l_aiWins.setFont(new Font("Arial", Font.PLAIN, 24));
98108
l_aiWins.setForeground(Color.CYAN);
99-
l_aiWins.setBounds(20,20,100,30);
109+
l_aiWins.setBounds(20, 20, 100, 30);
100110
this.add(l_aiWins);
101111
}
102112
}
103113

104-
public void reDraw() {
114+
public void reDraw()
115+
{
105116
this.repaint();
106117
}
107-
public void paint(Graphics g) {
118+
119+
public void paint(Graphics g)
120+
{
108121
super.paint(g);
122+
/*
123+
//Gradient background
109124
Graphics2D graphics2D = (Graphics2D) g;
125+
Color color1 = Color.BLACK;
126+
Color color2 = Color.DARK_GRAY;
127+
GradientPaint gp = new GradientPaint(0, SCREEN_HEIGHT, color1, SCREEN_WIDTH, 0, color2);
128+
graphics2D.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
129+
graphics2D.setPaint(gp);
130+
graphics2D.fillRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
131+
*/
110132

111133
//Draw board
112-
for (Graph.Node node : Graph.nodes) {
134+
for (Graph.Node node : Graph.nodes)
135+
{
113136
g.setColor(Color.WHITE);
114137
if (node == null) continue;
115138
Point position = new Point(map(Graph.getCol(node.num), Graph.getRow(node.num)));
116139
g.fillOval(position.x, position.y, CIRCLE_SIZE, CIRCLE_SIZE);
117140
}
118141

119142
g.setColor(Color.RED);
120-
for (int i = 0; i < 10; i++) {
143+
for (int i = 0; i < 10; i++)
144+
{
121145
Point position = new Point(map(Graph.getCol(StateManager.curr.redMarbles[i]), Graph.getRow(StateManager.curr.redMarbles[i])));
122146
g.fillOval(position.x, position.y, CIRCLE_SIZE, CIRCLE_SIZE);
123147
}
124148

125149
g.setColor(Color.BLUE);
126-
for (int i = 0; i < 10; i++) {
150+
for (int i = 0; i < 10; i++)
151+
{
127152
Point position = new Point(map(Graph.getCol(StateManager.curr.blueMarbles[i]), Graph.getRow(StateManager.curr.blueMarbles[i])));
128153
g.fillOval(position.x, position.y, CIRCLE_SIZE, CIRCLE_SIZE);
129154
}
@@ -132,33 +157,40 @@ public void paint(Graphics g) {
132157
return;
133158

134159
g.setColor(Color.YELLOW);
135-
for (Integer m : moves) {
160+
for (Integer m : moves)
161+
{
136162
Point position = new Point(map(Graph.getCol(m), Graph.getRow(m)));
137163
g.fillOval(position.x, position.y, CIRCLE_SIZE, CIRCLE_SIZE);
138164
}
139165

140166
}
141167

142-
boolean find(ArrayList<Integer> moves, int num) {
168+
boolean find(ArrayList<Integer> moves, int num)
169+
{
143170
if (moves == null)
144171
return false;
145-
for (Integer m : moves) {
172+
for (Integer m : moves)
173+
{
146174
if (m == num)
147175
return true;
148176
}
149177
return false;
150178
}
151179

152-
public Point invMap(Point point) {
153-
int newX = ((point.x / (SCREEN_WIDTH / 25)));
154-
int newY = (int) (((point.y - 35.0) / 0.95) * (17.0 / SCREEN_HEIGHT));
180+
public Point invMap(Point point)
181+
{
182+
int actualHeight = SCREEN_HEIGHT - yPadding;
183+
int newX = (int) ((point.x - xOffset) / ((SCREEN_WIDTH * xScaling) / 25));
184+
int newY = (int) (((point.y - yOffset) / yScaling) * (17.0 / actualHeight));
155185
return new Point(newY, newX);
156186
}
157187

158188

159-
public Point map(int x, int y) {
160-
int newX = (int) (((SCREEN_WIDTH / 25) * x));
161-
int newY = (int) (((SCREEN_HEIGHT / 17) * y) * (0.95) + 35);
189+
public Point map(int x, int y)
190+
{
191+
int actualHeight = SCREEN_HEIGHT - yPadding;
192+
int newX = (int) ((((SCREEN_WIDTH * xScaling / 25) * x))) + xOffset;
193+
int newY = (int) (((actualHeight / 17) * y) * (yScaling) + yOffset);
162194
return new Point(newX, newY);
163195
}
164196

@@ -170,13 +202,11 @@ public void actionPerformed(ActionEvent e)
170202
{
171203
GameManager.depth = 1;
172204
System.out.println("Changed difficulty to easy");
173-
}
174-
else if (e.getSource() == b_mediumButton)
205+
} else if (e.getSource() == b_mediumButton)
175206
{
176207
GameManager.depth = 3;
177208
System.out.println("Changed difficulty to medium");
178-
}
179-
else if(e.getSource() == b_hardButton)
209+
} else if (e.getSource() == b_hardButton)
180210
{
181211
GameManager.depth = 5;
182212
System.out.println("Changed difficulty to hard");

0 commit comments

Comments
 (0)