-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathImprovedButtonChooser.java
More file actions
36 lines (29 loc) · 897 Bytes
/
Copy pathImprovedButtonChooser.java
File metadata and controls
36 lines (29 loc) · 897 Bytes
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
import greenfoot.Actor;
import greenfoot.Greenfoot;
import greenfoot.GreenfootImage;
import java.awt.*;
import java.util.ArrayList;
public class ImprovedButtonChooser extends Actor {
private ArrayList<String> options;
private int currentIndex = 0;
public ImprovedButtonChooser(ArrayList<String> options, String startingValue) {
this.options = options;
currentIndex = options.indexOf(startingValue);
}
private void incrementIndex() {
if (options.size() > currentIndex + 1) {
currentIndex++;
} else {
currentIndex = 0;
}
}
public String value() {
return options.get(currentIndex);
}
public void act() {
if(Greenfoot.mousePressed(this)) {
incrementIndex();
}
this.setImage( new GreenfootImage(value(), 30, Color.WHITE, null, Color.BLACK) );
}
}