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 demo.sharededitor.ui.Texturable;
011 import java.awt.geom.Ellipse2D;
012 import java.awt.geom.RectangularShape;
013 import java.awt.Image;
014 import java.awt.Shape;
015
016 /**
017 * Description of the Class
018 *
019 *@author Terracotta, Inc.
020 */
021 final class Circle extends BaseObject implements Texturable {
022 private Ellipse2D.Double shape;
023
024 private transient Shape[] anchors = null;
025
026 private int x1, y1, x2, y2;
027
028 public Circle() {
029 x1 = y1 = x2 = y2 = 0;
030 shape = new Ellipse2D.Double();
031 shape.setFrameFromDiagonal(x1, y1, x2, y2);
032 }
033
034 public void setTexture(Image image) {
035 synchronized (this) {
036 super.setTexture(image);
037 }
038 notifyListeners(this);
039 }
040
041 public boolean isTransient() {
042 RectangularShape bounds = (RectangularShape) shape.getBounds();
043 return (bounds.getHeight() * bounds.getWidth()) < 4;
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 protected Shape getShape() {
091 return this.shape;
092 }
093
094 protected Shape[] getAnchors() {
095 return updateAnchors();
096 }
097
098 private Shape[] updateAnchors() {
099 if (anchors == null) {
100 anchors = new Shape[]{
101 new Ellipse2D.Double(x1 - 5, y2 - 5, 10, 10),
102 new Ellipse2D.Double(x2 - 5, y2 - 5, 10, 10),
103 new Ellipse2D.Double(x2 - 5, y1 - 5, 10, 10),
104 new Ellipse2D.Double(x1 - 5, y1 - 5, 10, 10)};
105 return anchors;
106 }
107
108 ((Ellipse2D.Double) anchors[0]).x = x1 - 5;
109 ((Ellipse2D.Double) anchors[0]).y = y2 - 5;
110 ((Ellipse2D.Double) anchors[1]).x = x2 - 5;
111 ((Ellipse2D.Double) anchors[1]).y = y2 - 5;
112 ((Ellipse2D.Double) anchors[2]).x = x2 - 5;
113 ((Ellipse2D.Double) anchors[2]).y = y1 - 5;
114 ((Ellipse2D.Double) anchors[3]).x = x1 - 5;
115 ((Ellipse2D.Double) anchors[3]).y = y1 - 5;
116 return anchors;
117 }
118 }
|