GraphicsApp API
Eine überarbeitete und angepasste Variante der originalen GraphicsApp-Umgebung
Label.java
gehe zur Dokumentation dieser Datei
1package de.ur.mi.oop.graphics;
2
3import de.ur.mi.oop.colors.Color;
4import de.ur.mi.oop.fonts.FontHelper;
5
6import java.awt.*;
7
8/**
9 * Die Klasse Label ist ein grafisches Objekt, das einen Text darstellt.
10 */
11public class Label extends GraphicsObject implements RotatableGraphicsObject {
12
13 private double rotationAngle;
14
15 private static final int DEFAULT_FONT_SIZE = 12;
16 private static final String DEFAULT_FONT = "SansSerif";
17
18 private String text;
19 private String font;
20 private int fontSize;
21
22 /**
23 * Konstruiert ein neues Label, das an den Koordinaten x und y mit
24 * angegebenem Text und Schriftfarbe.
25 *
26 * @param x Die x-Position des Labels in Pixel
27 * @param y Die y-Position des Labels in Pixel
28 * @param text Der Text des Labels
29 */
30 public Label(float x, float y, String text) {
31 super(x, y);
32 this.text = text;
33 this.fontSize = DEFAULT_FONT_SIZE;
34 this.font = DEFAULT_FONT;
36 }
37
38 /**
39 * Konstruiert ein neues Label, das an den Koordinaten x und y mit
40 * angegebenem Text und Schriftfarbe.
41 *
42 * @param x Die x-Position des Labels in Pixel
43 * @param y y
44 * Die y-Position ds Labels in Pixel
45 * @param text Der Text des Labels
46 * @param fontColor Die Farbe des Labels
47 */
48 public Label(float x, float y, String text, Color fontColor) {
49 this(x, y, text);
50 this.setColor(fontColor);
51 }
52
53 public int getFontSize() {
54 return fontSize;
55 }
56
57 public void setFontSize(int fontSize) {
58 this.fontSize = fontSize;
59 }
60
61 public String getFont() {
62 return font;
63 }
64
65 public void setFont(String font) {
66 this.font = font;
67 }
68
69 public String getText() {
70 return this.text;
71 }
72
73 public void setText(String text) {
74 this.text = text;
75 }
76
77 /**
78 * Gibt die geschätzte Breite des Labels auf dem Bildschirm zurück (in Pixel)
79 *
80 */
81 public int getWidthEstimate() {
82 FontMetrics metrics = FontHelper.getFontMetrics(font, fontSize);
83 return metrics.stringWidth(text);
84 }
85
86 /**
87 * Gibt die geschätzte Höhe des Labels auf dem Bildschirm zurück (in Pixel)
88 *
89 */
90 public int getHeightEstimate() {
91 FontMetrics metrics = FontHelper.getFontMetrics(font, fontSize);
92 return metrics.getHeight();
93 }
94
95 @Override
97 return new Point(this.getXPos() + (this.getWidthEstimate()/2.0f), this.getYPos() - (this.getHeightEstimate()/2.0f));
98 }
99
100 public double getRotationAngle() {
101 return rotationAngle;
102 }
103
104 /**
105 * Liefert den aktuellen Rotationswinkel des Objects
106 *
107 * @return Aktueller Rotationswinkel in Grad
108 */
110 return Math.toRadians(rotationAngle);
111 }
112
113 /**
114 * Setzt den Rotationswinkel des Objekts
115 *
116 * @param angle Neuer Rotationswinkel in Grad
117 */
118 public void setRotationAngle(double angle) {
119 if (angle >= MIN_ROTATION_ANGLE && angle <= MAX_ROTATION_ANGLE) {
120 rotationAngle = angle;
121 }
122 }
123
124}
static FontMetrics getFontMetrics(String fontName, int fontSize)
Definition: FontHelper.java:9
Label(float x, float y, String text)
Definition: Label.java:30
void setRotationAngle(double angle)
Definition: Label.java:118
void setFontSize(int fontSize)
Definition: Label.java:57
Label(float x, float y, String text, Color fontColor)
Definition: Label.java:48
void setText(String text)
Definition: Label.java:73
void setFont(String font)
Definition: Label.java:65
double getRotationAngleInRadians()
Definition: Label.java:109