-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathLabel.java
61 lines (55 loc) · 1.86 KB
/
Label.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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Simple notification label.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Label extends Actor
{
private boolean mouseDown = false;
private boolean mouseOver;
private String text = "Label";
public Label() {
draw(text, 20, Color.BLACK, new Color(0, 0, 0, 0));
}
public Label(String text)
{
this.text = text;
draw(text, 20, Color.BLACK, new Color(0, 0, 0, 0));
}
private void draw(String text, int fontsize, Color fontColor, Color bgColor) {
// create the text image
GreenfootImage txtImg = new GreenfootImage(text, fontsize, fontColor, bgColor);
// create the base image
GreenfootImage img = new GreenfootImage(txtImg.getWidth()+20, txtImg.getHeight()+10);
img.setColor(bgColor);
img.fill();
// draw text image on base image
img.drawImage(txtImg, 10, 5);
setImage(img);
}
public void act() {
if (!mouseDown && Greenfoot.mousePressed(this)) {
mouseDown = true;
}
if (mouseDown && Greenfoot.mouseClicked(this)) {
mouseDown = false; // and here
}
if (!mouseOver && Greenfoot.mouseMoved(this))
{
mouseOver = true;
draw(text, 20, Color.RED, new Color(0, 0, 0, 0));
}
if (mouseOver && Greenfoot.mouseMoved(null) && ! Greenfoot.mouseMoved(this))
{
mouseOver = false;
draw(text, 20, Color.BLACK, new Color(0, 0, 0, 0));
}
}
public void setText(String text)
{
this.text = text;
draw(text, 20, Color.BLACK, new Color(0, 0, 0, 0));
}
}