GraphicsApp API
Eine überarbeitete und angepasste Variante der originalen GraphicsApp-Umgebung
DebugInfo.java
gehe zur Dokumentation dieser Datei
1package de.ur.mi.oop.utils;
2
3import de.ur.mi.oop.colors.Color;
4import de.ur.mi.oop.graphics.Label;
5import de.ur.mi.oop.graphics.Rectangle;
6
7import java.util.LinkedHashMap;
8
9/**
10 * Mit dieser Klasse können Sie ein semi-transparentes Feld mit Debug-Informationen in Ihrer GraphicsApp anzeigen. Die angezeigten Informationen können
11 * von beliebigen Stellen der Anwendung gesetzt und aktualisiert werden. Angezeigt werden jeweils eine Beschreibung und ein konkreter Wert. Beim ersten
12 * Setzen einer Beschreibung wird die Information im Feld ergänzt, wiederholtes Setzen der gleichen Beschreibung (mit anderen Werten) führt zu Aktualisierung
13 * der angezeigten Informationen. Der Aufruf der statische draw-Methode zeichnet das Feld. Fügen Sie diesem AM ENDE der draw-Methode Ihrer GraphicsApp ein.
14 * <p>
15 * Das Feld ist standardmäßig ausgeblendet und muss über den Aufruf der Methode DebugInfo.show() eingeblendet werden.
16 * <p>
17 * Öffentliche statische Methode der Klasse
18 * <p>
19 * DebugInfo.show() => Blendet das Feld ein
20 * DebugInfo.hide() => Blendet das Feld aus
21 * DebugInfo.draw() => Zeichnet das Feld
22 * DebugInfo.set(String name, String value) => Fügt den Wert (value) mit der Beschreibung (name) im Feld ein bzw. aktualisiert den Wert
23 * DebugInfo.set(String name, boolean value) => siehe DebugInfo.set(String name, String value)
24 * DebugInfo.set(String name, boolean short) => siehe DebugInfo.set(String name, String value)
25 * DebugInfo.set(String name, boolean int) => siehe DebugInfo.set(String name, String value)
26 * DebugInfo.set(String name, boolean float) => siehe DebugInfo.set(String name, String value)
27 * DebugInfo.set(String name, boolean double) => siehe DebugInfo.set(String name, String value)
28 * DebugInfo.set(String name, boolean char) => siehe DebugInfo.set(String name, String value)
29 * DebugInfo.remove(String name) => Entfernt Wert und Beschreibung des Eintrags mit der Beschreibung (name) aus dem Feld
30 * <p>
31 * Beispiel:
32 * <p>
33 * int frame;
34 * <p>
35 * public void initialize() {
36 * DebugInfo.show();
37 * }
38 * <p>
39 * public void draw() {
40 * // Zeichnen der App-Bestandteile
41 * // ...
42 * <p>
43 * // Anzeige der Anzahl der gezeichneten Frames
44 * DebugInfo.set("FRAMES DRAWN", frame);
45 * DebugInfo.draw();
46 * frame++;
47 * }
48 */
49
50public class DebugInfo {
51
52 private static final int X_POSITION = 5;
53 private static final int Y_POSITION = 5;
54 private static final int MIN_WIDTH = 1;
55 private static final int MIN_HEIGHT = 1;
56 private static final int PADDING = 10;
57 private static final Color BACKGROUND_COLOR = new Color(250, 250, 250, 150);
58 private static final Color BORDER_COLOR = new Color(200, 200, 200, 150);
59 private static final float BORDER_WEIGHT = 1;
60 private static final String FONT_NAME = "Monospaced";
61 private static final Color FONT_COLOR = new Color(30, 30, 30);
62 private static final int FONT_SIZE = 10;
63 private static final String TITLE = "### Debug Info ###";
64
65 private static DebugInfo instance;
66
67 private final Rectangle background;
68 private final LinkedHashMap<String, Label> values;
69 private boolean isVisible;
70
71 private DebugInfo() {
72 background = new Rectangle(X_POSITION, Y_POSITION, MIN_WIDTH, MIN_HEIGHT, BACKGROUND_COLOR);
73 background.setBorder(BORDER_COLOR, BORDER_WEIGHT);
74 values = new LinkedHashMap<>();
75 isVisible = false;
76 updateOrAddLabel(TITLE, "");
77 pack();
78 }
79
80 private static DebugInfo getInstance() {
81 if (instance == null) {
82 instance = new DebugInfo();
83 }
84 return instance;
85 }
86
87 private void updateOrAddLabel(String name, String value) {
88 Label label = values.get(name);
89 if (label == null) {
90 label = createLabel(name, value);
91 values.put(name, label);
92 } else {
93 label.setText(name + ": " + value);
94 }
95 }
96
97 private Label createLabel(String name, String value) {
98 Label label = new Label(0, 0, name + ": " + value, FONT_COLOR);
99 if (name.equals(TITLE)) {
100 label.setText(TITLE);
101 }
102 label.setFont(FONT_NAME);
103 label.setFontSize(FONT_SIZE);
104 return label;
105 }
106
107 private void pack() {
108 float minHeight = 0;
109 float minWidth = 0;
110 float x = background.getXPos() + PADDING;
111 float y = background.getYPos() + PADDING;
112 for (Label label : values.values()) {
113 label.setPosition(x, y);
114 y += FONT_SIZE + PADDING;
115 minHeight += FONT_SIZE + PADDING;
116 if (minWidth < label.getWidthEstimate()) {
117 minWidth = label.getWidthEstimate();
118 }
119 }
120 background.setWidth(minWidth + 2 * PADDING);
121 background.setHeight(minHeight - FONT_SIZE);
122 }
123
124 private void setValue(String name, String value) {
125 updateOrAddLabel(name, value);
126 pack();
127 }
128
129 private void removeValue(String name) {
130 values.remove(name);
131 pack();
132 }
133
134 private void showInfo() {
135 isVisible = true;
136 }
137
138 public void hideInfo() {
139 isVisible = false;
140 }
141
142 public void drawInfo() {
143 if (!isVisible) {
144 return;
145 }
146 background.draw();
147 for (Label label : values.values()) {
148 label.draw();
149 }
150 }
151
152 public static void set(String name, String value) {
153 getInstance().setValue(name, value);
154 }
155
156 public static void set(String name, boolean value) {
157 set(name, String.valueOf(value));
158 }
159
160 public static void set(String name, short value) {
161 set(name, String.valueOf(value));
162 }
163
164 public static void set(String name, int value) {
165 set(name, String.valueOf(value));
166 }
167
168 public static void set(String name, float value) {
169 set(name, String.valueOf(value));
170 }
171
172 public static void set(String name, double value) {
173 set(name, String.valueOf(value));
174 }
175
176 public static void set(String name, char value) {
177 set(name, String.valueOf(value));
178 }
179
180 public static void remove(String name) {
181 getInstance().removeValue(name);
182 }
183
184 public static void show() {
185 getInstance().showInfo();
186 }
187
188 public static void hide() {
189 getInstance().hideInfo();
190 }
191
192 public static void draw() {
193 getInstance().drawInfo();
194 }
195
196}
void setBorder(Color color, float weight)
void setFontSize(int fontSize)
Definition: Label.java:57
void setText(String text)
Definition: Label.java:73
void setFont(String font)
Definition: Label.java:65