DrawingLayer delegiert nun zu einer Shape

Macht Weniger doppelte Implementierungen nötig
This commit is contained in:
ngb 2022-07-28 12:25:35 +02:00
parent f1d32685b4
commit c93a203ab9
3 changed files with 183 additions and 224 deletions

View File

@ -3,9 +3,13 @@ package schule.ngb.zm.layers;
import schule.ngb.zm.Color; import schule.ngb.zm.Color;
import schule.ngb.zm.Layer; import schule.ngb.zm.Layer;
import schule.ngb.zm.Options; import schule.ngb.zm.Options;
import schule.ngb.zm.shapes.Text;
import schule.ngb.zm.util.io.ImageLoader; import schule.ngb.zm.util.io.ImageLoader;
import java.awt.*; import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Image;
import java.awt.Shape;
import java.awt.geom.*; import java.awt.geom.*;
import java.util.Stack; import java.util.Stack;
@ -15,39 +19,9 @@ import java.util.Stack;
* Ein {@code DrawingLayer} ist eine der drei Standardebenen der * Ein {@code DrawingLayer} ist eine der drei Standardebenen der
* {@link schule.ngb.zm.Zeichenmaschine}. * {@link schule.ngb.zm.Zeichenmaschine}.
*/ */
@SuppressWarnings( "unused" )
public class DrawingLayer extends Layer { public class DrawingLayer extends Layer {
/**
* Aktuelle Füllfarbe.
*/
protected Color fillColor = DEFAULT_FILLCOLOR;
/**
* Der aktuelle Farbverlauf oder {@code null}, wenn aktuell kein
* Farbverlauf gesetzt ist.
*/
protected Paint fill = null;
/**
* Aktuelle Konturfarbe.
*/
protected Color strokeColor = DEFAULT_STROKECOLOR;
/**
* Aktuelle Dicke der Konturlinie.
*/
protected double strokeWeight = DEFAULT_STROKEWEIGHT;
/**
* Art der Konturlinie.
*/
protected Options.StrokeType strokeType = SOLID;
/**
* Standardanker für Formen.
*/
private Options.Direction default_anchor = CENTER;
/** /**
* Wiederverwendbarer Speicher für eine Linie. * Wiederverwendbarer Speicher für eine Linie.
*/ */
@ -84,6 +58,14 @@ public class DrawingLayer extends Layer {
private final Stack<AffineTransform> transformStack; private final Stack<AffineTransform> transformStack;
// private FontMetrics fontMetrics; // private FontMetrics fontMetrics;
/**
* Eine Shape, an die die Methoden für Farben und Konturlinien delegiert
* werden.
*/
private schule.ngb.zm.shapes.Text shapeDelegate;
private final Stack<schule.ngb.zm.shapes.Text> styleStack;
/** /**
* Erstellt eine Ebene in der Standardgröße. * Erstellt eine Ebene in der Standardgröße.
*/ */
@ -92,6 +74,9 @@ public class DrawingLayer extends Layer {
transformStack = new Stack<>(); transformStack = new Stack<>();
transformStack.push(new AffineTransform()); transformStack.push(new AffineTransform());
// fontMetrics = drawing.getFontMetrics(); // fontMetrics = drawing.getFontMetrics();
styleStack = new Stack<>();
resetStyle();
} }
/** /**
@ -105,6 +90,9 @@ public class DrawingLayer extends Layer {
transformStack = new Stack<>(); transformStack = new Stack<>();
transformStack.push(new AffineTransform()); transformStack.push(new AffineTransform());
// fontMetrics = drawing.getFontMetrics(); // fontMetrics = drawing.getFontMetrics();
styleStack = new Stack<>();
resetStyle();
} }
/** /**
@ -113,7 +101,7 @@ public class DrawingLayer extends Layer {
* @return Die aktuelle Füllfarbe. * @return Die aktuelle Füllfarbe.
*/ */
public Color getFillColor() { public Color getFillColor() {
return fillColor; return shapeDelegate.getFillColor();
} }
/** /**
@ -123,8 +111,8 @@ public class DrawingLayer extends Layer {
* @see Color * @see Color
*/ */
public void setFillColor( Color color ) { public void setFillColor( Color color ) {
fillColor = color; shapeDelegate.setFillColor(color);
drawing.setColor(color.getJavaColor()); drawing.setPaint(color);
} }
/** /**
@ -199,7 +187,7 @@ public class DrawingLayer extends Layer {
* Entfernt die Füllung der Form. * Entfernt die Füllung der Form.
*/ */
public void noFill() { public void noFill() {
fillColor = null; shapeDelegate.noFill();
} }
/** /**
@ -208,8 +196,7 @@ public class DrawingLayer extends Layer {
* @see schule.ngb.zm.Constants#DEFAULT_FILLCOLOR * @see schule.ngb.zm.Constants#DEFAULT_FILLCOLOR
*/ */
public void resetFill() { public void resetFill() {
setFillColor(DEFAULT_FILLCOLOR); shapeDelegate.resetFill();
noGradient();
} }
/** /**
@ -225,11 +212,7 @@ public class DrawingLayer extends Layer {
* @param to Farbe am Endpunkt. * @param to Farbe am Endpunkt.
*/ */
public void setGradient( double fromX, double fromY, Color from, double toX, double toY, Color to ) { public void setGradient( double fromX, double fromY, Color from, double toX, double toY, Color to ) {
setFillColor(from); shapeDelegate.setGradient(fromX, fromY, from, toX, toY, to);
fill = new GradientPaint(
(float) fromX, (float) fromY, from.getJavaColor(),
(float) toX, (float) toY, to.getJavaColor()
);
} }
/** /**
@ -246,18 +229,14 @@ public class DrawingLayer extends Layer {
* @param to Farbe am Rand des Kreises. * @param to Farbe am Rand des Kreises.
*/ */
public void setGradient( double centerX, double centerY, double radius, Color from, Color to ) { public void setGradient( double centerX, double centerY, double radius, Color from, Color to ) {
setFillColor(from); shapeDelegate.setGradient(centerX, centerY, radius, from, to);
fill = new RadialGradientPaint(
(float) centerX, (float) centerY, (float) radius,
new float[]{0f, 1f},
new java.awt.Color[]{from.getJavaColor(), to.getJavaColor()});
} }
/** /**
* Entfernt den Farbverlauf von der Form. * Entfernt den Farbverlauf von der Form.
*/ */
public void noGradient() { public void noGradient() {
fill = null; shapeDelegate.noGradient();
} }
/** /**
@ -266,7 +245,7 @@ public class DrawingLayer extends Layer {
* @return Die Konturfarbe oder {@code null}. * @return Die Konturfarbe oder {@code null}.
*/ */
public Color getStrokeColor() { public Color getStrokeColor() {
return strokeColor; return shapeDelegate.getStrokeColor();
} }
/** /**
@ -276,8 +255,8 @@ public class DrawingLayer extends Layer {
* @see Color * @see Color
*/ */
public void setStrokeColor( Color color ) { public void setStrokeColor( Color color ) {
strokeColor = color; shapeDelegate.setStrokeColor(color);
drawing.setColor(color.getJavaColor()); drawing.setPaint(color);
} }
/** /**
@ -352,7 +331,7 @@ public class DrawingLayer extends Layer {
* Entfernt die Kontur der Form. * Entfernt die Kontur der Form.
*/ */
public void noStroke() { public void noStroke() {
strokeColor = null; shapeDelegate.noStroke();
} }
/** /**
@ -363,9 +342,7 @@ public class DrawingLayer extends Layer {
* @see schule.ngb.zm.Constants#SOLID * @see schule.ngb.zm.Constants#SOLID
*/ */
public void resetStroke() { public void resetStroke() {
setStrokeColor(DEFAULT_STROKECOLOR); shapeDelegate.resetStroke();
setStrokeWeight(DEFAULT_STROKEWEIGHT);
setStrokeType(SOLID);
} }
/** /**
@ -374,7 +351,7 @@ public class DrawingLayer extends Layer {
* @return Die aktuelle Dicke der Linie. * @return Die aktuelle Dicke der Linie.
*/ */
public double getStrokeWeight() { public double getStrokeWeight() {
return strokeWeight; return shapeDelegate.getStrokeWeight();
} }
/** /**
@ -384,8 +361,8 @@ public class DrawingLayer extends Layer {
* @param weight Die Dicke der Konturlinie. * @param weight Die Dicke der Konturlinie.
*/ */
public void setStrokeWeight( double weight ) { public void setStrokeWeight( double weight ) {
strokeWeight = weight; shapeDelegate.setStrokeWeight(weight);
drawing.setStroke(createStroke()); drawing.setStroke(shapeDelegate.getStroke());
} }
/** /**
@ -395,7 +372,7 @@ public class DrawingLayer extends Layer {
* @see Options.StrokeType * @see Options.StrokeType
*/ */
public Options.StrokeType getStrokeType() { public Options.StrokeType getStrokeType() {
return strokeType; return shapeDelegate.getStrokeType();
} }
/** /**
@ -406,40 +383,11 @@ public class DrawingLayer extends Layer {
* @see Options.StrokeType * @see Options.StrokeType
*/ */
public void setStrokeType( Options.StrokeType type ) { public void setStrokeType( Options.StrokeType type ) {
this.strokeType = type; shapeDelegate.setStrokeType(type);
}
/**
* Hilfsmethode, um ein {@link Stroke} Objekt mit den aktuellen
* Kontureigenschaften zu erstellen. Der aktuelle {@code Stroke} wird
* zwischengespeichert.
*
* @return Ein {@code Stroke} mit den passenden Kontureigenschaften.
*/
protected Stroke createStroke() {
switch( strokeType ) {
case DOTTED:
return new BasicStroke(
(float) strokeWeight,
BasicStroke.CAP_ROUND,
BasicStroke.JOIN_ROUND,
10.0f, new float[]{1.0f, 5.0f}, 0.0f);
case DASHED:
return new BasicStroke(
(float) strokeWeight,
BasicStroke.CAP_ROUND,
BasicStroke.JOIN_ROUND,
10.0f, new float[]{5.0f}, 0.0f);
default:
return new BasicStroke(
(float) strokeWeight,
BasicStroke.CAP_ROUND,
BasicStroke.JOIN_ROUND);
}
} }
public void setAnchor( Options.Direction anchor ) { public void setAnchor( Options.Direction anchor ) {
default_anchor = anchor; shapeDelegate.setAnchor(anchor);
} }
public void clear( int gray ) { public void clear( int gray ) {
@ -471,18 +419,16 @@ public class DrawingLayer extends Layer {
} }
public void line( double x1, double y1, double x2, double y2 ) { public void line( double x1, double y1, double x2, double y2 ) {
//Shape line = new Line2D.Double(x1, y1, x2, y2);
line.setLine(x1, y1, x2, y2); line.setLine(x1, y1, x2, y2);
drawShape(line); drawShape(line);
} }
public void pixel( double x, double y ) { public void pixel( double x, double y ) {
// square(x, y, 1); buffer.setRGB((int) x, (int) y, shapeDelegate.getFontColor().getRGBA());
buffer.setRGB((int) x, (int) y, fillColor.getRGBA());
} }
public void square( double x, double y, double w ) { public void square( double x, double y, double w ) {
rect(x, y, w, w); rect(x, y, w, w, shapeDelegate.getAnchor());
} }
public void square( double x, double y, double w, Options.Direction anchor ) { public void square( double x, double y, double w, Options.Direction anchor ) {
@ -490,23 +436,22 @@ public class DrawingLayer extends Layer {
} }
public void rect( double x, double y, double w, double h ) { public void rect( double x, double y, double w, double h ) {
rect(x, y, w, h, default_anchor); rect(x, y, w, h, shapeDelegate.getAnchor());
} }
public void rect( double x, double y, double w, double h, Options.Direction anchor ) { public void rect( double x, double y, double w, double h, Options.Direction anchor ) {
Point2D.Double anchorPoint = getAnchorPoint(x, y, w, h, anchor); Point2D.Double anchorPoint = this.getOriginPoint(x, y, w, h, anchor);
// Shape rect = new Rectangle2D.Double(anchorPoint.getX(), anchorPoint.getY(), w, h);
rect.setRect(anchorPoint.getX(), anchorPoint.getY(), w, h); rect.setRect(anchorPoint.getX(), anchorPoint.getY(), w, h);
fillShape(rect); fillShape(rect);
drawShape(rect); drawShape(rect);
} }
public void point( double x, double y ) { public void point( double x, double y ) {
circle(x - 1, y - 1, 2); ellipse(x - 1, y - 1, 2, 2, shapeDelegate.getAnchor());
} }
public void circle( double x, double y, double r ) { public void circle( double x, double y, double r ) {
ellipse(x, y, r + r, r + r, default_anchor); ellipse(x, y, r + r, r + r, shapeDelegate.getAnchor());
} }
public void circle( double x, double y, double r, Options.Direction anchor ) { public void circle( double x, double y, double r, Options.Direction anchor ) {
@ -514,12 +459,11 @@ public class DrawingLayer extends Layer {
} }
public void ellipse( double x, double y, double w, double h ) { public void ellipse( double x, double y, double w, double h ) {
ellipse(x, y, w, h, default_anchor); ellipse(x, y, w, h, shapeDelegate.getAnchor());
} }
public void ellipse( double x, double y, double w, double h, Options.Direction anchor ) { public void ellipse( double x, double y, double w, double h, Options.Direction anchor ) {
Point2D.Double anchorPoint = getAnchorPoint(x, y, w, h, anchor); Point2D.Double anchorPoint = getOriginPoint(x, y, w, h, anchor);
// Shape ellipse = new Ellipse2D.Double(anchorPoint.x, anchorPoint.y, w, h);
ellipse.setFrame(anchorPoint.x, anchorPoint.y, w, h); ellipse.setFrame(anchorPoint.x, anchorPoint.y, w, h);
fillShape(ellipse); fillShape(ellipse);
drawShape(ellipse); drawShape(ellipse);
@ -532,14 +476,7 @@ public class DrawingLayer extends Layer {
public void arc( double x, double y, double w, double h, double angle1, double angle2 ) { public void arc( double x, double y, double w, double h, double angle1, double angle2 ) {
while( angle2 < angle1 ) angle2 += 360.0; while( angle2 < angle1 ) angle2 += 360.0;
Point2D.Double anchorPoint = getAnchorPoint(x, y, w, h, CENTER); Point2D.Double anchorPoint = getOriginPoint(x, y, w, h, CENTER);
/*Shape arc = new Arc2D.Double(
anchorPoint.x,
anchorPoint.y,
d, d,
angle1, angle2 - angle1,
Arc2D.OPEN
);*/
arc.setArc( arc.setArc(
anchorPoint.x, anchorPoint.y, anchorPoint.x, anchorPoint.y,
w, h, w, h,
@ -556,14 +493,7 @@ public class DrawingLayer extends Layer {
double d = r + r; double d = r + r;
Point2D.Double anchorPoint = getAnchorPoint(x, y, d, d, CENTER); Point2D.Double anchorPoint = getOriginPoint(x, y, d, d, CENTER);
/*Shape arc = new Arc2D.Double(
anchorPoint.x,
anchorPoint.y,
d, d,
angle1, angle2 - angle1,
Arc2D.PIE
);*/
arc.setArc( arc.setArc(
anchorPoint.x, anchorPoint.y, anchorPoint.x, anchorPoint.y,
d, d, d, d,
@ -597,12 +527,12 @@ public class DrawingLayer extends Layer {
} }
public void rhombus( double x, double y, double width, double height ) { public void rhombus( double x, double y, double width, double height ) {
rhombus(x, y, width, height, default_anchor); rhombus(x, y, width, height, shapeDelegate.getAnchor());
} }
public void rhombus( double x, double y, double width, double height, Options.Direction anchor ) { public void rhombus( double x, double y, double width, double height, Options.Direction anchor ) {
double whalf = width / 2.0, hhalf = height / 2.0; double whalf = width / 2.0, hhalf = height / 2.0;
Point2D.Double anchorPoint = getAnchorPoint(x, y, width, height, anchor); Point2D.Double anchorPoint = getOriginPoint(x, y, width, height, anchor);
polygon(anchorPoint.x + whalf, anchorPoint.y, anchorPoint.x + width, anchorPoint.y + hhalf, anchorPoint.x + whalf, anchorPoint.y + height, anchorPoint.x, anchorPoint.y + hhalf); polygon(anchorPoint.x + whalf, anchorPoint.y, anchorPoint.x + width, anchorPoint.y + hhalf, anchorPoint.x + whalf, anchorPoint.y + height, anchorPoint.x, anchorPoint.y + hhalf);
} }
@ -648,8 +578,22 @@ public class DrawingLayer extends Layer {
drawShape(path); drawShape(path);
} }
// TODO: General shape drawing protected void fillShape( Shape shape ) {
/* if( shapeDelegate.getPaint() != null ) {
drawing.setPaint(shapeDelegate.getPaint());
drawing.fill(shape);
}
}
protected void drawShape( Shape shape ) {
if( shapeDelegate.getStrokeColor() != null && shapeDelegate.getStrokeColor().getAlpha() > 0
&& shapeDelegate.getStrokeWeight() > 0.0 ) {
drawing.setColor(shapeDelegate.getStrokeColor().getJavaColor());
drawing.setStroke(shapeDelegate.getStroke());
drawing.draw(shape);
}
}
public void beginShape() { public void beginShape() {
path.reset(); path.reset();
pathStarted = false; pathStarted = false;
@ -672,10 +616,9 @@ public class DrawingLayer extends Layer {
fillShape(path); fillShape(path);
drawShape(path); drawShape(path);
} }
*/
public void image( String imageSource, double x, double y ) { public void image( String imageSource, double x, double y ) {
image(ImageLoader.loadImage(imageSource), x, y, 1.0, default_anchor); image(ImageLoader.loadImage(imageSource), x, y, 1.0, shapeDelegate.getAnchor());
} }
public void image( String imageSource, double x, double y, Options.Direction anchor ) { public void image( String imageSource, double x, double y, Options.Direction anchor ) {
@ -683,7 +626,7 @@ public class DrawingLayer extends Layer {
} }
public void image( String imageSource, double x, double y, double scale ) { public void image( String imageSource, double x, double y, double scale ) {
image(ImageLoader.loadImage(imageSource), x, y, scale, default_anchor); image(ImageLoader.loadImage(imageSource), x, y, scale, shapeDelegate.getAnchor());
} }
public void image( String imageSource, double x, double y, double scale, Options.Direction anchor ) { public void image( String imageSource, double x, double y, double scale, Options.Direction anchor ) {
@ -691,120 +634,108 @@ public class DrawingLayer extends Layer {
} }
public void image( Image image, double x, double y ) { public void image( Image image, double x, double y ) {
image(image, x, y, 1.0, default_anchor); image(image, x, y, 1.0, shapeDelegate.getAnchor());
} }
public void image( Image image, double x, double y, double scale ) { public void image( Image image, double x, double y, double scale ) {
image(image, x, y, scale, default_anchor); image(image, x, y, scale, shapeDelegate.getAnchor());
} }
public void image( Image image, double x, double y, double scale, Options.Direction anchor ) { public void image( Image image, double x, double y, double scale, Options.Direction anchor ) {
if( image != null ) { if( image != null ) {
double neww = image.getWidth(null) * scale; double neww = image.getWidth(null) * scale;
double newh = image.getHeight(null) * scale; double newh = image.getHeight(null) * scale;
Point2D.Double anchorPoint = getAnchorPoint(x, y, neww, newh, anchor); Point2D.Double anchorPoint = getOriginPoint(x, y, neww, newh, anchor);
drawing.drawImage(image, (int) anchorPoint.x, (int) anchorPoint.y, (int) neww, (int) newh, null); drawing.drawImage(image, (int) anchorPoint.x, (int) anchorPoint.y, (int) neww, (int) newh, null);
} }
} }
public Font getFont() {
return shapeDelegate.getFont();
}
public void setFont( Font newFont ) {
shapeDelegate.setFont(newFont);
// fontMetrics = drawing.getFontMetrics();
}
public void setFont( String fontName ) {
shapeDelegate.setFont(fontName);
}
public void setFont( String fontName, int size ) {
shapeDelegate.setFont(fontName, size);
}
public void setFont( String fontName, int size, int style ) {
shapeDelegate.setFont(fontName, size, style);
}
public double getFontSize() {
return shapeDelegate.getFontSize();
}
public void setFontSize( double size ) {
shapeDelegate.setFontSize(size);
}
public void text( String text, double x, double y ) { public void text( String text, double x, double y ) {
text(text, x, y, default_anchor); text(text, x, y, shapeDelegate.getAnchor());
} }
public void text( String text, double x, double y, Options.Direction anchor ) { public void text( String text, double x, double y, Options.Direction anchor ) {
drawing.setFont(shapeDelegate.getFont());
FontMetrics fm = drawing.getFontMetrics(); FontMetrics fm = drawing.getFontMetrics();
Point2D.Double anchorPoint = getAnchorPoint(x, y + fm.getAscent(), fm.stringWidth(text), fm.getHeight(), anchor); Point2D.Double anchorPoint = getOriginPoint(x, y + fm.getAscent(), fm.stringWidth(text), fm.getHeight(), anchor);
drawing.drawString(text, (float) anchorPoint.x, (float) anchorPoint.y); drawing.drawString(text, (float) anchorPoint.x, (float) anchorPoint.y);
} }
public void setFontSize( int size ) { /**
setFont(drawing.getFont().deriveFont((float) size)); * Berechnet den Ursprung einer zu zeichnenden Form, wenn der angegebene
* Ankerpunkt zugrunde gelegt wird.
* <p>
* Der Ursprung einer Form liegt immer oben links. Eine Form an den
* Koordinaten {@code (300, 300)} und den Abmessungen {@code (100, 50)}
* würde für den Anker {@link Options.Direction#NORTHWEST} also den Ursprung
* {@code (300, 300)} haben. Für den Anker {@link Options.Direction#CENTER}
* wäre der Ursprung zu den Koordinaten um die hälfte der Abmessungen nach
* links und oben verschoben bei {@code (250, 275)}. Beim Zeichnen liegen
* die Koordinaten {@code (300, 300)} dann in der Mitte der Form.
*
* @param x x-Koordinate des umgebenden Rechtecks.
* @param y y-Koordinate des umgebenden Rechtecks.
* @param w Breite des umgebenden Rechtecks.
* @param h Höhe des umgebenden Rechtecks.
* @param anchor Zu verendender Anker.
* @return Der Ursprung des umgebenden Rechtecks.
*/
protected Point2D.Double getOriginPoint( double x, double y, double w, double h, Options.Direction anchor ) {
Point2D.Double ap = schule.ngb.zm.shapes.Shape.getAnchorPoint(w, h, anchor);
ap.x = x - ap.x;
ap.y = y - ap.y;
return ap;
} }
public void setFont( String fontName ) { public void pushStyle() {
Font font = new Font(fontName, drawing.getFont().getStyle(), drawing.getFont().getSize()); styleStack.push(shapeDelegate);
setFont(font); shapeDelegate = new Text(shapeDelegate);
} }
public void setFont( String fontName, int size ) { public void popStyle() {
Font font = new Font(fontName, drawing.getFont().getStyle(), size); if( styleStack.isEmpty() ) {
setFont(font); resetStyle();
} } else {
shapeDelegate = styleStack.pop();
public void setFont( String fontName, int size, int style ) {
Font font = new Font(fontName, style, size);
setFont(font);
}
public void setFont( Font font ) {
drawing.setFont(font);
fontMetrics = drawing.getFontMetrics();
}
private Point2D.Double transformToCanvas( double x, double y ) {
return transformToCanvas(new Point2D.Double(x, y));
}
private Point2D.Double transformToCanvas( Point2D.Double pPoint ) {
AffineTransform matrix = getMatrix();
matrix.transform(pPoint, pPoint);
return pPoint;
}
private Point2D.Double transformToUser( double x, double y ) {
return transformToUser(new Point2D.Double(x, y));
}
private Point2D.Double transformToUser( Point2D.Double pPoint ) {
AffineTransform matrix = getMatrix();
try {
matrix.inverseTransform(pPoint, pPoint);
} catch( NoninvertibleTransformException e ) {
e.printStackTrace();
}
return pPoint;
}
private Point2D.Double getAnchorPoint( double x, double y, double w, double h, Options.Direction anchor ) {
double whalf = w * .5, hhalf = h * .5;
// anchor == CENTER
Point2D.Double anchorPoint = new Point2D.Double(x - whalf, y - hhalf);
if( NORTH.in(anchor) ) {
anchorPoint.y += hhalf;
}
if( SOUTH.in(anchor) ) {
anchorPoint.y -= hhalf;
}
if( WEST.in(anchor) ) {
anchorPoint.x += whalf;
}
if( EAST.in(anchor) ) {
anchorPoint.x -= whalf;
}
return anchorPoint;
}
private void fillShape( Shape pShape ) {
if( fillColor != null && fillColor.getAlpha() > 0.0 ) {
drawing.setColor(fillColor.getJavaColor());
drawing.fill(pShape);
} }
} }
private void drawShape( Shape pShape ) { public void resetStyle() {
if( strokeColor != null && strokeColor.getAlpha() > 0.0 Text newDelegate = new Text(0, 0, "");
&& strokeWeight > 0.0 ) { if( shapeDelegate != null ) {
drawing.setColor(strokeColor.getJavaColor()); newDelegate.setAnchor(shapeDelegate.getAnchor());
drawing.setStroke(createStroke());
drawing.draw(pShape);
} }
shapeDelegate = newDelegate;
} }
public void translate( double dx, double dy ) { public void translate( double dx, double dy ) {

View File

@ -24,6 +24,7 @@ public class Text extends Shape {
public Text( double x, double y, String text ) { public Text( double x, double y, String text ) {
this(x, y, text, new Font(Font.SANS_SERIF, Font.PLAIN, DEFAULT_FONTSIZE)); this(x, y, text, new Font(Font.SANS_SERIF, Font.PLAIN, DEFAULT_FONTSIZE));
} }
public Text( double x, double y, String text, String fontname ) { public Text( double x, double y, String text, String fontname ) {
super(x, y); super(x, y);
Font userfont = FontLoader.loadFont(fontname); Font userfont = FontLoader.loadFont(fontname);
@ -60,29 +61,58 @@ public class Text extends Shape {
return height; return height;
} }
public void setFont( String fontname ) { public Font getFont() {
Font newFont = FontLoader.loadFont(fontname); return font;
}
public void setFont( Font newFont ) {
//font = newFont.deriveFont(font.getStyle(), font.getSize2D());
font = new Font(newFont.getFontName(), newFont.getStyle(), newFont.getSize());
//font = newFont;
calculateBounds();
}
public void setFont( String fontName ) {
Font newFont = FontLoader.loadFont(fontName);
if( newFont != null ) { if( newFont != null ) {
setFont(newFont); setFont(newFont);
} }
} }
public void setFont( Font newFont ) { public void setFont( String fontName, double fontSize ) {
font = newFont.deriveFont(font.getSize2D()); Font newFont = FontLoader.loadFont(fontName);
if( newFont != null ) {
setFont(newFont);
}
font = newFont.deriveFont((float) fontSize);
calculateBounds(); calculateBounds();
} }
public Font getFont() { public void setFont( String fontName, double fontSize, int style ) {
return font; Font newFont = FontLoader.loadFont(fontName);
if( newFont != null ) {
setFont(newFont);
}
font = newFont.deriveFont(style, (float) fontSize);
calculateBounds();
} }
public void setFontsize( double size ) { public double getFontSize() {
return font.getSize2D();
}
public void setFontSize( double size ) {
font = font.deriveFont((float) size); font = font.deriveFont((float) size);
calculateBounds(); calculateBounds();
} }
public double getFontsize() { public int getFontStyle() {
return font.getSize2D(); return font.getStyle();
}
public void setFontStyle( int fontStyle ) {
font = font.deriveFont(fontStyle);
calculateBounds();
} }
public String getText() { public String getText() {
@ -163,7 +193,7 @@ public class Text extends Shape {
@Override @Override
public void scale( double factor ) { public void scale( double factor ) {
super.scale(factor); super.scale(factor);
setFontsize(font.getSize2D() * factor); setFontSize(font.getSize2D() * factor);
} }
@Override @Override
@ -191,7 +221,7 @@ public class Text extends Shape {
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.drawRect(0, 0, width, height); graphics.drawRect(0, 0, width, height);
} }

View File

@ -2,8 +2,6 @@ package schule.ngb.zm;
import schule.ngb.zm.shapes.*; import schule.ngb.zm.shapes.*;
import java.awt.geom.Point2D;
public class TestInput extends Zeichenmaschine { public class TestInput extends Zeichenmaschine {
public static void main( String[] args ) { public static void main( String[] args ) {
@ -23,7 +21,7 @@ public class TestInput extends Zeichenmaschine {
shapes.getShape(1).setFillColor(PINK); shapes.getShape(1).setFillColor(PINK);
text = new Text(10, 42, ""); text = new Text(10, 42, "");
text.setFontsize(32); text.setFontSize(32);
text.setAnchor(NORTHWEST); text.setAnchor(NORTHWEST);
shapes.add(text); shapes.add(text);
} }