-
Notifications
You must be signed in to change notification settings - Fork 0
/
TitleScreen.java~
218 lines (193 loc) · 6.81 KB
/
TitleScreen.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
215
216
217
218
/*----------------------------------------------------------------
* Author: Apurva Gandhi
* Email: [email protected]
* Written: 11/29/2018
* This class implements the initial "splash" title screen window.
*
* You can test this class using:
* java TitleScreen
*----------------------------------------------------------------*/
import GUI.*;
/**
* <i>TitleScreen</i> object represents the "splash screen" at the start of the
* game. It shows the title and allows the user to select the game difficulty
* and possibly other options. TitleScreen extends Window, so it can be drawn on
* the screen. It also extends EventListener so it can respond to user
* interaction. See main() for an example of how to use this class.
*/
public class TitleScreen extends Window implements EventListener {
// The user's choice. Uses "quit" as the default if nothing else is chosen.
// Possible values are "quit", "easy", "medium", and "hard".
private String selection = "quit";
// Buttons for the different options.
private Button easy, okay, hard, quit, restore;
/**
* Initialize a new title screen.
*/
public TitleScreen() {
super("Minesweeper!", 600, 400);
setBackgroundColor(Canvas.LIGHT_GRAY);
// Add all the buttons
easy = new Button(280, 120, 160, 40, "1 - Easy");
okay = new Button(280, 170, 160, 40, "2 - Medium");
hard = new Button(280, 220, 160, 40, "3 - Hard");
quit = new Button(280, 320, 160, 40, "Q - Quit");
easy.setForegroundColor(Canvas.WHITE);
easy.setBackgroundColor(Canvas.GREEN.darker());
easy.setBorderColor(null);
okay.setForegroundColor(Canvas.WHITE);
okay.setBackgroundColor(Canvas.BLUE.darker());
okay.setBorderColor(null);
hard.setForegroundColor(Canvas.WHITE);
hard.setBackgroundColor(Canvas.ORANGE.darker());
hard.setBorderColor(null);
quit.setForegroundColor(Canvas.WHITE);
quit.setBackgroundColor(Canvas.RED.darker());
quit.setBorderColor(null);
add(easy);
add(okay);
add(hard);
add(quit);
// Add a title at the top
Label title = new Label(300, 70, "Minesweeper!");
title.setFont("SansSerif Bold", 48);
title.setForegroundColor(Canvas.DARK_BLUE);
add(title);
}
/**
* Paint the background for this window on the canvas. Don't call this
* directly, it is called by the GUI system automatically. This function
* should draw something on the canvas.
* @param canvas the canvas on which to draw.
*/
public void repaintWindowBackground(GUI.Canvas canvas)
{
// Put a nice logo on the left side
canvas.picture(70, 100, "logo.png");
// Also draw some "raised 3D" beveled rectangles, so they look like
// mines. We will use a mostly gray ones, and a few odd colors. I use a
// clever trick to pick the colors.
int r = (int)System.currentTimeMillis() / 500;
for (int i = 0; i < 30; i++)
{
for (int j = 0; j < 20; j++)
{
if (i >= 3 && i < 27 && j >= 2 && j < 18)
continue; // skip the middle part of the window
int x = i*20;
int y = j*20;
setRandomColor(canvas, r, i, j);
canvas.raisedBevelRectangle(x, y, 20, 20, 4.0);
}
}
}
// Pick a random color, but usually gray, and set the pen to that color.
private void setRandomColor(Canvas canvas, int r, int i, int j)
{
int h = r ^ ((i+1) * (j+1));
h = h ^ (h >> 16);
h = h ^ (h >> 8);
switch(h % 97)
{
case 0: canvas.setPenColor(Canvas.BLUE); break;
case 1: canvas.setPenColor(Canvas.CYAN); break;
case 2: canvas.setPenColor(Canvas.GREEN); break;
case 3: canvas.setPenColor(Canvas.MAGENTA); break;
case 4: canvas.setPenColor(Canvas.ORANGE); break;
case 5: canvas.setPenColor(Canvas.PINK); break;
case 6: canvas.setPenColor(Canvas.YELLOW); break;
case 7: canvas.setPenColor(Canvas.MAROON); break;
case 8: canvas.setPenColor(Canvas.TURQUOISE); break;
default: canvas.setPenColor(Canvas.GRAY); break;
}
}
/**
* Respond to key presses. This function will be called each time the user
* presses a key. The parameter indicates the character the user pressed.
* The function updates the state according to what character the user has
* pressed.
* @param c the character that was typed.
*/
public void keyTyped(char c)
{
if (c == '1')
{
selection = "easy";
hide();
}
else if (c == '2')
{
selection = "medium";
hide();
}
else if (c == '3')
{
selection = "hard";
hide();
}
else if (c == 'q' || c == 'Q')
{
selection = "quit";
hide();
}
else if (c == 'r' || c == 'R')
{
selection = "restore";
hide();
}
}
/**
* Respond to a mouse click. This function will be called each time the user
* clicks on the title window. The x, y parameters indicate the screen
* coordinates where the user has clicked, and the button parameter
* indicates which mouse button was clicked (either "left", "middle", or
* "right"). The function updates the state according to what the user has
* clicked.
* @param x the x coordinate where the user clicked, in pixels.
* @param y the y coordinate where the user clicked, in pixels.
* @param button either "left", "middle", or "right".
*/
public void mouseClicked(double x, double y, String button)
{
if (button.equals("left"))
{
if (easy.containsPoint(x, y))
{
selection = "easy";
hide();
}
else if (okay.containsPoint(x, y))
{
selection = "medium";
hide();
}
else if (hard.containsPoint(x, y))
{
selection = "hard";
hide();
}
else if (quit.containsPoint(x, y))
{
selection = "quit";
hide();
}
}
}
/**
* Get the selection. Call this after calling showAndWait() to find out
* what the user selected.
*/
public String getSelection()
{
return selection;
}
/**
* A main() function, for testing.
*/
public static void main(String args[])
{
TitleScreen t = new TitleScreen();
t.showAndAnimate(4);
System.out.println("You chose: " + t.getSelection());
}
}