package jsynoptic.plugins.circuit.boxes;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import jsynoptic.plugins.circuit.Abstract2DCircuitComponent;
import simtools.shapes.AbstractShape;
/**
* A circuit box is a gated rectangle with a optional symbol.
* @author zxpletran007
*
*/
public abstract class CircuitBox extends Abstract2DCircuitComponent {
private static final long serialVersionUID = 9159230286968343152L;
protected transient Rectangle2D.Double rec;
protected transient Rectangle2D.Double fillrec;
/**
* An optional symbol that shall be drawn on the 10*10 area
*/
protected transient Polygon[] symbol;
public CircuitBox(int ox, int oy, int width, int height) {
super(ox, oy, width, height);
rec = new Rectangle2D.Double(ox, oy-_h, width-1, height-1);
fillrec = new Rectangle2D.Double();
symbol = createSymbol();
}
/**
* Allow subclasses to create a 10*10 symbol that will be displayed inside the rectangle circuit component
*/
protected Polygon[] createSymbol(){
return null;
}
protected AbstractShape cloneShape() {
CircuitBox res = (CircuitBox) super.cloneShape();
res.rec = (Rectangle2D.Double)rec.clone();
res.fillrec = (Rectangle2D.Double)fillrec.clone();
return res;
}
private void readObject(java.io.ObjectInputStream in) throws java.lang.ClassNotFoundException, java.io.IOException {
in.defaultReadObject();
rec = new Rectangle2D.Double(_ox, _oy-_h, _w-1, _h-1);
fillrec = new Rectangle2D.Double();
symbol = createSymbol();
}
/* (non-Javadoc)
* @see jsynoptic.builtin.SimpleShape#getDelegateShape()
*/
protected Shape getDelegateShape() {
return rec;
}
/* (non-Javadoc)
* @see simtools.shapes.AbstractShape#translate(int, int)
*/
public void translate(int dx, int dy) {
super.translate(dx, dy);
rec.x+=dx;
rec.y+=dy;
}
/* (non-Javadoc)
* @see simtools.shapes.AbstractShape#setAnchor(int, int)
*/
public void setAnchor(int ox, int oy) {
super.setAnchor(ox, oy);
rec.x=ox;
rec.y=oy-_h;
}
/* (non-Javadoc)
* @see simtools.diagram.Resizable#resize(int, int)
*/
public void resize(int dx, int dy) {
super.resize(dx, dy);
rec.x=_ox; rec.y=_oy-_h;
rec.height=_h-1; rec.width=_w-1;
if (symbol != null){
symbol = createSymbol();
}
}
public void setPropertyValue(String name, Object value) {
super.setPropertyValue(name, value);
// Update rectangle dimension
rec.x=_ox;
rec.y=_oy-_h;
rec.height=_h-1;
rec.width=_w-1;
}
protected void drawHook(Graphics2D g, boolean shapeDrawn) {
if (!shapeDrawn) {
return;
}
Object textRenderingHint = g.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, AbstractShape.ANTI_ALIASING ? RenderingHints.VALUE_ANTIALIAS_ON : RenderingHints.VALUE_ANTIALIAS_OFF);
Color oldColor = g.getColor();
g.setColor(drawColor);
// draw shape
if (symbol != null){
AffineTransform oldTrasfrom = g.getTransform();
Stroke oldStroke = g.getStroke();
g.translate(_ox, _oy - _h);
g.scale(_w / 10., _h/ 10.);
g.setStroke(new BasicStroke(0.1f));
for(int i=0;i<symbol.length;i++){
g.draw(symbol[i]);
g.fill(symbol[i]);
}
g.setStroke(oldStroke);
g.setTransform(oldTrasfrom);
}
// Restore old params
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, textRenderingHint);
g.setColor(oldColor);
}
}