001: /*
002: @COPYRIGHT@
003: */
004: package demo.sharededitor.models;
005:
006: import java.awt.geom.Ellipse2D;
007: import java.awt.geom.RectangularShape;
008: import java.awt.geom.RoundRectangle2D;
009: import java.awt.Image;
010: import java.awt.Shape;
011:
012: import demo.sharededitor.ui.Texturable;
013:
014: class Square extends BaseObject implements Texturable {
015: private RoundRectangle2D.Double shape;
016:
017: protected Shape getShape() {
018: return 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 - 5, 10, 10),
027: new Ellipse2D.Double(x2 - 5, y2 - 5, 10, 10),
028: new Ellipse2D.Double(x2 - 5, y1 - 5, 10, 10),
029: new Ellipse2D.Double(x1 - 5, y1 - 5, 10, 10) };
030: return anchors;
031: }
032:
033: ((Ellipse2D.Double) anchors[0]).x = x1 - 5;
034: ((Ellipse2D.Double) anchors[0]).y = y2 - 5;
035: ((Ellipse2D.Double) anchors[1]).x = x2 - 5;
036: ((Ellipse2D.Double) anchors[1]).y = y2 - 5;
037: ((Ellipse2D.Double) anchors[2]).x = x2 - 5;
038: ((Ellipse2D.Double) anchors[2]).y = y1 - 5;
039: ((Ellipse2D.Double) anchors[3]).x = x1 - 5;
040: ((Ellipse2D.Double) anchors[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: notifyListeners(this );
096: }
097: }
098:
099: public boolean isTransient() {
100: RectangularShape bounds = (RectangularShape) shape.getBounds();
101: return (bounds.getHeight() * bounds.getWidth()) < 4;
102: }
103:
104: private int x1, y1, x2, y2;
105:
106: public Square() {
107: x1 = y1 = x2 = y2 = 0;
108: shape = new RoundRectangle2D.Double();
109: shape.arcwidth = shape.archeight = 12;
110: shape.setFrameFromDiagonal(x1, y1, x2, y2);
111: }
112: }
|