Back to index

moin  1.9.0~rc2
ElbowHandle.java
Go to the documentation of this file.
00001 /*
00002  * @(#)ElbowHandle.java 5.1
00003  *
00004  */
00005 
00006 package CH.ifa.draw.figures;
00007 
00008 import java.awt.*;
00009 import CH.ifa.draw.framework.*;
00010 import CH.ifa.draw.standard.*;
00011 import CH.ifa.draw.util.Geom;
00012 
00016 public class ElbowHandle extends AbstractHandle {
00017 
00018     private int fSegment;
00019     private int fLastX, fLastY;      // previous mouse position
00020 
00021     public ElbowHandle(LineConnection owner, int segment) {
00022         super(owner);
00023         fSegment = segment;
00024     }
00025 
00026     public void invokeStart(int  x, int  y, DrawingView view) {
00027         fLastX = x;
00028         fLastY = y;
00029     }
00030 
00031     public void invokeStep (int x, int y, int anchorX, int anchorY, DrawingView view) {
00032         LineConnection line = ownerConnection();
00033         Point p1 = line.pointAt(fSegment);
00034         Point p2 = line.pointAt(fSegment+1);
00035         int ddx = x - fLastX;
00036         int ddy = y - fLastY;
00037 
00038         Point np1;
00039         Point np2;
00040         if (isVertical(p1, p2)) {
00041             int cx = constrainX(p1.x + ddx);
00042             np1 = new Point(cx, p1.y);
00043             np2 = new Point(cx, p2.y);
00044         } else {
00045             int cy = constrainY(p1.y + ddy);
00046             np1 = new Point(p1.x, cy);
00047             np2 = new Point(p2.x, cy);
00048         }
00049         line.setPointAt(np1, fSegment);
00050         line.setPointAt(np2, fSegment+1);
00051         fLastX = x;
00052         fLastY = y;
00053     }
00054 
00055     private boolean isVertical(Point p1, Point p2) {
00056         return p1.x == p2.x;
00057     }
00058 
00059     public Point locate() {
00060         LineConnection line = ownerConnection();
00061         int segment = Math.min(fSegment, line.pointCount()-2);
00062         Point p1 = line.pointAt(segment);
00063         Point p2 = line.pointAt(segment+1);
00064         return new Point((p1.x + p2.x)/2, (p1.y + p2.y)/2);
00065     }
00066 
00067     public void draw(Graphics g) {
00068         Rectangle r = displayBox();
00069 
00070         g.setColor(Color.yellow);
00071         g.fillOval(r.x, r.y, r.width, r.height);
00072 
00073         g.setColor(Color.black);
00074         g.drawOval(r.x, r.y, r.width, r.height);
00075     }
00076 
00077     private int constrainX(int x) {
00078         LineConnection line = ownerConnection();
00079         Figure startFigure = line.start().owner();
00080         Figure endFigure = line.end().owner();
00081         Rectangle start = startFigure.displayBox();
00082         Rectangle end = endFigure.displayBox();
00083         Insets i1 = startFigure.connectionInsets();
00084         Insets i2 = endFigure.connectionInsets();
00085 
00086         int r1x, r1width, r2x, r2width;
00087         r1x = start.x + i1.left;
00088         r1width = start.width - i1.left - i1.right-1;
00089 
00090         r2x = end.x + i2.left;
00091         r2width = end.width - i2.left - i2.right-1;
00092 
00093         if (fSegment == 0)
00094             x = Geom.range(r1x, r1x + r1width, x);
00095         if (fSegment == line.pointCount()-2)
00096             x = Geom.range(r2x, r2x + r2width, x);
00097         return x;
00098     }
00099 
00100     private int constrainY(int y) {
00101         LineConnection line = ownerConnection();
00102         Figure startFigure = line.start().owner();
00103         Figure endFigure = line.end().owner();
00104         Rectangle start = startFigure.displayBox();
00105         Rectangle end = endFigure.displayBox();
00106         Insets i1 = startFigure.connectionInsets();
00107         Insets i2 = endFigure.connectionInsets();
00108 
00109         int r1y, r1height, r2y, r2height;
00110         r1y = start.y + i1.top;
00111         r1height = start.height - i1.top - i1.bottom-1;
00112         r2y = end.y + i2.top;
00113         r2height = end.height - i2.top - i2.bottom-1;
00114 
00115         if (fSegment == 0)
00116             y = Geom.range(r1y, r1y + r1height, y);
00117         if (fSegment == line.pointCount()-2)
00118             y = Geom.range(r2y, r2y + r2height, y);
00119         return y;
00120     }
00121 
00122     private LineConnection ownerConnection() {
00123         return (LineConnection)owner();
00124     }
00125 
00126 }