mirror of
https://github.com/hneemann/Digital.git
synced 2025-09-19 01:44:44 -04:00
speed up of drawing
This commit is contained in:
parent
d6ee37a1c1
commit
f107fe79d9
@ -29,6 +29,7 @@ public class VisualElement implements Drawable, Moveable, AttributeListener {
|
|||||||
private transient InteractorInterface interactor;
|
private transient InteractorInterface interactor;
|
||||||
private transient Element element;
|
private transient Element element;
|
||||||
private transient ShapeFactory shapeFactory;
|
private transient ShapeFactory shapeFactory;
|
||||||
|
private transient Transform transform;
|
||||||
|
|
||||||
private final String elementName;
|
private final String elementName;
|
||||||
private final ElementAttributes elementAttributes;
|
private final ElementAttributes elementAttributes;
|
||||||
@ -43,7 +44,7 @@ public class VisualElement implements Drawable, Moveable, AttributeListener {
|
|||||||
public VisualElement(String elementName) {
|
public VisualElement(String elementName) {
|
||||||
this.elementName = elementName;
|
this.elementName = elementName;
|
||||||
elementAttributes = new ElementAttributes();
|
elementAttributes = new ElementAttributes();
|
||||||
pos = new Vector(0, 0);
|
setPos(new Vector(0, 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -54,7 +55,7 @@ public class VisualElement implements Drawable, Moveable, AttributeListener {
|
|||||||
public VisualElement(VisualElement proto) {
|
public VisualElement(VisualElement proto) {
|
||||||
this.elementName = proto.elementName;
|
this.elementName = proto.elementName;
|
||||||
this.elementAttributes = new ElementAttributes(proto.elementAttributes);
|
this.elementAttributes = new ElementAttributes(proto.elementAttributes);
|
||||||
this.pos = new Vector(proto.pos);
|
setPos(new Vector(proto.pos));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -91,6 +92,7 @@ public class VisualElement implements Drawable, Moveable, AttributeListener {
|
|||||||
public VisualElement setPos(Vector pos) {
|
public VisualElement setPos(Vector pos) {
|
||||||
this.pos = pos;
|
this.pos = pos;
|
||||||
minMax = null;
|
minMax = null;
|
||||||
|
transform=null;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -167,11 +169,14 @@ public class VisualElement implements Drawable, Moveable, AttributeListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Transform createTransform() {
|
private Transform createTransform() {
|
||||||
|
if (transform==null) {
|
||||||
int rotate = getRotate();
|
int rotate = getRotate();
|
||||||
if (rotate == 0)
|
if (rotate == 0)
|
||||||
return v -> v.add(pos);
|
transform = v -> v.add(pos);
|
||||||
else
|
else
|
||||||
return new TransformRotate(pos, rotate);
|
transform = new TransformRotate(pos, rotate);
|
||||||
|
}
|
||||||
|
return transform;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -188,8 +193,7 @@ public class VisualElement implements Drawable, Moveable, AttributeListener {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void move(Vector delta) {
|
public void move(Vector delta) {
|
||||||
pos = pos.add(delta);
|
setPos(pos.add(delta));
|
||||||
minMax = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -303,6 +307,7 @@ public class VisualElement implements Drawable, Moveable, AttributeListener {
|
|||||||
@Override
|
@Override
|
||||||
public void attributeChanged(Key key) {
|
public void attributeChanged(Key key) {
|
||||||
shape = null;
|
shape = null;
|
||||||
|
transform=null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -15,6 +15,8 @@ import static de.neemann.digital.core.element.ElementAttributes.cleanLabel;
|
|||||||
public class GraphicSwing implements Graphic {
|
public class GraphicSwing implements Graphic {
|
||||||
|
|
||||||
private final Graphics2D gr;
|
private final Graphics2D gr;
|
||||||
|
private final int minFontSize;
|
||||||
|
private int pixelSize;
|
||||||
private Style lastStyle;
|
private Style lastStyle;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -23,7 +25,19 @@ public class GraphicSwing implements Graphic {
|
|||||||
* @param gr the {@link Graphics2D} instave to use.
|
* @param gr the {@link Graphics2D} instave to use.
|
||||||
*/
|
*/
|
||||||
public GraphicSwing(Graphics2D gr) {
|
public GraphicSwing(Graphics2D gr) {
|
||||||
|
this(gr, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new instance
|
||||||
|
*
|
||||||
|
* @param gr the {@link Graphics2D} instave to use.
|
||||||
|
* @param pixelSize the size of one pixel
|
||||||
|
*/
|
||||||
|
public GraphicSwing(Graphics2D gr, int pixelSize) {
|
||||||
this.gr = gr;
|
this.gr = gr;
|
||||||
|
this.pixelSize = pixelSize;
|
||||||
|
this.minFontSize = pixelSize * 3;
|
||||||
gr.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
|
gr.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,14 +73,16 @@ public class GraphicSwing implements Graphic {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void drawCircle(Vector p1, Vector p2, Style style) {
|
public void drawCircle(Vector p1, Vector p2, Style style) {
|
||||||
|
Vector w = Vector.width(p1, p2);
|
||||||
|
if (w.x > pixelSize || w.y > pixelSize) {
|
||||||
applyStyle(style);
|
applyStyle(style);
|
||||||
Vector p = Vector.min(p1, p2);
|
Vector p = Vector.min(p1, p2);
|
||||||
Vector w = Vector.width(p1, p2);
|
|
||||||
if (style.isFilled())
|
if (style.isFilled())
|
||||||
gr.fillOval(p.x - 1, p.y - 1, w.x + 2, w.y + 2);
|
gr.fillOval(p.x - 1, p.y - 1, w.x + 2, w.y + 2);
|
||||||
else
|
else
|
||||||
gr.drawOval(p.x, p.y, w.x, w.y);
|
gr.drawOval(p.x, p.y, w.x, w.y);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void applyStyle(Style style) {
|
private void applyStyle(Style style) {
|
||||||
if (style != lastStyle) {
|
if (style != lastStyle) {
|
||||||
@ -79,6 +95,9 @@ public class GraphicSwing implements Graphic {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void drawText(Vector p1, Vector p2, String text, Orientation orientation, Style style) {
|
public void drawText(Vector p1, Vector p2, String text, Orientation orientation, Style style) {
|
||||||
|
applyStyle(style); // sets also font size!
|
||||||
|
int height = gr.getFontMetrics().getHeight();
|
||||||
|
if (height > minFontSize) {
|
||||||
if (text == null || text.length() == 0) return;
|
if (text == null || text.length() == 0) return;
|
||||||
|
|
||||||
//GraphicMinMax.approxTextSize(this, p1, p2, text, orientation, style);
|
//GraphicMinMax.approxTextSize(this, p1, p2, text, orientation, style);
|
||||||
@ -105,8 +124,6 @@ public class GraphicSwing implements Graphic {
|
|||||||
gr.translate(-p1.x, -p1.y);
|
gr.translate(-p1.x, -p1.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
applyStyle(style);
|
|
||||||
int xoff = 0;
|
int xoff = 0;
|
||||||
if (orientation.getX() != 0) {
|
if (orientation.getX() != 0) {
|
||||||
int width = gr.getFontMetrics().stringWidth(text);
|
int width = gr.getFontMetrics().stringWidth(text);
|
||||||
@ -115,7 +132,6 @@ public class GraphicSwing implements Graphic {
|
|||||||
|
|
||||||
int yoff = 0;
|
int yoff = 0;
|
||||||
if (orientation.getY() != 0) {
|
if (orientation.getY() != 0) {
|
||||||
int height = gr.getFontMetrics().getHeight();
|
|
||||||
yoff += height * orientation.getY() / 3;
|
yoff += height * orientation.getY() / 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -123,7 +139,7 @@ public class GraphicSwing implements Graphic {
|
|||||||
|
|
||||||
if (rotateText)
|
if (rotateText)
|
||||||
gr.setTransform(old);
|
gr.setTransform(old);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -337,8 +337,10 @@ public class CircuitComponent extends JComponent {
|
|||||||
AffineTransform oldTrans = gr2.getTransform();
|
AffineTransform oldTrans = gr2.getTransform();
|
||||||
gr2.transform(transform);
|
gr2.transform(transform);
|
||||||
|
|
||||||
GraphicSwing gr = new GraphicSwing(gr2);
|
GraphicSwing gr = new GraphicSwing(gr2, (int) (2 / transform.getScaleX()));
|
||||||
|
// long time = System.currentTimeMillis();
|
||||||
circuit.drawTo(gr, highLighted, modelSync);
|
circuit.drawTo(gr, highLighted, modelSync);
|
||||||
|
// System.out.println(System.currentTimeMillis()-time); // -agentlib:hprof=cpu=samples
|
||||||
|
|
||||||
activeMouseController.drawTo(gr);
|
activeMouseController.drawTo(gr);
|
||||||
gr2.setTransform(oldTrans);
|
gr2.setTransform(oldTrans);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user