-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathButton.java
More file actions
113 lines (96 loc) · 2.66 KB
/
Button.java
File metadata and controls
113 lines (96 loc) · 2.66 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
import greenfoot.Actor;
import greenfoot.Color;
import greenfoot.Greenfoot;
import greenfoot.GreenfootImage;
import greenfoot.MouseInfo;
/**
* Defines the plan for the buttons, but needs to be specialized as one of the
* subclasses.
*/
public abstract class Button extends Actor {
public static final GreenfootImage TEMP_IDLE_IMG = generateTempIdleImage(32);
public static final GreenfootImage TEMP_ACTIVE_IMG = generateTempActiveImage(32);
private boolean active;
private GreenfootImage idleImage;
private GreenfootImage activeImage;
public Button() {
this(TEMP_IDLE_IMG, TEMP_ACTIVE_IMG);
}
public Button(String idleImageName, String activeImageName) {
this(new GreenfootImage(idleImageName), new GreenfootImage(activeImageName));
}
public Button(GreenfootImage idleImage, GreenfootImage activeImage) {
this.idleImage = idleImage;
this.activeImage = activeImage;
setImage(idleImage);
}
/**
* Presses the button when clicking with the mouse on it.
*/
@Override
public void act() {
MouseInfo mouse = Greenfoot.getMouseInfo();
if(Greenfoot.mouseClicked(this) && mouse.getButton() == 1 && mouse != null) {
clickAction();
}
}
/**
* what happens when the button is clicked
*/
protected abstract void clickAction();
/**
* updates the image accordingly to the current state
*/
public void updateImage() {
if(active) {
setImage(getActiveImage());
} else {
setImage(getIdleImage());
}
}
/**
* Generates the image for an idle button.
*
* @param size - the size of the button
* @return the image for the button
*/
public static GreenfootImage generateTempIdleImage(int size) {
GreenfootImage img = new GreenfootImage(size, size);
img.setColor(Color.BLACK);
img.fillOval(1, 1, img.getWidth() - 2, img.getHeight() - 2);
img.setColor(Color.RED);
img.drawOval(1, 1, img.getWidth() - 2, img.getHeight() - 2);
return img;
}
/**
* Generates the image for an active button.
*
* @param size - the size of the button
* @return the image for the button
*/
public static GreenfootImage generateTempActiveImage(int size) {
GreenfootImage img = new GreenfootImage(size, size);
img.setColor(Color.WHITE);
img.fillOval(1, 1, img.getWidth() - 2, img.getHeight() - 2);
img.setColor(Color.GREEN);
img.drawOval(1, 1, img.getWidth() - 2, img.getHeight() - 2);
return img;
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
updateImage();
}
public GreenfootImage getIdleImage() {
return idleImage;
}
public GreenfootImage getActiveImage() {
return activeImage;
}
@Override
public GameWorld getWorld() {
return (GameWorld) super.getWorld();
}
}