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