1 package de.lathanda.eos.gui.diagram;
3 import java.awt.BasicStroke;
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;
12 import de.lathanda.eos.base.util.GuiToolkit;
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;
46 private Font originalFont;
50 private final LinkedList<Translation> transformations =
new LinkedList<>();
54 private Translation translation =
new Translation();
84 fontScale2pixel = dpi / 72f;
86 setFont(
new Font(Font.SANS_SERIF, Font.PLAIN, 12));
87 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
89 g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
96 public void init(Graphics2D g) {
97 while (!transformations.isEmpty()) {
101 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
103 g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
112 return g.getFontMetrics(font).stringWidth(text) / scale;
120 return g.getFontMetrics(font).getAscent() / scale;
128 return g.getFontMetrics(font).getHeight() / scale;
138 public void drawLine(
float x1,
float y1,
float x2,
float y2) {
139 g.drawLine(xp(x1), yp(y1), xp(x2), yp(y2));
149 g.drawString(text, xp(x), yp(y));
158 this.font = font.deriveFont(font.getSize2D() * fontScale2pixel);
159 g.setFont(this.font);
175 g.setStroke(
new BasicStroke(width * scale));
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));
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));
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);
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));
247 int[] ix =
new int[n];
248 int[] iy =
new int[n];
249 for (
int i = n; i-- > 0;) {
253 g.drawPolygon(ix, iy, n);
256 private int xp(
float x) {
257 return (
int) ((x + translation.x) * scale);
260 private int yp(
float y) {
261 return (
int) ((y + translation.y) * scale);
264 private int wp(
float x,
float w) {
265 return xp(x + w) - xp(x);
268 private int hp(
float y,
float h) {
269 return yp(y + h) - yp(y);
272 private int scale(
float val) {
273 return (
int) (scale * val);
284 public void drawArrow(
float x1,
float y1,
float x2,
float y2,
float size) {
287 float length = (float) Math.sqrt(dx * dx + dy * dy);
288 dx = dx * size / length;
289 dy = dy * size / length;
293 drawLine(x2, y2, xb - dy * 0.5f, yb + dx * 0.5f);
294 drawLine(x2, y2, xb + dy * 0.5f, yb - dx * 0.5f);
301 transformations.push(translation.copy());
308 translation = transformations.pop();
318 translation.add(dx, dy);
327 return (
int) (mm * scale);
336 return pixel / scale;
339 private static class Translation {
344 Translation t =
new Translation();
350 void add(
float dx,
float dy) {
float convertpixel2mm(int pixel)
float stringWidth(String text)
void drawLine(float x1, float y1, float x2, float y2)
void drawEllipse(float x, float y, float width, float height)
void translate(float dx, float dy)
void drawPolygon(float[] x, float[] y, int n)
void fillRect(float x, float y, float width, float height)
void drawRoundRect(float x, float y, float width, float height, float radius)
void setDrawWidth(float width)
void drawRect(float x, float y, float width, float height)
void drawArrow(float x1, float y1, float x2, float y2, float size)
Drawing(float dpi, Graphics2D g)
int convertmm2pixel(float mm)
void drawString(String text, float x, float y)