1 8 package com.nightlabs.editor2d.figures; 9 10 import java.awt.Graphics2D ; 11 import java.awt.geom.Area ; 12 13 import org.apache.log4j.Logger; 14 import org.eclipse.draw2d.Figure; 15 import org.eclipse.draw2d.Graphics; 16 import org.eclipse.draw2d.J2DGraphics; 17 import org.eclipse.draw2d.geometry.Rectangle; 18 import org.eclipse.gef.editparts.ZoomListener; 19 20 import com.nightlabs.editor2d.DrawComponent; 21 import com.nightlabs.editor2d.ShapeDrawComponent; 22 import com.nightlabs.editor2d.j2d.GeneralShape; 23 import com.nightlabs.editor2d.render.Renderer; 24 import com.nightlabs.editor2d.util.J2DUtil; 25 26 27 public class DrawComponentFigure 28 extends Figure 30 implements RendererFigure 31 { 32 public static final Logger LOGGER = Logger.getLogger(DrawComponentFigure.class.getName()); 33 34 protected J2DGraphics j2d; 35 protected Graphics2D g2d; 36 public void paint(Graphics graphics) 37 { 38 if (graphics instanceof J2DGraphics) { 39 j2d = (J2DGraphics) graphics; 40 g2d = j2d.createGraphics2D(); 41 g2d.setClip(null); 42 if (renderer != null) 43 renderer.paint(drawComponent, g2d); 44 g2d.dispose(); 45 } 46 } 47 48 public void paint(Graphics2D graphics) 49 { 50 if (renderer == null && drawComponent != null) 51 renderer = drawComponent.getRenderer(); 52 if (renderer != null) 53 renderer.paint(drawComponent, graphics); 54 } 55 56 protected Renderer renderer; 57 public void setRenderer(Renderer renderer) { 58 this.renderer = renderer; 59 } 60 61 protected DrawComponent drawComponent; 62 public void setDrawComponent(DrawComponent drawComponent) { 63 this.drawComponent = drawComponent; 64 } 65 66 public boolean containsPoint(int x, int y) 67 { 68 if (drawComponent != null) { 69 if (drawComponent instanceof ShapeDrawComponent) { 70 ShapeDrawComponent sdc = (ShapeDrawComponent) drawComponent; 71 if (sdc.isFill()) { 72 return sdc.getGeneralShape().contains(x,y); 73 } 74 else 75 { 76 if (outlineArea == null) 77 { 78 Rectangle outerBounds = getBounds().getCopy(); 79 Rectangle innerBounds = getBounds().getCopy(); 80 outerBounds.expand((int)hitTolerance, (int)hitTolerance); 81 innerBounds.shrink((int)hitTolerance, (int)hitTolerance); 82 GeneralShape outerGS = (GeneralShape) sdc.getGeneralShape().clone(); 83 GeneralShape innerGS = (GeneralShape) sdc.getGeneralShape().clone(); 84 J2DUtil.transformGeneralShape(outerGS, getBounds(), outerBounds); 85 J2DUtil.transformGeneralShape(innerGS, getBounds(), innerBounds); 86 outlineArea = new Area (outerGS); 87 Area innerArea = new Area (innerGS); 88 outlineArea.exclusiveOr(innerArea); 89 } 90 boolean contains = outlineArea.contains(x,y); 91 outlineArea = null; 92 return contains; 93 } 94 } 95 } 96 return super.containsPoint(x, y); 97 } 98 99 public static final double DEFAULT_HIT_TOLERANCE = 3; 100 protected double hitTolerance = DEFAULT_HIT_TOLERANCE; 101 public double getHitTolerance() { 102 return hitTolerance; 103 } 104 public void setHitTolerance(double hitTolerance) { 105 this.hitTolerance = hitTolerance; 106 } 107 108 protected Area outlineArea; 109 110 protected ZoomListener zoomListener = new ZoomListener() 111 { 112 public void zoomChanged(double zoom) 113 { 114 hitTolerance = DEFAULT_HIT_TOLERANCE / zoom; 115 } 116 }; 117 public ZoomListener getZoomListener() { 118 return zoomListener; 119 } 120 } 121 | Popular Tags |