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.setColor(Color.BLACK.getJavaColor());
graphics.setStroke(createStroke());
graphics.setStroke(getStroke());
graphics.draw(shape);
}
@@ -367,7 +367,7 @@ public class TurtleLayer extends Layer {
if( penDown && strokeColor != null ) {
drawing.setColor(strokeColor.getJavaColor());
drawing.setStroke(createStroke());
drawing.setStroke(getStroke());
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 ) {
drawing.setColor(strokeColor.getJavaColor());
drawing.setStroke(createStroke());
drawing.setStroke(getStroke());
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;
}
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
* Füllung auf den Grafik-Kontext zu zeichnen. Die Methode verändert

View File

@@ -14,7 +14,7 @@ import java.awt.geom.Point2D;
* <p>
* Neben den abstrakten Methoden implementieren Unterklassen mindestens zwei
* 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
* beispielsweise so implementiert:
*
@@ -191,6 +191,7 @@ public abstract class Shape extends FilledShape {
/**
* Dreht die Form um den angegebenen Winkel um ihren Ankerpunkt.
*
* @param angle Drehwinkel in Grad.
*/
public void rotate( double angle ) {
@@ -227,6 +228,7 @@ public abstract class Shape extends FilledShape {
public double getScale() {
return scale;
}
public void scale( double 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.
* <p>
* Die Koordinaten des Ankerpunkt werden relativ zur oberen linken Ecke des
* Rechtecks mit der Breite {@code width} und der Höhe {@code height}
* Die Koordinaten des Ankerpunktes werden relativ zur oberen linken Ecke
* des Rechtecks mit der Breite {@code width} und der Höhe {@code height}
* bestimmt.
*
* @param width Breite des umschließdenden Rechtecks.
* @param height Höhe des umschließdenden Rechtecks.
* @param width Breite des umschließenden Rechtecks.
* @param height Höhe des umschließenden Rechtecks.
* @param anchor Gesuchter Ankerpunkt.
* @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;
// anchor == CENTER
Point2D.Double anchorPoint = new Point2D.Double(
return new Point2D.Double(
wHalf + wHalf * anchor.x,
hHalf + hHalf * anchor.y
);
return anchorPoint;
}
/**
@@ -303,12 +302,10 @@ public abstract class Shape extends FilledShape {
double wHalf = getWidth() * .5, hHalf = getHeight() * .5;
// anchor == CENTER
Point2D.Double anchorPoint = new Point2D.Double(
return new Point2D.Double(
wHalf * (anchor.x - this.anchor.x),
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
* {@link Graphics2D Grafikkontext} zu zeichnen.
* <p>
* Da die AWT-Shape bei jedem Zeichnen (also mindestens einmal pro Frame)
* 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
* Wenn diese Form nicht durch eine AWT-Shape dargestellt wird, kann die
* 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}.
*/
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.Point2D;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
@@ -35,7 +36,7 @@ import java.util.List;
* {@link #arrangeInGrid(int, Options.Direction, double, int)} und
* {@link #align(Options.Direction)} verwendet werden, die jeweils die Position
* 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 {
@@ -43,7 +44,7 @@ public class ShapeGroup extends Shape {
public static final int ARRANGE_COLS = 1;
private List<Shape> shapes;
private final List<Shape> shapes;
private double groupWidth = -1.0;
@@ -51,7 +52,7 @@ public class ShapeGroup extends Shape {
public ShapeGroup() {
super();
shapes = new ArrayList<>(10);
shapes = Collections.synchronizedList(new ArrayList<>(10));
}
public ShapeGroup( double x, double y ) {
@@ -62,9 +63,7 @@ public class ShapeGroup extends Shape {
public ShapeGroup( double x, double y, Shape... shapes ) {
super(x, y);
this.shapes = new ArrayList<>(shapes.length);
for( Shape pShape : shapes ) {
this.shapes.add(pShape);
}
Collections.addAll(this.shapes, shapes);
}
public Shape copy() {
@@ -99,9 +98,11 @@ public class ShapeGroup extends Shape {
public <ShapeType extends Shape> List<ShapeType> getShapes( Class<ShapeType> typeClass ) {
LinkedList<ShapeType> list = new LinkedList<>();
for( Shape s : shapes ) {
if( typeClass.isInstance(s) ) {
list.add((ShapeType) s);
synchronized( shapes ) {
for( Shape shape : shapes ) {
if( typeClass.isInstance(shape) ) {
list.add(typeClass.cast(shape));
}
}
}
return list;
@@ -170,19 +171,15 @@ public class ShapeGroup extends Shape {
int rows, cols;
if( mode == ARRANGE_ROWS ) {
rows = n;
cols = (int) ceil(shapes.size() / n);
cols = (int) ceil(shapes.size() / (double)n);
} else {
cols = n;
rows = (int) ceil(shapes.size() / n);
rows = (int) ceil(shapes.size() / (double)n);
}
// Calculate grid cell size
double maxHeight = shapes.stream().mapToDouble(
( s ) -> s.getHeight()
).reduce(0.0, Double::max);
double maxWidth = shapes.stream().mapToDouble(
( s ) -> s.getWidth()
).reduce(0.0, Double::max);
double maxHeight = shapes.stream().mapToDouble(Shape::getHeight).reduce(0.0, Double::max);
double maxWidth = shapes.stream().mapToDouble(Shape::getWidth).reduce(0.0, Double::max);
double halfHeight = maxHeight * .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.
*/
protected Stroke createStroke() {
public Stroke getStroke() {
// TODO: Used global cached Stroke Objects?
if( stroke == null ) {
switch( strokeType ) {
@@ -241,7 +241,7 @@ public abstract class StrokedShape extends Constants implements Drawable {
if( strokeColor != null && strokeColor.getAlpha() > 0
&& strokeWeight > 0.0 ) {
graphics.setColor(strokeColor.getJavaColor());
graphics.setStroke(createStroke());
graphics.setStroke(getStroke());
graphics.draw(shape);
}
}

View File

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

View File

@@ -52,7 +52,7 @@ public class ChartAxes extends Rectangle {
}
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), 0, (int)(height - (yMax-yMin) * yUnit));
if( showArrows ) {

View File

@@ -91,7 +91,7 @@ public class LineChart extends Rectangle {
for( ChartValue lcv : val ) {
if( drawLines && lastLcv != null ) {
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));
drawDot(graphics, lastLcv, xUnit, yUnit);
}

View File

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