-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedLabel.java
More file actions
47 lines (41 loc) · 1.64 KB
/
LinkedLabel.java
File metadata and controls
47 lines (41 loc) · 1.64 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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.function.Supplier;
/**
* Class representing linked labels
*
* @author Austin
* @version 0
*/
public class LinkedLabel extends Label
{
private Supplier<String> supplier;
/**
* Constructor for a label linked to a supplier
*
* @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 supplier A string supplier that returns a string determined by a lambda function
* @param fontSize Font size of text
* @param textColour Colour of text
*/
public LinkedLabel(int x, int y, int width, int height, Color colour, Supplier<String> supplier, int fontSize, Color textColour) {
super(x, y, width, height, colour, "", fontSize, textColour);
this.supplier = supplier;
createImage();
}
/**
* Draw the label as a rectangle to the screen
* Display the text from the supplier 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(supplier.get(), fontSize, textColour, new Color(0, 0, 0, 0));
image.drawImage(textImage, (getWidth() - textImage.getWidth()) / 2, (getHeight() - textImage.getHeight()) / 2);
setImage(image);
}
}