forked from RalfsGames/antGreenfoot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Counter.java
77 lines (69 loc) · 1.43 KB
/
Counter.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
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
/**
* Counter that displays some taxt and a number.
*
* @author Michael Kölling
* @version 1.1
*/
public class Counter extends Actor
{
private int value = 0;
private String text;
/**
* Create a counter without a text prefix, initialized to zero.
*/
public Counter()
{
this("");
}
/**
* Create a counter with a given text prefix, initialized to zero.
*/
public Counter(String prefix)
{
text = prefix;
int imageWidth= (text.length() + 2) * 10;
setImage(new GreenfootImage(imageWidth, 16));
updateImage();
}
/**
* Increment the counter value by one.
*/
public void increment()
{
value++;
updateImage();
}
/**
* Decrement the counter value by one
*/
public void decrement() {
decrement(1);
}
public void decrement(int v){
value -= v;
updateImage();
}
/**
* @return
*/
public int getValue() {
return value;
}
/**
* @param v
*/
public void setValue(int v) {
value = v;
updateImage();
}
/**
* Show the current text and count on this actor's image.
*/
private void updateImage()
{
GreenfootImage image = getImage();
image.clear();
image.drawString(text + value, 1, 12);
}
}