EOS 2  1.1.0
Einfache Objektbasierte Sprache
Drawing.java
gehe zur Dokumentation dieser Datei
1 package de.lathanda.eos.gui.diagram;
2 
3 import java.awt.BasicStroke;
4 import java.awt.Color;
5 import java.awt.Font;
6 import java.awt.Graphics2D;
7 import java.awt.RenderingHints;
8 import java.awt.geom.RoundRectangle2D;
9 import java.awt.image.BufferedImage;
10 import java.util.LinkedList;
11 
12 import de.lathanda.eos.base.util.GuiToolkit;
13 
22 public class Drawing {
26  private static final BufferedImage DUMMY = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
30  private static final Graphics2D DUMMY_G = DUMMY.createGraphics();
34  private final float scale;
38  private final float fontScale2pixel;
42  private Font font;
46  private Font originalFont;
50  private final LinkedList<Translation> transformations = new LinkedList<>();
54  private Translation translation = new Translation();
58  private Graphics2D g;
59 
64  public Drawing() {
65  this(GuiToolkit.getScreenResolution(), DUMMY_G);
66  }
67 
73  public Drawing(float dpi) {
74  this(dpi, DUMMY_G);
75  }
76 
82  public Drawing(float dpi, Graphics2D g) {
83  scale = dpi / 25.4f;
84  fontScale2pixel = dpi / 72f;
85  this.g = g;
86  setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
87  g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
88 
89  g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
90  }
91 
96  public void init(Graphics2D g) {
97  while (!transformations.isEmpty()) {
98  popTransform();
99  }
100  this.g = g;
101  g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
102 
103  g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
104  }
105 
111  public float stringWidth(String text) {
112  return g.getFontMetrics(font).stringWidth(text) / scale;
113  }
114 
119  public float getAscent() {
120  return g.getFontMetrics(font).getAscent() / scale;
121  }
122 
127  public float getHeight() {
128  return g.getFontMetrics(font).getHeight() / scale;
129  }
130 
138  public void drawLine(float x1, float y1, float x2, float y2) {
139  g.drawLine(xp(x1), yp(y1), xp(x2), yp(y2));
140  }
141 
148  public void drawString(String text, float x, float y) {
149  g.drawString(text, xp(x), yp(y));
150  }
151 
156  public void setFont(Font font) {
157  originalFont = font;
158  this.font = font.deriveFont(font.getSize2D() * fontScale2pixel);
159  g.setFont(this.font);
160  }
161 
166  public Font getFont() {
167  return originalFont;
168  }
169 
174  public void setDrawWidth(float width) {
175  g.setStroke(new BasicStroke(width * scale));
176  }
177 
182  public void setColor(Color c) {
183  g.setColor(c);
184  }
185 
189  public Color getColor() {
190  return g.getColor();
191  }
192 
200  public void drawEllipse(float x, float y, float width, float height) {
201  g.drawOval(xp(x), yp(y), wp(x, width), hp(y, height));
202  }
203 
211  public void drawRect(float x, float y, float width, float height) {
212  g.drawRect(xp(x), yp(y), wp(x, width), hp(y, height));
213  }
214 
223  public void drawRoundRect(float x, float y, float width, float height, float radius) {
224  RoundRectangle2D roundedRectangle = new RoundRectangle2D.Float(xp(x), yp(y), wp(x, width), hp(y, height),
225  scale(radius), scale(radius));
226  g.draw(roundedRectangle);
227  }
228 
236  public void fillRect(float x, float y, float width, float height) {
237  g.fillRect(xp(x), yp(y), wp(x, width), hp(y, height));
238  }
239 
246  public void drawPolygon(float[] x, float[] y, int n) {
247  int[] ix = new int[n];
248  int[] iy = new int[n];
249  for (int i = n; i-- > 0;) {
250  ix[i] = xp(x[i]);
251  iy[i] = yp(y[i]);
252  }
253  g.drawPolygon(ix, iy, n);
254  }
255 
256  private int xp(float x) {
257  return (int) ((x + translation.x) * scale);
258  }
259 
260  private int yp(float y) {
261  return (int) ((y + translation.y) * scale);
262  }
263 
264  private int wp(float x, float w) {
265  return xp(x + w) - xp(x);
266  }
267 
268  private int hp(float y, float h) {
269  return yp(y + h) - yp(y);
270  }
271 
272  private int scale(float val) {
273  return (int) (scale * val);
274  }
275 
284  public void drawArrow(float x1, float y1, float x2, float y2, float size) {
285  float dx = x2 - x1;
286  float dy = y2 - y1;
287  float length = (float) Math.sqrt(dx * dx + dy * dy);
288  dx = dx * size / length;
289  dy = dy * size / length;
290  float xb = x2 - dx;
291  float yb = y2 - dy;
292  drawLine(x1, y1, x2, y2);
293  drawLine(x2, y2, xb - dy * 0.5f, yb + dx * 0.5f);
294  drawLine(x2, y2, xb + dy * 0.5f, yb - dx * 0.5f);
295  }
296 
300  public void pushTransform() {
301  transformations.push(translation.copy());
302  }
303 
307  public void popTransform() {
308  translation = transformations.pop();
309  }
310 
317  public void translate(float dx, float dy) {
318  translation.add(dx, dy);
319  }
320 
326  public int convertmm2pixel(float mm) {
327  return (int) (mm * scale);
328  }
329 
335  public float convertpixel2mm(int pixel) {
336  return pixel / scale;
337  }
338 
339  private static class Translation {
340  float x;
341  float y;
342 
343  Translation copy() {
344  Translation t = new Translation();
345  t.x = this.x;
346  t.y = this.y;
347  return t;
348  }
349 
350  void add(float dx, float dy) {
351  x += dx;
352  y += dy;
353  }
354  }
355 }
float stringWidth(String text)
Definition: Drawing.java:111
void drawLine(float x1, float y1, float x2, float y2)
Definition: Drawing.java:138
void drawEllipse(float x, float y, float width, float height)
Definition: Drawing.java:200
void translate(float dx, float dy)
Definition: Drawing.java:317
void drawPolygon(float[] x, float[] y, int n)
Definition: Drawing.java:246
void fillRect(float x, float y, float width, float height)
Definition: Drawing.java:236
void drawRoundRect(float x, float y, float width, float height, float radius)
Definition: Drawing.java:223
void drawRect(float x, float y, float width, float height)
Definition: Drawing.java:211
void drawArrow(float x1, float y1, float x2, float y2, float size)
Definition: Drawing.java:284
Drawing(float dpi, Graphics2D g)
Definition: Drawing.java:82
void drawString(String text, float x, float y)
Definition: Drawing.java:148
Impressum