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