6
6
public class GUI extends JFrame implements ActionListener
7
7
{
8
8
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 ;
9
14
public static boolean gameOver = false ;
10
15
public static int [] clicks = {0 , 0 , 0 , 0 };
11
16
public static final int SCREEN_WIDTH = 1000 ;
12
- public static final int SCREEN_HEIGHT = 680 ;
17
+ public static final int SCREEN_HEIGHT = 720 ;
13
18
public static final int CIRCLE_SIZE = 40 ;
14
19
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" );
18
23
19
24
20
-
21
- GUI () {
25
+ GUI ()
26
+ {
22
27
this .setResizable (false );
23
28
this .setSize (SCREEN_WIDTH , SCREEN_HEIGHT );
24
29
this .getContentPane ().setBackground (Color .DARK_GRAY );
25
30
this .setDefaultCloseOperation (JFrame .EXIT_ON_CLOSE );
26
31
this .setLayout (null );
27
32
this .setVisible (true );
28
33
clickCounter = 0 ;
29
- b_easyButton .setBounds (600 , 10 ,100 ,40 );
34
+ b_easyButton .setBounds (580 , 10 , 100 , 40 );
30
35
b_easyButton .setFont (new Font ("Arial" , Font .PLAIN , 15 ));
31
36
b_easyButton .addActionListener (this );
32
37
this .add (b_easyButton );
33
- b_mediumButton .setBounds (750 ,10 ,100 ,40 );
38
+
39
+ b_mediumButton .setBounds (730 , 10 , 100 , 40 );
34
40
b_mediumButton .addActionListener (this );
35
41
this .add (b_mediumButton );
36
- b_hardButton .setBounds (900 ,10 ,100 ,40 );
42
+
43
+ b_hardButton .setBounds (880 , 10 , 100 , 40 );
37
44
b_hardButton .addActionListener (this );
38
45
b_hardButton .setFont (new Font ("Arial" , Font .PLAIN , 15 ));
39
-
40
46
this .add (b_hardButton );
41
47
42
- addMouseListener (new MouseAdapter () {
48
+ addMouseListener (new MouseAdapter ()
49
+ {
43
50
@ Override
44
- public void mouseClicked (MouseEvent mouseEvent ) {
45
- if (gameOver ) return ;
51
+ public void mouseClicked (MouseEvent mouseEvent )
52
+ {
53
+ if (gameOver ) return ;
46
54
47
55
Point lastClick = new Point (mouseEvent .getX (), mouseEvent .getY ());
48
56
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
+ {
50
59
51
60
clicks [0 ] = actualPoint .x ;
52
61
clicks [1 ] = actualPoint .y ;
@@ -55,12 +64,14 @@ public void mouseClicked(MouseEvent mouseEvent) {
55
64
ArrayList <StateManager .Move > allmoves = GameManager .getPlayerMove ();
56
65
moves = new ArrayList <>();
57
66
58
- for (StateManager .Move m : allmoves ) {
67
+ for (StateManager .Move m : allmoves )
68
+ {
59
69
if (m .x1 == clicks [0 ] && m .y1 == clicks [1 ]) moves .add (Graph .map (m .x2 , m .y2 ));
60
70
}
61
71
62
72
reDraw ();
63
- } else if (find (moves , Graph .map (actualPoint .x , actualPoint .y ))) {
73
+ } else if (find (moves , Graph .map (actualPoint .x , actualPoint .y )))
74
+ {
64
75
65
76
clicks [2 ] = actualPoint .x ;
66
77
clicks [3 ] = actualPoint .y ;
@@ -83,47 +94,61 @@ public void showWinText(int status)
83
94
{
84
95
if (status == -1 )
85
96
{
86
- gameOver =true ;
97
+ gameOver = true ;
87
98
JLabel l_uWin = new JLabel ("You Win" );
88
99
l_uWin .setFont (new Font ("Arial" , Font .PLAIN , 24 ));
89
100
l_uWin .setForeground (Color .PINK );
90
- l_uWin .setBounds (20 ,20 ,100 ,30 );
101
+ l_uWin .setBounds (20 , 20 , 100 , 30 );
91
102
this .add (l_uWin );
92
- }
93
- else if (status == 1 )
103
+ } else if (status == 1 )
94
104
{
95
- gameOver =true ;
105
+ gameOver = true ;
96
106
JLabel l_aiWins = new JLabel ("AI Wins" );
97
107
l_aiWins .setFont (new Font ("Arial" , Font .PLAIN , 24 ));
98
108
l_aiWins .setForeground (Color .CYAN );
99
- l_aiWins .setBounds (20 ,20 ,100 ,30 );
109
+ l_aiWins .setBounds (20 , 20 , 100 , 30 );
100
110
this .add (l_aiWins );
101
111
}
102
112
}
103
113
104
- public void reDraw () {
114
+ public void reDraw ()
115
+ {
105
116
this .repaint ();
106
117
}
107
- public void paint (Graphics g ) {
118
+
119
+ public void paint (Graphics g )
120
+ {
108
121
super .paint (g );
122
+ /*
123
+ //Gradient background
109
124
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
+ */
110
132
111
133
//Draw board
112
- for (Graph .Node node : Graph .nodes ) {
134
+ for (Graph .Node node : Graph .nodes )
135
+ {
113
136
g .setColor (Color .WHITE );
114
137
if (node == null ) continue ;
115
138
Point position = new Point (map (Graph .getCol (node .num ), Graph .getRow (node .num )));
116
139
g .fillOval (position .x , position .y , CIRCLE_SIZE , CIRCLE_SIZE );
117
140
}
118
141
119
142
g .setColor (Color .RED );
120
- for (int i = 0 ; i < 10 ; i ++) {
143
+ for (int i = 0 ; i < 10 ; i ++)
144
+ {
121
145
Point position = new Point (map (Graph .getCol (StateManager .curr .redMarbles [i ]), Graph .getRow (StateManager .curr .redMarbles [i ])));
122
146
g .fillOval (position .x , position .y , CIRCLE_SIZE , CIRCLE_SIZE );
123
147
}
124
148
125
149
g .setColor (Color .BLUE );
126
- for (int i = 0 ; i < 10 ; i ++) {
150
+ for (int i = 0 ; i < 10 ; i ++)
151
+ {
127
152
Point position = new Point (map (Graph .getCol (StateManager .curr .blueMarbles [i ]), Graph .getRow (StateManager .curr .blueMarbles [i ])));
128
153
g .fillOval (position .x , position .y , CIRCLE_SIZE , CIRCLE_SIZE );
129
154
}
@@ -132,33 +157,40 @@ public void paint(Graphics g) {
132
157
return ;
133
158
134
159
g .setColor (Color .YELLOW );
135
- for (Integer m : moves ) {
160
+ for (Integer m : moves )
161
+ {
136
162
Point position = new Point (map (Graph .getCol (m ), Graph .getRow (m )));
137
163
g .fillOval (position .x , position .y , CIRCLE_SIZE , CIRCLE_SIZE );
138
164
}
139
165
140
166
}
141
167
142
- boolean find (ArrayList <Integer > moves , int num ) {
168
+ boolean find (ArrayList <Integer > moves , int num )
169
+ {
143
170
if (moves == null )
144
171
return false ;
145
- for (Integer m : moves ) {
172
+ for (Integer m : moves )
173
+ {
146
174
if (m == num )
147
175
return true ;
148
176
}
149
177
return false ;
150
178
}
151
179
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 ));
155
185
return new Point (newY , newX );
156
186
}
157
187
158
188
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 );
162
194
return new Point (newX , newY );
163
195
}
164
196
@@ -170,13 +202,11 @@ public void actionPerformed(ActionEvent e)
170
202
{
171
203
GameManager .depth = 1 ;
172
204
System .out .println ("Changed difficulty to easy" );
173
- }
174
- else if (e .getSource () == b_mediumButton )
205
+ } else if (e .getSource () == b_mediumButton )
175
206
{
176
207
GameManager .depth = 3 ;
177
208
System .out .println ("Changed difficulty to medium" );
178
- }
179
- else if (e .getSource () == b_hardButton )
209
+ } else if (e .getSource () == b_hardButton )
180
210
{
181
211
GameManager .depth = 5 ;
182
212
System .out .println ("Changed difficulty to hard" );
0 commit comments