📄 arcpath.java
字号:
/* * Created on 12-Feb-2004 * * To change the template for this generated file go to * Window - Preferences - Java - Code Generation - Code and Comments */package pipe.dataLayer;import java.awt.BasicStroke;import java.awt.Rectangle;import java.awt.Shape;import java.awt.Stroke;import java.awt.geom.AffineTransform;import java.awt.geom.GeneralPath;import java.awt.geom.PathIterator;import java.awt.geom.Point2D;import java.awt.geom.Rectangle2D;import java.util.ArrayList;import java.util.List;import pipe.gui.ArcPathPointHandler;import pipe.gui.Constants;import pipe.gui.GuiView;/** * @author Peter Kyme, Tom Barnwell and Michael Camacho * * To change the template for this generated type comment go to * Window - Preferences - Java - Code Generation - Code and Comments */public class ArcPath implements Shape, Cloneable, Constants { // private static final int CONTROL_POINT_CONSTANT = 2;// private static final int PATH_SELECTION_RADIUS = 6; private GeneralPath path = new GeneralPath(); private GeneralPath arcSelection; private GeneralPath pointSelection; private List pathPoints = new ArrayList(); private Arc myArc; ArcPathPoint currentPoint; private boolean pointLock = false; private static Stroke proximityStroke = new BasicStroke(ARC_PATH_PROXIMITY_WIDTH/*, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER*/); private static Stroke stroke = new BasicStroke(ARC_PATH_SELECTION_WIDTH); private Shape shape, proximityShape; private int transitionAngle; private boolean showControlPoints = false; public ArcPath(Arc a){ myArc = a; transitionAngle = 0; } public void createPath() { setControlPoints(); currentPoint = null; path = new GeneralPath(); currentPoint = (ArcPathPoint)pathPoints.get(0); path.moveTo(currentPoint.getPoint().x, currentPoint.getPoint().y); getArc().setWeightLabelPosition(); currentPoint.setPointType(ArcPathPoint.STRAIGHT); for (int c = 1; c <= getEndIndex(); c++) { currentPoint = (ArcPathPoint)pathPoints.get(c); if (currentPoint.getPointType() == ArcPathPoint.STRAIGHT){ path.lineTo(currentPoint.getPoint().x, currentPoint.getPoint().y); } else if (currentPoint.getPointType() == ArcPathPoint.CURVED){ if (showControlPoints){//draw control lines for illustrative purposes path.lineTo(currentPoint.getControl1().x,currentPoint.getControl1().y); path.lineTo(currentPoint.getControl2().x,currentPoint.getControl2().y); path.lineTo(currentPoint.getPoint().x, currentPoint.getPoint().y); path.moveTo(((ArcPathPoint)pathPoints.get(c-1)).getPoint().x, ((ArcPathPoint)pathPoints.get(c-1)).getPoint().y); } path.curveTo(currentPoint.getControl1().x,currentPoint.getControl1().y,currentPoint.getControl2().x,currentPoint.getControl2().y,currentPoint.getPoint().x,currentPoint.getPoint().y); } } shape = stroke.createStrokedShape(this); proximityShape = proximityStroke.createStrokedShape(this); } private void setControlPoints(){ setCurveControlPoints(); //must be in this order setStraightControlPoints(); setEndControlPoints(); } /* returns a control point for curve CD with incoming vector AB*/ private Point2D.Float getControlPoint(Point2D.Float A, Point2D.Float B, Point2D.Float C, Point2D.Float D){ Point2D.Float p = new Point2D.Float(0, 0); double modAB = getMod(A, B); double modCD = getMod(C, D); double ABx = (B.x - A.x) / modAB; double ABy = (B.y - A.y) / modAB; if (modAB < 7) // hack, stops division by zero, modAB can only be this low // if the points are virtually superimposed anyway p = (Point2D.Float)C.clone(); else{ p.x = C.x + (float) (ABx * modCD / ARC_CONTROL_POINT_CONSTANT ); p.y = C.y + (float) (ABy * modCD / ARC_CONTROL_POINT_CONSTANT ); } return p; } private double getMod(Point2D.Float A, Point2D.Float B){ double ABx = A.x - B.x; double ABy = A.y - B.y; return Math.sqrt(ABx*ABx + ABy*ABy); } /* function sets control points for any curved sections of the path */ private void setCurveControlPoints(){ if(pathPoints.size()<1) return; ArcPathPoint myCurrentPoint = (ArcPathPoint)pathPoints.get(0); myCurrentPoint.setPointType(ArcPathPoint.STRAIGHT); // ArcPathPoint myPreviousButOnePoint = null;// ArcPathPoint myNextPoint = null;// ArcPathPoint myPreviousPoint = null; Cubic[] X,Y; int endIndex = getEndIndex(); for (int c=1; c <= endIndex;){ int curveStartIndex = 0; int curveEndIndex = 0; myCurrentPoint = (ArcPathPoint)pathPoints.get(c); if (myCurrentPoint.getPointType()==true){ curveStartIndex = c-1; for(; c<= endIndex && myCurrentPoint.getPointType()==true; c++){ myCurrentPoint = (ArcPathPoint)pathPoints.get(c); curveEndIndex = c; } /* calculate a cubic for each section of the curve */ int lengthOfCurve = curveEndIndex - curveStartIndex; int k1; int x[] = new int[lengthOfCurve + 2]; int y[] = new int[lengthOfCurve + 2]; X = new Cubic[lengthOfCurve + 2]; Y = new Cubic[lengthOfCurve + 2]; for (k1= 0; k1 <= (curveEndIndex - curveStartIndex); k1++) { x[k1] = (int)((ArcPathPoint)pathPoints.get(curveStartIndex + k1)).getPoint().x; y[k1] = (int)((ArcPathPoint)pathPoints.get(curveStartIndex + k1)).getPoint().y; } x[k1] = x[k1-1]; y[k1] = y[k1-1]; X = calcNaturalCubic(k1,x); Y = calcNaturalCubic(k1,y); for (int k2 = 1; k2 <= lengthOfCurve; k2++){ myCurrentPoint = (ArcPathPoint)pathPoints.get(k2 + curveStartIndex); myCurrentPoint.setControl1(X[k2-1].getX1(),Y[k2-1].getX1()); myCurrentPoint.setControl2(X[k2-1].getX2(),Y[k2-1].getX2()); } } else { c++; } } } /* fuction sets the control points for any straight sections * and for smooth intersection between straight and curved sections */ private void setStraightControlPoints() { ArcPathPoint myCurrentPoint = (ArcPathPoint)pathPoints.get(0); ArcPathPoint myPreviousButOnePoint = null; ArcPathPoint myNextPoint = null; ArcPathPoint myPreviousPoint = null; for (int c=1; c<=getEndIndex(); c++){ myPreviousPoint = (ArcPathPoint)pathPoints.get(c-1); myCurrentPoint = (ArcPathPoint)pathPoints.get(c); if (myCurrentPoint.getPointType() == false){ myCurrentPoint.setControl1(getControlPoint(myPreviousPoint.getPoint(), myCurrentPoint.getPoint(), myPreviousPoint.getPoint(), myCurrentPoint.getPoint())); myCurrentPoint.setControl2(getControlPoint(myCurrentPoint.getPoint(), myPreviousPoint.getPoint(), myCurrentPoint.getPoint(), myPreviousPoint.getPoint())); } else { if (c>1 && myPreviousPoint.getPointType() == false){ myPreviousButOnePoint = (ArcPathPoint)pathPoints.get(c-2); myCurrentPoint.setControl1(getControlPoint(myPreviousButOnePoint.getPoint(), myPreviousPoint.getPoint(), myPreviousPoint.getPoint(), myCurrentPoint.getPoint())); } if (c<getEndIndex()){ myNextPoint = (ArcPathPoint)pathPoints.get(c+1); if (myNextPoint.getPointType() == false){ myCurrentPoint.setControl2(getControlPoint(myNextPoint.getPoint(), myCurrentPoint.getPoint(), myCurrentPoint.getPoint(), myPreviousPoint.getPoint())); } } } } } private void setEndControlPoints() {// Transition endTransition; PlaceTransitionObject source = getArc().getSource(); PlaceTransitionObject target = getArc().getTarget(); double anAngle = Math.toRadians(transitionAngle); if (!(getEndIndex() > 0)) return; else if (source != null && source instanceof Transition && ((ArcPathPoint)pathPoints.get(1)).getPointType() == true) { ArcPathPoint myPoint = (ArcPathPoint)pathPoints.get(1); ArcPathPoint myLastPoint = (ArcPathPoint)pathPoints.get(0); float distance = (float)getMod(myPoint.getPoint(), myLastPoint.getPoint())/ARC_CONTROL_POINT_CONSTANT; myPoint.setControl1((float)(myLastPoint.getPoint().x + Math.cos(anAngle)*distance), (float)(myLastPoint.getPoint().y + Math.sin(anAngle)*distance) ); myPoint = (ArcPathPoint)pathPoints.get(getEndIndex()); myPoint.setControl2(getControlPoint(myPoint.getPoint(), myPoint.getControl1(), myPoint.getPoint(), myPoint.getControl1())); } else if (target != null && source instanceof Place&& ((ArcPathPoint)pathPoints.get(getEndIndex())).getPointType() == true){ ArcPathPoint myPoint = (ArcPathPoint)pathPoints.get(getEndIndex()); ArcPathPoint myLastPoint = (ArcPathPoint)pathPoints.get(getEndIndex()-1); float distance = (float)getMod(myPoint.getPoint(), myLastPoint.getPoint())/ARC_CONTROL_POINT_CONSTANT; myPoint.setControl2((float)(myPoint.getPoint().x + Math.cos(anAngle)*distance), (float)(myPoint.getPoint().y + Math.sin(anAngle)*distance) ); myPoint = (ArcPathPoint)pathPoints.get(1); myPoint.setControl1(getControlPoint(((ArcPathPoint)pathPoints.get(0)).getPoint(), myPoint.getControl2(), ((ArcPathPoint)pathPoints.get(0)).getPoint(), myPoint.getControl2())); } } public GeneralPath getPath() { return path; } public GeneralPath getArcSelection() { return arcSelection; } public GeneralPath getPointSelection() { return pointSelection; } public void addPoint(float x, float y, boolean type) { pathPoints.add(new ArcPathPoint(x, y, type, this)); } public void addPoint(double x, double y, boolean type) { pathPoints.add(new ArcPathPoint((float)x, (float)y, type, this)); } public void addPoint() { pathPoints.add(new ArcPathPoint(this)); } public void deletePoint(ArcPathPoint a) { pathPoints.remove(a); } public void updateArc() { myArc.updateArcPosition(); } public void translatePoint(int index, float x, float y) { ArcPathPoint point = (ArcPathPoint)pathPoints.get(index); point.setPointLocation(point.getPoint().x+x,point.getPoint().y+y); } /* (non-Javadoc) * @see java.awt.Shape#contains(double, double) */ public boolean contains(double arg0, double arg1) { // TODO Auto-generated method stub return false; } public int getEndIndex() { return pathPoints.size()-1; } // public void setPointLocation(int index, float x, float y) {// ((ArcPathPoint)pathPoints.get(index)).setPointLocation(x,y);// }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -