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