1 27 28 package org.nightlabs.editor2d.figures; 29 30 import java.awt.Graphics2D ; 31 import java.awt.geom.AffineTransform ; 32 import java.awt.geom.Area ; 33 34 import org.apache.log4j.Logger; 35 import org.eclipse.draw2d.Graphics; 36 import org.eclipse.draw2d.IFigure; 37 import org.eclipse.draw2d.J2DGraphics; 38 import org.eclipse.draw2d.Shape; 39 import org.eclipse.draw2d.geometry.Rectangle; 40 import org.eclipse.gef.editparts.ZoomListener; 41 42 import org.nightlabs.editor2d.j2d.GeneralShape; 43 import org.nightlabs.editor2d.util.J2DUtil; 44 import org.nightlabs.editor2d.util.RenderUtil; 45 46 public class AbstractShapeFigure 47 extends Shape 48 implements ShapeFigure 49 { 50 public static final Logger LOGGER = Logger.getLogger(AbstractShapeFigure.class); 51 52 protected J2DGraphics j2d; 53 protected AffineTransform at = new AffineTransform (); 54 protected GeneralShape gp; 55 protected java.awt.Rectangle gpBounds; 56 57 public void setBounds(Rectangle newBounds) 58 { 59 repaint(); 60 super.setBounds(J2DUtil.toDraw2D(getGPBounds())); 61 } 62 63 public AbstractShapeFigure() { 64 super(); 65 } 66 67 protected Graphics2D g2d; 68 protected void fillShape(Graphics graphics) 69 { 70 if (graphics instanceof J2DGraphics) 71 { 72 j2d = (J2DGraphics) graphics; 73 g2d = j2d.createGraphics2D(); 74 g2d.setClip(null); 75 g2d.setPaint(J2DUtil.toAWTColor(getBackgroundColor())); 76 g2d.fill(getGeneralShape()); 77 g2d.dispose(); 78 } 79 } 80 81 84 protected void outlineShape(Graphics graphics) 85 { 86 if (graphics instanceof J2DGraphics) 87 { 88 j2d = (J2DGraphics) graphics; 89 g2d = j2d.createGraphics2D(); 90 g2d.setClip(null); 91 g2d.setPaint(J2DUtil.toAWTColor(getForegroundColor())); 92 g2d.setStroke(RenderUtil.setStrokeStyle(lineWidth, lineStyle)); 93 g2d.draw(getGeneralShape()); 94 g2d.dispose(); 95 } 96 } 97 98 public GeneralShape getGeneralShape() { 99 return gp; 100 } 101 102 public void setGeneralShape(GeneralShape generalShape) { 103 gp = generalShape; 104 outlineArea = null; 105 } 106 107 public java.awt.Rectangle getGPBounds() 108 { 109 if (gp == null) 110 gpBounds = J2DUtil.toAWTRectangle(getBounds()); 111 else 112 gpBounds = getGeneralShape().getBounds(); 113 114 return gpBounds; 115 } 116 117 public void transform(AffineTransform at) 118 { 119 getGeneralShape().transform(at); 120 outlineArea = null; 121 bounds = J2DUtil.toDraw2D(getGeneralShape().getBounds()); 122 repaint(); 123 } 124 125 public static final double DEFAULT_HIT_TOLERANCE = 5; 126 protected double hitTolerance = DEFAULT_HIT_TOLERANCE; 127 public double getHitTolerance() { 128 return hitTolerance; 129 } 130 public void setHitTolerance(double hitTolerance) { 131 this.hitTolerance = hitTolerance; 132 } 133 134 protected Area outlineArea; 135 136 protected ZoomListener zoomListener = new ZoomListener() 137 { 138 public void zoomChanged(double zoom) 139 { 140 hitTolerance = hitTolerance / zoom; 141 } 142 }; 143 public ZoomListener getZoomListener() { 144 return zoomListener; 145 } 146 147 150 public boolean containsPoint(int x, int y) 151 { 152 if (isFill()) 153 return getGeneralShape().contains(x, y); 154 else 155 { 156 if (outlineArea == null) { 157 Rectangle outerBounds = getBounds().getCopy(); 158 Rectangle innerBounds = getBounds().getCopy(); 159 outerBounds.expand((int)hitTolerance, (int)hitTolerance); 160 innerBounds.shrink((int)hitTolerance, (int)hitTolerance); 161 GeneralShape outerGS = (GeneralShape) getGeneralShape().clone(); 162 GeneralShape innerGS = (GeneralShape) getGeneralShape().clone(); 163 J2DUtil.transformGeneralShape(outerGS, getBounds(), outerBounds); 164 J2DUtil.transformGeneralShape(innerGS, getBounds(), innerBounds); 165 outlineArea = new Area (outerGS); 166 Area innerArea = new Area (innerGS); 167 outlineArea.exclusiveOr(innerArea); 168 } 169 return outlineArea.contains(x,y); 170 } 171 } 172 173 public void performScale(double factor) 174 { 175 at.setToIdentity(); 176 at.scale(factor, factor); 177 transform(at); 178 } 179 180 public void performTranslate(int dx, int dy) 181 { 182 at.setToIdentity(); 183 at.translate(dx, dy); 184 transform(at); 185 } 186 187 public GeneralShape getHandleShape() { 188 return getGeneralShape(); 189 } 190 191 protected boolean fill = true;; 192 198 public void setFill(boolean b) { 199 fill = b; 200 super.setFill(b); 201 } 202 public boolean isFill() { 203 return fill; 204 } 205 206 207 222 public Rectangle getBounds() 223 { 224 if (getGeneralShape() != null) { 225 return J2DUtil.toDraw2D(getGeneralShape().getBounds()); 226 } 227 else { 228 return super.getBounds(); 229 } 230 } 231 232 } 233 | Popular Tags |