This repository was archived by the owner on Nov 1, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathText.java
More file actions
29 lines (27 loc) · 1.23 KB
/
Text.java
File metadata and controls
29 lines (27 loc) · 1.23 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
import java.awt.*;
/**
* Static helper class that draws text on the screen
*/
public class Text {
private Text() {}
// Draw text from x centered at y
public static void draw(Graphics g, int fontSize, String text, double x, double y) {
g.setFont(FontLoader.getFont(fontSize));
FontMetrics fm = g.getFontMetrics();
g.drawString(text, (int)x, (int)(y - fm.getHeight() / 2 + fm.getAscent()));
}
// Draw text centered at (x, y)
public static void drawCentered(Graphics g, int fontSize, String text, double x, double y) {
g.setFont(FontLoader.getFont(fontSize));
FontMetrics fm = g.getFontMetrics();
g.drawString(text, (int)(x - fm.stringWidth(text) / 2), (int)(y - fm.getHeight() / 2 + fm.getAscent()));
}
// Draw text centered at the provided rectangle
public static void drawCentered(Graphics g, int fontSize, String text, double x, double y, int width, int height) {
drawCentered(g, fontSize, text, x + width / 2, y + height / 2);
}
// Draw text centered at the provided rectangle
public static void drawCentered(Graphics g, int fontSize, String text, Rectangle rect) {
drawCentered(g, fontSize, text, rect.getCenterX(), rect.getCenterY());
}
}