001: /*
002: @COPYRIGHT@
003: */
004: package demo.sharededitor.models;
005:
006: import demo.sharededitor.ui.Texturable;
007: import java.awt.geom.Ellipse2D;
008: import java.awt.geom.RectangularShape;
009: import java.awt.Image;
010: import java.awt.Shape;
011:
012: final class Circle extends BaseObject implements Texturable {
013: private Ellipse2D.Double shape;
014:
015: protected Shape getShape() {
016: return this .shape;
017: }
018:
019: private transient Shape[] anchors = null;
020:
021: private Shape[] updateAnchors() {
022: if (anchors == null) {
023: anchors = new Shape[] {
024: new Ellipse2D.Double(x1 - 5, y2 - 5, 10, 10),
025: new Ellipse2D.Double(x2 - 5, y2 - 5, 10, 10),
026: new Ellipse2D.Double(x2 - 5, y1 - 5, 10, 10),
027: new Ellipse2D.Double(x1 - 5, y1 - 5, 10, 10) };
028: return anchors;
029: }
030:
031: ((Ellipse2D.Double) anchors[0]).x = x1 - 5;
032: ((Ellipse2D.Double) anchors[0]).y = y2 - 5;
033: ((Ellipse2D.Double) anchors[1]).x = x2 - 5;
034: ((Ellipse2D.Double) anchors[1]).y = y2 - 5;
035: ((Ellipse2D.Double) anchors[2]).x = x2 - 5;
036: ((Ellipse2D.Double) anchors[2]).y = y1 - 5;
037: ((Ellipse2D.Double) anchors[3]).x = x1 - 5;
038: ((Ellipse2D.Double) anchors[3]).y = y1 - 5;
039: return anchors;
040: }
041:
042: protected Shape[] getAnchors() {
043: return updateAnchors();
044: }
045:
046: public void move(int dx, int dy) {
047: synchronized (this ) {
048: x1 += dx;
049: y1 += dy;
050: x2 += dx;
051: y2 += dy;
052: shape.setFrameFromDiagonal(x1, y1, x2, y2);
053: updateAnchors();
054: }
055: this .notifyListeners(this );
056: }
057:
058: public void resize(int x, int y) {
059: synchronized (this ) {
060: switch (grabbedAnchor()) {
061: case 0:
062: x1 = x;
063: y2 = y;
064: break;
065: case 1:
066: x2 = x;
067: y2 = y;
068: break;
069: case 2:
070: x2 = x;
071: y1 = y;
072: break;
073: case 3:
074: x1 = x;
075: y1 = y;
076: break;
077: }
078: shape.setFrameFromDiagonal(x1, y1, x2, y2);
079: updateAnchors();
080: }
081: this .notifyListeners(this );
082: }
083:
084: public void clearTexture() {
085: synchronized (this ) {
086: super .clearTexture();
087: }
088: }
089:
090: public void setTexture(Image image) {
091: synchronized (this ) {
092: super .setTexture(image);
093: }
094: notifyListeners(this );
095: }
096:
097: public boolean isTransient() {
098: RectangularShape bounds = (RectangularShape) shape.getBounds();
099: return (bounds.getHeight() * bounds.getWidth()) < 4;
100: }
101:
102: private int x1, y1, x2, y2;
103:
104: public Circle() {
105: x1 = y1 = x2 = y2 = 0;
106: shape = new Ellipse2D.Double();
107: shape.setFrameFromDiagonal(x1, y1, x2, y2);
108: }
109: }
|