-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLabel.java
More file actions
71 lines (62 loc) · 2.06 KB
/
Label.java
File metadata and controls
71 lines (62 loc) · 2.06 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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Class representing labels
*
* @author Austin
* @version 0
*/
public class Label extends UIBase {
protected String text;
protected int fontSize;
protected Color textColour;
/**
* Constructor for a text label
*
* @param x Top left coordinate x
* @param y Top left coordinate y
* @param width Width of label
* @param height Height of label
* @param colour Background colour of label
* @param text String to display on the label
* @param fontSize Font size of text
* @param textColour Colour of text
*/
public Label(int x, int y, int width, int height, Color colour, String text, int fontSize, Color textColour) {
super(x, y, width, height, colour);
this.text = text;
this.fontSize = fontSize;
this.textColour = textColour;
}
/**
* Update the image
*/
public void act() {
createImage();
}
/**
* Draw the label as a rectangle to the screen
* Display the text in the centre of the rectangle
*/
protected void createImage() {
GreenfootImage image = new GreenfootImage(width, height);
image.setColor(colour);
image.fillRect(0, 0, width, height);
GreenfootImage textImage = new GreenfootImage(text, fontSize, textColour, new Color(0, 0, 0, 0));
image.drawImage(textImage, (getWidth() - textImage.getWidth()) / 2, (getHeight() - textImage.getHeight()) / 2);
setImage(image);
}
/**
* Set the background colour of the label
*
* @param colour The colour to set the background to
*/
public void setColour(Color colour) { this.colour = colour; }
/**
* Set the text the label displays
*
* @param text The text to display
*/
public void setText(String text) { this.text = text; }
// Getters
public String getText() { return text; }
}