Refactorings

This commit is contained in:
ngb
2022-07-28 12:25:56 +02:00
parent c93a203ab9
commit b0353c53a0
9 changed files with 48 additions and 52 deletions

View File

@@ -351,7 +351,7 @@ public class TurtleLayer extends Layer {
} }
graphics.fill(shape); graphics.fill(shape);
graphics.setColor(Color.BLACK.getJavaColor()); graphics.setColor(Color.BLACK.getJavaColor());
graphics.setStroke(createStroke()); graphics.setStroke(getStroke());
graphics.draw(shape); graphics.draw(shape);
} }
@@ -367,7 +367,7 @@ public class TurtleLayer extends Layer {
if( penDown && strokeColor != null ) { if( penDown && strokeColor != null ) {
drawing.setColor(strokeColor.getJavaColor()); drawing.setColor(strokeColor.getJavaColor());
drawing.setStroke(createStroke()); drawing.setStroke(getStroke());
drawing.drawLine((int) positionStart.x, (int) positionStart.y, (int) position.x, (int) position.y); drawing.drawLine((int) positionStart.x, (int) positionStart.y, (int) position.x, (int) position.y);
} }
} }
@@ -420,7 +420,7 @@ public class TurtleLayer extends Layer {
if( penDown && strokeColor != null ) { if( penDown && strokeColor != null ) {
drawing.setColor(strokeColor.getJavaColor()); drawing.setColor(strokeColor.getJavaColor());
drawing.setStroke(createStroke()); drawing.setStroke(getStroke());
drawing.drawLine((int) x, (int) y, (int) position.x, (int) position.y); drawing.drawLine((int) x, (int) y, (int) position.x, (int) position.y);
} }
} }

View File

