-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButton.java
More file actions
203 lines (191 loc) · 4.68 KB
/
Button.java
File metadata and controls
203 lines (191 loc) · 4.68 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
import java.awt.Color;
import java.awt.Font;
/**
* This Class creates button objects
* @author elan
*
*/
public class Button
{
/**
* the x, y, height and width are all final private and final as they are never changed
* the background colour changes and is thus not final
* the caption is added after the object is created and thus cannot be final (the constructor conplains)
*/
protected final double x, y, height, width;
private Color backGround = Color.BLACK;
protected String text, caption;
/**
* Constructor for Button
* @param x x co-ordinate where the button will be created
* @param y y co-ordinate where the button will be created
* @param w the width of the button
* @param h the height of the button
* @param t the string that will be displayed on the button
*/
public Button(double x, double y, double w, double h, String t)
{
this.x = x;
this.y = y;
height = h;
width = w;
text = t;
}
/**
* this will draw the button
*/
public void render()
{
StdDraw.setPenColor(backGround);
StdDraw.filledRectangle(x, y, width/(double)2, height/(double)2);
StdDraw.setPenColor(Color.BLUE);
StdDraw.setPenRadius((height/(double)3)*0.001);
StdDraw.rectangle(x, y, width/(double)2, height/(double)2);
StdDraw.setPenColor(Color.WHITE);
StdDraw.setFont(new Font(Font.SANS_SERIF, Font.BOLD, (int) (width * 0.08)));//change to function of num of letters in button?
if(caption == null)
StdDraw.text(x, y - height/(double)10, text);
else
{
StdDraw.setFont(new Font(Font.SANS_SERIF, Font.BOLD, (int) (width * 0.06)));
StdDraw.text(x, y - height/(double)10, text + ": $" + caption);
}
}
/**
* this will color the background red
*/
private void highLight()
{
backGround = Color.RED;
}
/**
* this will add onto the text already displayed on the button
* @param caption string that will be added to the text
*/
public void addCaption(String caption)
{
this.caption = caption;
}
/**
* this will add onto the text already displayed on the button
* @param caption integer that will be added to the text
*/
public void addCaption(int caption)
{
this.caption = "" + caption;
}
/**
* checks if the mouse is on the button and highlights it if so
*/
public void checkMouse()
{
if(this.xInBounds() && this.yInBounds())
{
highLight();
}
else
{
backGround = Color.BLACK;
}
}
/**
* Returns if the mouse pointer is within the x bounds of the button
* @return true if mouses x bounds are within the buttons
*/
private boolean xInBounds()
{
return (StdDraw.mouseX() > x - width/((double)2) && StdDraw.mouseX() < x + width/(double)2);
}
/**
* Returns if the mouse pointer is within the y bounds of the button
* @return true if mouses y bounds are within the buttons
*/
private boolean yInBounds()
{
return (StdDraw.mouseY() > y - height/((double)2) && StdDraw.mouseY() < y + height/(double)2);
}
/**
* decides based on the text that is on the button what it should do if it is clicked
*/
public void clicked()
{
Controller.soundManager.playClick();
if(text.equals("Options"))
{
States.isOptionRunning = true;
States.isWelcomeRunning = false;
}
else if(text.equals("Instructions"))
{
States.isInstructionsRunning = true;
States.isWelcomeRunning = false;
}
else if(text.equals("Play Now!"))
{
States.isPlayRunning = true;
States.isWelcomeRunning = false;
}
else if(text.equals("High-Scores"))
{
States.isHighScoreRunning = true;
States.isWelcomeRunning = false;
}
else if(text.equals("Back"))
{
if(Controller.screen instanceof OptionScreen)
States.isOptionRunning = false;
else if(Controller.screen instanceof InstructionsScreen)
States.isInstructionsRunning = false;
else if(Controller.screen instanceof HighScoreScreen)
States.isHighScoreRunning = false;
States.isWelcomeRunning = true;
}
else if(text.equals("Next Level"))
{
States.isUpgradeRunning = false;
States.isPlayRunning = true;
}
else if(text.equals("Fire Rate"))
{
Store.upgradeFireRate();
}
else if(text.equals("Bullet Damage"))
{
Store.upgradeBulletDamage();
}
else if(text.equals("HP"))
{
Store.upgradeHP();
}
else if(text.equals("Power-Ups"))
{
Store.upgradePowerUps();
}
else if(text.equals("Multi-shot"))
{
Store.upgradeMultiShot();
}
else if(text.equals("Upgrade Bunkers"))
{
Store.upgradeBunkers();
}
else if(text.equals("Buy Lives x5"))
{
Store.increaseLives();
}
else if(text.equals("Warp Drive"))
{
Store.slowEnemies();
}
}
/**
* checks if the mouse is pressed while on the button
*/
public void isClicked()
{
if(this.xInBounds() && this.yInBounds())
{
clicked();
}
}
}