1 package de.lathanda.eos.base;
3 import java.awt.BasicStroke;
4 import java.awt.Component;
6 import java.awt.Graphics2D;
7 import java.awt.Polygon;
8 import java.awt.RenderingHints;
10 import java.awt.geom.AffineTransform;
11 import java.awt.geom.Ellipse2D;
12 import java.awt.geom.Rectangle2D;
13 import java.util.LinkedList;
15 import de.lathanda.eos.base.layout.Transform;
16 import de.lathanda.eos.base.math.Point;
26 private AffineTransform baseTrans;
29 private final LinkedList<Transform> storedTransform;
30 private final LinkedList<AffineTransform> storedAffineTransform;
32 private double gridwidth = 10;
33 private static double GRID_LINE_WIDTH = 0.25;
34 private static double AXIS_LINE_WIDTH = 1;
37 private boolean gridvisible =
true;
43 private Component target;
48 private static double ZOOM = 128d;
49 public Picture2D(
double width,
double height, Component target) {
52 font =
new Font(
"Serif", Font.PLAIN, 12);
54 storedTransform =
new LinkedList<>();
55 storedAffineTransform =
new LinkedList<>();
64 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
65 RenderingHints.VALUE_ANTIALIAS_ON);
67 g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
68 RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
70 g.scale(1/ZOOM, 1/ZOOM);
72 baseTrans = g.getTransform();
79 public final void setSize(
int width,
int height) {
82 this.pCenterX = width/2;
83 this.pCenterY = height/2;
84 this.
halfwidth = pixel2mmInternal(pWidth/2);
93 this.
halfwidth = pixel2mmInternal(pWidth/2);
108 public void drag(
int dx,
int dy) {
109 centerX += pixel2mmInternal(dx);
110 centerY -= pixel2mmInternal(dy);
126 private double pixel2mmInternal(
int pixel) {
150 private int mm2unit(
double mm) {
151 return (
int)mm2unitd(mm);
158 private float mm2unitf(
double mm) {
159 return (
float)mm2unitd(mm);
167 private double mm2unitd(
double mm) {
181 public void drawLine(
double x1,
double y1,
double x2,
double y2) {
183 g.drawLine(mm2unit(x1), -mm2unit(y1), mm2unit(x2), -mm2unit(y2));
188 storedTransform.push(transform);
189 storedAffineTransform.push(g.getTransform());
193 g.setTransform(storedAffineTransform.pop());
194 transform = storedTransform.pop();
199 g.setTransform(baseTrans);
211 g.setTransform(baseTrans);
222 g.setTransform(baseTrans);
234 private boolean applyLine() {
238 g.setStroke(
new BasicStroke(
240 BasicStroke.CAP_ROUND,
241 BasicStroke.JOIN_ROUND,
243 new float[]{mm2unitf(5), mm2unitf(5)},
248 g.setStroke(
new BasicStroke(
250 BasicStroke.CAP_ROUND,
251 BasicStroke.JOIN_ROUND
255 g.setStroke(
new BasicStroke(
257 BasicStroke.CAP_ROUND,
258 BasicStroke.JOIN_ROUND,
260 new float[]{mm2unitf(0.5), mm2unitf(3)},
265 g.setStroke(
new BasicStroke(
267 BasicStroke.CAP_ROUND,
268 BasicStroke.JOIN_ROUND,
270 new float[]{mm2unitf(5), mm2unitf(1),
271 mm2unitf(0.5), mm2unitf(1)},
283 int[] xPoints =
new int[x.length];
284 int[] yPoints =
new int[y.length];
285 for (
int i = 0; i < x.length; i++) {
286 xPoints[i] = mm2unit(x[i]);
287 yPoints[i] = -mm2unit(y[i]);
289 drawShape(
new Polygon(xPoints, yPoints, x.length));
293 public void drawRect(
double x,
double y,
double width,
double height) {
294 drawShape(
new Rectangle2D.Double(
304 public void drawEllipse(
double x,
double y,
double radiusX,
double radiusY) {
305 drawShape(
new Ellipse2D.Double(
307 -mm2unitd(y+radiusY),
317 g.setFont(
font.deriveFont((
float)(
font.getSize()*
scale*ZOOM)));
318 g.drawString(text, mm2unit(x), -mm2unit(y));
323 return pixel2mm(target.getFontMetrics(
font).stringWidth(text));
328 return pixel2mm(target.getFontMetrics(
font).getHeight());
333 return pixel2mm(target.getFontMetrics(
font).getDescent());
342 if (!gridvisible)
return;
345 for(
double x = 0; x <
getMaxX(); x += gridwidth) {
348 for(
double x = 0; x >
getMinX(); x -= gridwidth) {
351 for(
double y = 0; y <
getMaxY(); y += gridwidth) {
354 for(
double y = 0; y >
getMinY(); y -= gridwidth) {
382 this.gridwidth = gridwidth;
406 public void drawImage(
Image image,
double x,
double y,
double width,
double height) {
437 double targetProportion = width / height;
438 double s = imageProportion / targetProportion;
445 -mm2unit(y+height*(0.5+0.5/s)),
447 -mm2unit(y+height*(0.5-0.5/s)),
457 mm2unit(x+width*(0.5-s/2)),
459 mm2unit(x+width*(0.5+s/2)),
501 public void drawImage(
Image image,
double x,
double y,
double width,
double height,
boolean mirror,
503 AffineTransform at = g.getTransform();
504 g.translate(mm2unit(x+width/2), -mm2unit(y+height/2));
532 private void drawShape(
Shape shape) {
542 Shape old = g.getClip();
543 double linegap = mm2unitd(2);
545 g.setStroke(
new BasicStroke(mm2unitf(0.25f)));
546 Rectangle2D border = shape.getBounds2D();
547 for(
double y = border.getMinY() + border.getWidth() % linegap; y < border.getMaxY(); y += linegap ) {
548 g.drawLine((
int)border.getMinX(), (
int)y, (
int)border.getMaxX(), (
int)y);
555 Shape old = g.getClip();
556 double checksize = mm2unitd(5);
558 g.setStroke(
new BasicStroke(mm2unitf(0.25f)));
559 Rectangle2D border = shape.getBounds2D();
560 for(
double y = border.getMinY() + border.getHeight() % checksize; y < border.getMaxY(); y += checksize ) {
561 g.drawLine((
int)border.getMinX(), (
int)y, (
int)border.getMaxX(), (
int)y);
563 for(
double x = border.getMinX() + border.getWidth() % checksize; x < border.getMaxX(); x += checksize ) {
564 g.drawLine((
int)x, (
int)border.getMinY(), (
int)x, (
int)border.getMaxY());
void setDrawWidth(double width)
void setColor(MutableColor c)
static final MutableColor GRID
double pixel2mm(int pixel)
void setGridColor(MutableColor color)
void drawImage(Image image, double x, double y, double width, double height)
void drawStringAt(String text, double x, double y)
void rotate(double angle)
void setGridWidth(double gridwidth)
Picture2D(double width, double height, Component target)
final void setSize(int width, int height)
void translate(double dx, double dy)
void drawLine(double x1, double y1, double x2, double y2)
void applyTransform(Transform tf)
void setGridVisible(boolean b)
Point pointFromPixel(int x, int y)
void drawPolygon(double[] x, double[] y)
void setScale(double scale)
void drawImage(Image image, double x, double y, double width, double height, boolean mirror, double angle)
MutableColor getGridColor()
void drawCoordinateSystem()
void setGraphics(Graphics2D g)
void drag(int dx, int dy)
double getStringWidth(String text)
double getStringDescent()
void drawImage(Image image, double x, double y, double width, double height, Scaling scale)
void drawRect(double x, double y, double width, double height)
void drawEllipse(double x, double y, double radiusX, double radiusY)
void setCenter(double x, double y)
void setLine(LineDescriptor line)
abstract void draw(Picture picture)