EOS 2  1.1.0
Einfache Objektbasierte Sprache
Picture.java
gehe zur Dokumentation dieser Datei
1 package de.lathanda.eos.base;
2 
3 import de.lathanda.eos.base.layout.Dimension;
4 import de.lathanda.eos.base.math.Point;
5 import de.lathanda.eos.base.util.GuiToolkit;
6 import de.lathanda.eos.base.layout.Transform;
7 import de.lathanda.eos.game.geom.Shape;
8 
9 //import java.awt.Color;
10 import java.awt.Font;
11 import java.util.Collection;
12 import java.util.Iterator;
13 import java.util.LinkedList;
14 import java.util.List;
15 import java.util.StringTokenizer;
16 
23 public abstract class Picture {
27  protected final double scaleBase;
28  protected double scale;
29  protected double centerX; //[mm]
30  protected double centerY; //[mm]
31  protected double halfwidth; //[mm]
32  protected double halfheight; //[mm]
33 
34  //Pen
35  protected LineDescriptor line;
36  protected FillDescriptor fill;
37 
38  //Text
41  protected double hspace = 1;
42  protected double vspace = 1;
43  protected Font font;
44 
45  public Picture() {
47  scale = 1.0f;
48  centerX = 0;
49  centerY = 0;
50  line = new LineDescriptor();
51  fill = new FillDescriptor();
52  }
57  public double getMinX() {
58  return centerX - halfwidth;
59  }
64  public double getMinY() {
65  return centerY - halfheight;
66  }
71  public double getMaxX() {
72  return centerX + halfwidth;
73  }
78  public double getMaxY() {
79  return centerY + halfheight;
80  }
85  public abstract void applyTransform(Transform tf);
93  public abstract void drawLine(double x1, double y1, double x2, double y2);
99  public final void drawLine(Point a, Point b) {
100  drawLine(a.getX(), a.getY(), b.getX(), b.getY());
101  }
106  public final void drawLine(List<Point> points) {
107  Iterator<Point> i = points.iterator();
108  Point prev = i.next();
109  Point act;
110  while (i.hasNext()) {
111  act = i.next();
112  drawLine(act, prev);
113  prev = act;
114  }
115  }
121  public abstract void drawPolygon(double[] x, double[] y);
126  public final void drawPolygon(Collection<? extends Point> points) {
127  double[] xPoints = new double[points.size()];
128  double[] yPoints = new double[points.size()];
129  int n = 0;
130  for (Point p : points) {
131  xPoints[n] = p.getX();
132  yPoints[n] = p.getY();
133  n++;
134  }
135  drawPolygon(xPoints, yPoints);
136  }
141  public final void drawPolygon(Point[] points) {
142  double[] xPoints = new double[points.length];
143  double[] yPoints = new double[points.length];
144  int n = 0;
145  for (Point p : points) {
146  xPoints[n] = p.getX();
147  yPoints[n] = p.getY();
148  n++;
149  }
150  drawPolygon(xPoints, yPoints);
151  }
159  public abstract void drawRect(double x, double y, double width, double height);
168  public abstract void drawImage(Image image, double x, double y, double width, double height);
177  public abstract void drawImage(Image image, double x, double y, double width, double height, Scaling scale);
187  public abstract void drawImage(Image image, double x, double y, double width, double height, boolean mirror, double angle);
194  public final void drawRect(double width, double height) {
195  drawRect(-width/2,-height/2,width,height);
196  }
203  public void drawEllipse(Point p, double radiusX, double radiusY) {
204  drawEllipse(p.getX(), p.getY(), radiusX, radiusY);
205  }
213  public abstract void drawEllipse(double x, double y, double radiusX, double radiusY);
220  public final void drawEllipse(double radiusX, double radiusY) {
221  drawEllipse(0, 0, radiusX, radiusY);
222  }
229  this.vertical = vertical;
230  this.horizontal = horizontal;
231  }
237  public final void setTextSpacing(double hspace, double vspace) {
238  this.hspace = hspace;
239  this.vspace = vspace;
240  }
245  public final void setFont(Font font) {
246  //Fontsize 20 is about 5mm height
247  this.font = font;
248  }
254  public final void setFont(String fontname, int size) {
255  //Fontsize 20 is about 5mm height
256  font = new Font(fontname, Font.PLAIN, size);
257  }
264  protected abstract void drawStringAt(String text, double x, double y);
270  public void drawString(String text, Shape shape) {
271  drawText(text, shape.getLeft(), shape.getBottom(), shape.getWidth(), shape.getHeight());
272  }
282  public void drawText(List<String> text, double left, double bottom, double width, double height) {
283  drawText(text.toArray(new String[text.size()]), left, bottom, width, height);
284  }
295  public void drawText(String[] text, double left, double bottom, double width, double height) {
296  double lineheight = getStringHeight();
297  double textheight = (lineheight + vspace) * text.length - vspace;
298  double descent = getStringDescent();
299  double y = 0;
300  double dx = left + width / 2;
301  double dy = bottom + height / 2;
302  switch (vertical) {
303  case CENTER:
304  y = textheight/2 - lineheight + descent;
305  break;
306  case LEFT:
307  case TOP:
308  y = height/2 - vspace - lineheight + descent;
309  break;
310  case RIGHT:
311  case BOTTOM:
312  y = -height/2 + textheight - lineheight + vspace + descent;
313  break;
314  }
315  for(String textline: text) {
316  double textwidth = getStringWidth(textline);
317  double x = 0;
318  switch (horizontal) {
319  case CENTER:
320  x = -textwidth/2;
321  break;
322  case LEFT:
323  case TOP:
324  x = -width/2 + hspace;
325  break;
326  case RIGHT:
327  case BOTTOM:
328  x = width/2 - textwidth - hspace;
329  break;
330  }
331  drawStringAt(textline, dx + x, dy +y);
332  y -= lineheight + vspace;
333  }
334  }
343  public final void drawText(String text, double x, double y, double width, double height) {
344  drawText(split(text), x, y, width, height);
345  }
353  public final void drawText(String text, double x, double y) {
354  List<String> list = split(text);
355  drawText(list, x, y, 0, 0);
356  }
362  protected abstract double getStringWidth(String text);
367  protected abstract double getStringHeight();
372  protected abstract double getStringDescent();
378  public Dimension getTextDimension(List<String> text) {
379  Dimension dim = new Dimension();
380  dim.setHeight((getStringHeight() + vspace) * text.size() + vspace);
381  for(String textline: text) {
382  double textwidth = getStringWidth(textline)+2*hspace;
383  if (textwidth > dim.getWidth()) {
384  dim.setWidth(textwidth);
385  }
386  }
387  return dim;
388  }
394  public Dimension getTextDimension(String text) {
395  return getTextDimension(split(text));
396  }
402  private List<String> split(String text) {
403  StringTokenizer st = new StringTokenizer(text, "\n\r\f", false);
404  LinkedList<String> list = new LinkedList<>();
405  while(st.hasMoreTokens()) {
406  list.add(st.nextToken());
407  }
408  return list;
409  }
414  public void setFill(FillDescriptor fill) {
415  this.fill = fill;
416  }
421  public void setLine(LineDescriptor line) {
422  this.line = line;
423  }
428  public void setLineColor(MutableColor c) {
429  line.setColor(c);
431  }
436  public void setLineWidth(double w) {
437  line.setDrawWidth(w);
438  }
443  public void setFillColor(MutableColor c) {
444  fill.setColor(c);
446  }
451  public void drawShape(Shape shape) {
452  shape.draw(this);
453  }
459  public abstract void translate(double dx, double dy);
464  public abstract void rotate(double angle);
468  public abstract void pushTransform();
472  public abstract void restoreTransform();
473 }
void drawEllipse(Point p, double radiusX, double radiusY)
Definition: Picture.java:203
void setLine(LineDescriptor line)
Definition: Picture.java:421
final void setTextSpacing(double hspace, double vspace)
Definition: Picture.java:237
abstract void drawEllipse(double x, double y, double radiusX, double radiusY)
abstract void applyTransform(Transform tf)
void drawText(List< String > text, double left, double bottom, double width, double height)
Definition: Picture.java:282
abstract void drawStringAt(String text, double x, double y)
void setFill(FillDescriptor fill)
Definition: Picture.java:414
final void drawText(String text, double x, double y)
Definition: Picture.java:353
void drawString(String text, Shape shape)
Definition: Picture.java:270
abstract void pushTransform()
abstract double getStringHeight()
void drawText(String[] text, double left, double bottom, double width, double height)
Definition: Picture.java:295
void setLineWidth(double w)
Definition: Picture.java:436
final void drawLine(Point a, Point b)
Definition: Picture.java:99
final void drawRect(double width, double height)
Definition: Picture.java:194
abstract void restoreTransform()
abstract void drawLine(double x1, double y1, double x2, double y2)
Dimension getTextDimension(List< String > text)
Definition: Picture.java:378
final void drawPolygon(Collection<? extends Point > points)
Definition: Picture.java:126
Dimension getTextDimension(String text)
Definition: Picture.java:394
void drawShape(Shape shape)
Definition: Picture.java:451
abstract void drawImage(Image image, double x, double y, double width, double height, Scaling scale)
abstract void drawImage(Image image, double x, double y, double width, double height, boolean mirror, double angle)
final void setFont(String fontname, int size)
Definition: Picture.java:254
void setFillColor(MutableColor c)
Definition: Picture.java:443
abstract void drawImage(Image image, double x, double y, double width, double height)
final void drawPolygon(Point[] points)
Definition: Picture.java:141
abstract double getStringWidth(String text)
final void setTextAlignment(Alignment vertical, Alignment horizontal)
Definition: Picture.java:228
abstract void translate(double dx, double dy)
final void setFont(Font font)
Definition: Picture.java:245
abstract void drawRect(double x, double y, double width, double height)
abstract void drawPolygon(double[] x, double[] y)
final void drawEllipse(double radiusX, double radiusY)
Definition: Picture.java:220
final void drawText(String text, double x, double y, double width, double height)
Definition: Picture.java:343
abstract void rotate(double angle)
abstract double getStringDescent()
final void drawLine(List< Point > points)
Definition: Picture.java:106
void setLineColor(MutableColor c)
Definition: Picture.java:428
Geometrische Transformation.
Definition: Transform.java:13
abstract void draw(Picture picture)
Impressum