@@ -179,6 +179,17 @@ public abstract class FilledShape extends StrokedShape {
fill = null; fill = null;
} }
public Paint getPaint() {
if( fill != null ) {
return fill;
} else if( fillColor != null && fillColor.getAlpha() > 0 ) {
return fillColor;
} else {
return null;
}
}
/** /**
* Hilfsmethode für Unterklassen, um die angegebene Form mit der aktuellen * Hilfsmethode für Unterklassen, um die angegebene Form mit der aktuellen
* Füllung auf den Grafik-Kontext zu zeichnen. Die Methode verändert * Füllung auf den Grafik-Kontext zu zeichnen. Die Methode verändert

View File

@@ -14,7 +14,7 @@ import java.awt.geom.Point2D;
* <p> * <p>
* Neben den abstrakten Methoden implementieren Unterklassen mindestens zwei * Neben den abstrakten Methoden implementieren Unterklassen mindestens zwei
* Konstruktoren. Einen Konstruktor, der die Form mit vom Nutzer gegebenen * Konstruktoren. Einen Konstruktor, der die Form mit vom Nutzer gegebenen
* Parametern initialisiert und einen, der die Werten einer anderen Form * Parametern initialisiert und einen, der die Werte einer anderen Form
* desselben Typs übernimmt. In der Klasse {@link Circle} sind die Konstruktoren * desselben Typs übernimmt. In der Klasse {@link Circle} sind die Konstruktoren
* beispielsweise so implementiert: * beispielsweise so implementiert:
* *
@@ -191,6 +191,7 @@ public abstract class Shape extends FilledShape {
/** /**
* Dreht die Form um den angegebenen Winkel um ihren Ankerpunkt. * Dreht die Form um den angegebenen Winkel um ihren Ankerpunkt.
*
* @param angle Drehwinkel in Grad. * @param angle Drehwinkel in Grad.
*/ */
public void rotate( double angle ) { public void rotate( double angle ) {
@@ -227,6 +228,7 @@ public abstract class Shape extends FilledShape {
public double getScale() { public double getScale() {
return scale; return scale;
} }
public void scale( double factor ) { public void scale( double factor ) {
scale = factor; scale = factor;
} }
@@ -268,28 +270,25 @@ public abstract class Shape extends FilledShape {
} }
/** /**
* Bestimmt die relativen Koordinaten des angegebenen Ankerpunkt basierend * Bestimmt die relativen Koordinaten des angegebenen Ankerpunkts basierend
* auf der angegebenen Breite und Höhe des umschließenden Rechtecks. * auf der angegebenen Breite und Höhe des umschließenden Rechtecks.
* <p> * <p>
* Die Koordinaten des Ankerpunkt werden relativ zur oberen linken Ecke des * Die Koordinaten des Ankerpunktes werden relativ zur oberen linken Ecke
* Rechtecks mit der Breite {@code width} und der Höhe {@code height} * des Rechtecks mit der Breite {@code width} und der Höhe {@code height}
* bestimmt. * bestimmt.
* *
* @param width Breite des umschließdenden Rechtecks. * @param width Breite des umschließenden Rechtecks.
* @param height Höhe des umschließdenden Rechtecks. * @param height Höhe des umschließenden Rechtecks.
* @param anchor Gesuchter Ankerpunkt. * @param anchor Gesuchter Ankerpunkt.
* @return Ein {@link Point2D} mit den relativen Koordinaten. * @return Ein {@link Point2D} mit den relativen Koordinaten.
*/ */
protected static Point2D.Double getAnchorPoint( double width, double height, Options.Direction anchor ) { public static Point2D.Double getAnchorPoint( double width, double height, Options.Direction anchor ) {
double wHalf = width * .5, hHalf = height * .5; double wHalf = width * .5, hHalf = height * .5;
// anchor == CENTER return new Point2D.Double(
Point2D.Double anchorPoint = new Point2D.Double(
wHalf + wHalf * anchor.x, wHalf + wHalf * anchor.x,
hHalf + hHalf * anchor.y hHalf + hHalf * anchor.y
); );
return anchorPoint;
} }
/** /**
@@ -303,12 +302,10 @@ public abstract class Shape extends FilledShape {
double wHalf = getWidth() * .5, hHalf = getHeight() * .5; double wHalf = getWidth() * .5, hHalf = getHeight() * .5;
// anchor == CENTER // anchor == CENTER
Point2D.Double anchorPoint = new Point2D.Double( return new Point2D.Double(
wHalf * (anchor.x - this.anchor.x), wHalf * (anchor.x - this.anchor.x),
hHalf * (anchor.y - this.anchor.y) hHalf * (anchor.y - this.anchor.y)
); );
return anchorPoint;
} }
/** /**
@@ -384,19 +381,10 @@ public abstract class Shape extends FilledShape {
* zurück. Intern werden die AWT Shapes benutzt, um sie auf den * zurück. Intern werden die AWT Shapes benutzt, um sie auf den
* {@link Graphics2D Grafikkontext} zu zeichnen. * {@link Graphics2D Grafikkontext} zu zeichnen.
* <p> * <p>
* Da die AWT-Shape bei jedem Zeichnen (also mindestens einmal pro Frame) * Wenn diese Form nicht durch eine AWT-Shape dargestellt wird, kann die
* benötigt wird, wird das aktuelle Shape-Objekt in {@link #awtShape}
* zwischengespeichert. Bei Änderungen der Objekteigenschaften muss daher
* intern {@link #invalidate()} aufgerufen werden, damit beim nächsten
* Aufruf von {@link #draw(Graphics2D)} die Shape mit einem Aufurf von
* {@code getShape()} neu erzeugt wird. Unterklassen können aber auch die
* zwischengespeicherte Shape direkt modifizieren, um eine Neugenerierung zu
* vermeiden.
* <p>
* Wenn diese Form nicht durch eine AWT-Shape dargstellt wird, kann die
* Methode {@code null} zurückgeben. * Methode {@code null} zurückgeben.
* *
* @return Eine Java-AWT {@code Shape} die diess Form repräsentiert oder * @return Eine Java-AWT {@code Shape} die diese Form repräsentiert oder
* {@code null}. * {@code null}.
*/ */
public abstract java.awt.Shape getShape(); public abstract java.awt.Shape getShape();

View File

@@ -7,6 +7,7 @@ import java.awt.geom.AffineTransform;
import java.awt.geom.Path2D; import java.awt.geom.Path2D;
import java.awt.geom.Point2D; import java.awt.geom.Point2D;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@@ -35,7 +36,7 @@ import java.util.List;
* {@link #arrangeInGrid(int, Options.Direction, double, int)} und * {@link #arrangeInGrid(int, Options.Direction, double, int)} und
* {@link #align(Options.Direction)} verwendet werden, die jeweils die Position * {@link #align(Options.Direction)} verwendet werden, die jeweils die Position
* der Formen in der Gruppe ändern und nicht die Position der Gruppe selbst (so * der Formen in der Gruppe ändern und nicht die Position der Gruppe selbst (so
* wie z.B. {@link #alignTo(Shape, Options.Direction)}. * wie z.B. {@link #alignTo(Shape, Options.Direction)}).
*/ */
public class ShapeGroup extends Shape { public class ShapeGroup extends Shape {
@@ -43,7 +44,7 @@ public class ShapeGroup extends Shape {
public static final int ARRANGE_COLS = 1; public static final int ARRANGE_COLS = 1;
private List<Shape> shapes; private final List<Shape> shapes;
private double groupWidth = -1.0; private double groupWidth = -1.0;
@@ -51,7 +52,7 @@ public class ShapeGroup extends Shape {
public ShapeGroup() { public ShapeGroup() {
super(); super();
shapes = new ArrayList<>(10); shapes = Collections.synchronizedList(new ArrayList<>(10));
} }
public ShapeGroup( double x, double y ) { public ShapeGroup( double x, double y ) {
@@ -62,9 +63,7 @@ public class ShapeGroup extends Shape {
public ShapeGroup( double x, double y, Shape... shapes ) { public ShapeGroup( double x, double y, Shape... shapes ) {
super(x, y); super(x, y);
this.shapes = new ArrayList<>(shapes.length); this.shapes = new ArrayList<>(shapes.length);
for( Shape pShape : shapes ) { Collections.addAll(this.shapes, shapes);
this.shapes.add(pShape);
}
} }
public Shape copy() { public Shape copy() {
@@ -99,9 +98,11 @@ public class ShapeGroup extends Shape {
public <ShapeType extends Shape> List<ShapeType> getShapes( Class<ShapeType> typeClass ) { public <ShapeType extends Shape> List<ShapeType> getShapes( Class<ShapeType> typeClass ) {
LinkedList<ShapeType> list = new LinkedList<>(); LinkedList<ShapeType> list = new LinkedList<>();
for( Shape s : shapes ) { synchronized( shapes ) {
if( typeClass.isInstance(s) ) { for( Shape shape : shapes ) {
list.add((ShapeType) s); if( typeClass.isInstance(shape) ) {
list.add(typeClass.cast(shape));
}
} }
} }
return list; return list;
@@ -170,19 +171,15 @@ public class ShapeGroup extends Shape {
int rows, cols; int rows, cols;
if( mode == ARRANGE_ROWS ) { if( mode == ARRANGE_ROWS ) {
rows = n; rows = n;
cols = (int) ceil(shapes.size() / n); cols = (int) ceil(shapes.size() / (double)n);
} else { } else {
cols = n; cols = n;
rows = (int) ceil(shapes.size() / n); rows = (int) ceil(shapes.size() / (double)n);
} }
// Calculate grid cell size // Calculate grid cell size
double maxHeight = shapes.stream().mapToDouble( double maxHeight = shapes.stream().mapToDouble(Shape::getHeight).reduce(0.0, Double::max);
( s ) -> s.getHeight() double maxWidth = shapes.stream().mapToDouble(Shape::getWidth).reduce(0.0, Double::max);
).reduce(0.0, Double::max);
double maxWidth = shapes.stream().mapToDouble(
( s ) -> s.getWidth()
).reduce(0.0, Double::max);
double halfHeight = maxHeight * .5; double halfHeight = maxHeight * .5;
double halfWidth = maxWidth * .5; double halfWidth = maxWidth * .5;

View File

@@ -197,7 +197,7 @@ public abstract class StrokedShape extends Constants implements Drawable {
* *
* @return Ein {@code Stroke} mit den passenden Kontureigenschaften. * @return Ein {@code Stroke} mit den passenden Kontureigenschaften.
*/ */
protected Stroke createStroke() { public Stroke getStroke() {
// TODO: Used global cached Stroke Objects? // TODO: Used global cached Stroke Objects?
if( stroke == null ) { if( stroke == null ) {
switch( strokeType ) { switch( strokeType ) {
@@ -241,7 +241,7 @@ public abstract class StrokedShape extends Constants implements Drawable {
if( strokeColor != null && strokeColor.getAlpha() > 0 if( strokeColor != null && strokeColor.getAlpha() > 0
&& strokeWeight > 0.0 ) { && strokeWeight > 0.0 ) {
graphics.setColor(strokeColor.getJavaColor()); graphics.setColor(strokeColor.getJavaColor());
graphics.setStroke(createStroke()); graphics.setStroke(getStroke());
graphics.draw(shape); graphics.draw(shape);
} }
} }

View File

@@ -288,7 +288,7 @@ public class BarChart extends Rectangle {
barY += barHeight + gap; barY += barHeight + gap;
} }
graphics.setColor(getStrokeColor().getJavaColor()); graphics.setColor(getStrokeColor().getJavaColor());
graphics.setStroke(createStroke()); graphics.setStroke(getStroke());
if( inverted ) { if( inverted ) {
graphics.drawLine((int) width, 0, (int) width, (int) height); graphics.drawLine((int) width, 0, (int) width, (int) height);
} else { } else {
@@ -360,7 +360,7 @@ public class BarChart extends Rectangle {
} }
graphics.setColor(getStrokeColor().getJavaColor()); graphics.setColor(getStrokeColor().getJavaColor());
graphics.setStroke(createStroke()); graphics.setStroke(getStroke());
if( inverted ) { if( inverted ) {
graphics.drawLine(0, 0, (int) width, 0); graphics.drawLine(0, 0, (int) width, 0);
} else { } else {

View File

@@ -52,7 +52,7 @@ public class ChartAxes extends Rectangle {
} }
graphics.setColor(strokeColor.getJavaColor()); graphics.setColor(strokeColor.getJavaColor());
graphics.setStroke(createStroke()); graphics.setStroke(getStroke());
graphics.drawLine(0, (int)(height), (int)((xMax-xMin) * xUnit), (int)(height)); graphics.drawLine(0, (int)(height), (int)((xMax-xMin) * xUnit), (int)(height));
graphics.drawLine(0, (int)(height), 0, (int)(height - (yMax-yMin) * yUnit)); graphics.drawLine(0, (int)(height), 0, (int)(height - (yMax-yMin) * yUnit));
if( showArrows ) { if( showArrows ) {

View File

@@ -91,7 +91,7 @@ public class LineChart extends Rectangle {
for( ChartValue lcv : val ) { for( ChartValue lcv : val ) {
if( drawLines && lastLcv != null ) { if( drawLines && lastLcv != null ) {
graphics.setColor(getStrokeColor().getJavaColor()); graphics.setColor(getStrokeColor().getJavaColor());
graphics.setStroke(createStroke()); graphics.setStroke(getStroke());
graphics.drawLine((int)(lastLcv.getX()*xUnit), (int)(height - lastLcv.getValue()*yUnit), (int)(lcv.getX()*xUnit), (int)(height - lcv.getValue()*yUnit)); graphics.drawLine((int)(lastLcv.getX()*xUnit), (int)(height - lastLcv.getValue()*yUnit), (int)(lcv.getX()*xUnit), (int)(height - lcv.getValue()*yUnit));
drawDot(graphics, lastLcv, xUnit, yUnit); drawDot(graphics, lastLcv, xUnit, yUnit);
} }

View File

@@ -326,7 +326,7 @@ public class PieChart extends Circle {
} }
graphics.setColor(getStrokeColor().getJavaColor()); graphics.setColor(getStrokeColor().getJavaColor());
graphics.setStroke(createStroke()); graphics.setStroke(getStroke());
graphics.drawOval(0, 0, (int) (radius * 2), (int) (radius * 2)); graphics.drawOval(0, 0, (int) (radius * 2), (int) (radius * 2));
graphics.setTransform(originalTransform); graphics.setTransform(originalTransform);