jfcurve.java
来自「用Java开发的、实现类似Visio功能的应用程序源码」· Java 代码 · 共 1,212 行 · 第 1/3 页
JAVA
1,212 行
/**
* $Id:JFCurve.java $
*
* Copyright 2004 ~ 2005 JingFei International Cooperation LTD. All rights reserved. *
*/
package com.jfimagine.jfgraph.shape.line;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.GeneralPath;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.RenderingHints;
import com.jfimagine.jfdom.Document;
import com.jfimagine.jfdom.Element;
import com.jfimagine.jfgraph.shape.base.AbstractObject;
import com.jfimagine.jfgraph.shape.base.AbstractShape;
import com.jfimagine.jfgraph.shape.base.ShapeConst;
import com.jfimagine.jfgraph.shape.base.ObjectList;
import com.jfimagine.jfgraph.shape.base.JFVersion;
import com.jfimagine.jfgraph.shape.base.Node;
import com.jfimagine.jfgraph.shape.base.Port;
import com.jfimagine.jfgraph.shape.decorate.LineFormat;
import com.jfimagine.jfgraph.shape.decorate.Arrow;
import com.jfimagine.jfgraph.shape.line.CurvePort;
import com.jfimagine.jfgraph.geom.JFPoint;
import com.jfimagine.jfgraph.geom.JFCurvePoint;
import com.jfimagine.jfgraph.geom.Curve;
import com.jfimagine.jfgraph.geom.Rect;
import com.jfimagine.jfgraph.geom.GeomConst;
/**
* A JFCurve class is used to represent a bezier curve with two end points and two control points.
*
* @author CookieMaker
*
* @version $Revision: 1.00 $
*/
public class JFCurve extends AbstractShape{
/**
* A XML string tag represents a Curve
*/
public static final String XML_CURVE ="Curve";
/**
* A XML string tag represents the x coordinates of start point
*/
public static final String XML_X1 ="X1";
/**
* A XML string tag represents the y coordinates of start point
*/
public static final String XML_Y1 ="Y1";
/**
* A XML string tag represents the x coordinates of control point1
*/
public static final String XML_X2 ="X2";
/**
* A XML string tag represents the y coordinates of control point1
*/
public static final String XML_Y2 ="Y2";
/**
* A XML string tag represents the x coordinates of control point3
*/
public static final String XML_X3 ="X3";
/**
* A XML string tag represents the y coordinates of control point3
*/
public static final String XML_Y3 ="Y3";
/**
* A XML string tag represents the x coordinates of end point
*/
public static final String XML_X4 ="X4";
/**
* A XML string tag represents the y coordinates of end point
*/
public static final String XML_Y4 ="Y4";
/**
* an internal curve object.
*/
private Curve m_curve;
/**
* arrow format of current line.
*/
protected Arrow m_arrow =new Arrow();
/**
* line format of current line.
*/
private LineFormat m_lineFormat = new LineFormat();
/**
* first node settled while drawing.
*/
protected JFPoint m_firstNode =new JFPoint();
/**
* while drawing rectangle, how many nodes have been decided. called by addnode method.
*/
protected int m_nodeAdded =0;
/**
* Constructor for Curve
*/
public JFCurve(){
setObjectType(ShapeConst.SHAPETYPE_CURVE);
setXMLTag(XML_CURVE);
m_curve =new Curve();
initNodes();
}
/**
* Constructor for Curve
* @param x1,y1 start point of this curve
* @param x2,y2 end point of this curve
*/
public JFCurve(double x1, double y1, double x2, double y2){
setObjectType(ShapeConst.SHAPETYPE_CURVE);
setXMLTag(XML_CURVE);
m_curve =new Curve();
addNode(x1,y1);
addNode(x2,y2);
finishDrawing();
}
/**
* Constructor for Curve
* @param x1,y1 start point of this curve
* @param x2,y2 end point of this curve
* @param ctrlx1,ctrly1 control point 1 of this curve
* @param ctrlx2,ctrly2 control point 2 of this curve
*/
public JFCurve(double x1, double y1, double x2, double y2,double ctrlx1, double ctrly1, double ctrlx2, double ctrly2){
setObjectType(ShapeConst.SHAPETYPE_CURVE);
setXMLTag(XML_CURVE);
m_curve =new Curve();
m_curve.setValue(x1,y1,ctrlx1,ctrly1,ctrlx2,ctrly2,x2,y2);
finishDrawing();
}
/**
* Get the arrow format of current object.
*
* @return The arrow format.
*
*/
public Arrow getArrow(){
return m_arrow;
}
/**
* Set the arrow format of current object.
*
* @param arrow A new arrow format object.
*
*/
public void setArrow(Arrow arrow){
m_arrow.setValue(arrow);
}
/**
* Get the line format of current line.
*
* @return The line format.
*
*/
public LineFormat getLineFormat(){
return m_lineFormat;
}
/**
* Set the line format of current line.
*
* @param lineFormat A new line format.
*
*/
public void setLineFormat(LineFormat lineFormat){
m_lineFormat =lineFormat;
}
/**
* Set the value of current curve.
*
* @param curve A new JFCurve object.
*
*/
public void setCurve(JFCurve curve){
m_curve.setValue(curve.m_curve);
initNodes();
}
/**
* Set the value of current curve.
*
* @param curve A new curve.
*
*/
public void setCurve(Curve curve){
m_curve.setValue(curve);
initNodes();
}
/**
* Set value of current Curve.
* We decide an curve by a startpoint and an endpoint, and use default controls here.
* This method is always used while drawing an curve when two end points are decided.
*
* @param startPoint The start point of this curve.
*
* @param endPoint The end point of this curve.
*
*/
public void setCurve(JFPoint startPoint, JFPoint endPoint){
m_curve.setValue(startPoint,endPoint);
initNodes();
}
/**
* Ask if this object is an open shape,e.g. line,curve,arc,etc.
*
* @return true if open,false closed.
*
*/
public boolean isOpenShape(){
return true;
}
/**
* Init all nodes of this curve.
* We will always consider that a bezier curve has four nodes: startpoint,endpoint and two controlpoints.
* node1 as start point
* node2 as control point1
* node3 as control point2
* node4 as end point
*/
protected void initNodes(){
//add four nodes if not assigned yet.
if (m_nodeList.size()<4){
try{ m_nodeList.clear(); }catch(Exception e){}
try{ m_nodeList.add(new Node()); }catch(Exception e){}
try{ m_nodeList.add(new Node()); }catch(Exception e){}
try{ m_nodeList.add(new Node()); }catch(Exception e){}
try{ m_nodeList.add(new Node()); }catch(Exception e){}
}
//set value of each node by m_curve.
Node node;
try {
node =(Node)m_nodeList.getByIndex(0);
node.setNodePoint(m_curve.getStartPoint());
node.setParent(this);
}catch(Exception e){
}
try{
node =(Node)m_nodeList.getByIndex(1);
node.setNodePoint(m_curve.getControlPoint1());
node.setParent(this);
}catch(Exception e){
}
try{
node =(Node)m_nodeList.getByIndex(2);
node.setNodePoint(m_curve.getControlPoint2());
node.setParent(this);
}catch(Exception e){
}
try {
node =(Node)m_nodeList.getByIndex(3);
node.setNodePoint(m_curve.getEndPoint());
node.setParent(this);
}catch(Exception e){
}
m_nodeList.setZoomScale(getZoomScale());
}
/**
* Init all ports of this curve.
* An object will always has its one or more stable ports and some customized ports,
* for JFLine, it will has two stable ports as below,
* port1 at start point of the line.
* port2 at end point of the line.
*/
protected void initPorts(){
if (m_portList.size()==0){
try{
JFPoint startPoint =m_curve.getStartPoint();
JFPoint endPoint =m_curve.getEndPoint();
//port at start point
m_portList.addPort(new CurvePort(this,Port.PORTTYPE_DEFAULT,startPoint,0));
//port at end point
m_portList.addPort(new CurvePort(this,Port.PORTTYPE_DEFAULT,endPoint,1));
}catch(Exception e){
}
}
m_portList.setZoomScale(getZoomScale());
}
/**
* Add a new node for current node list.
* here this method will always be called by DrawState class or any drawing class.
*
* @param x, y Coordinates of a new node.
*
*/
public void addNode(double x, double y){
//increase node added number.
m_nodeAdded++;
if (m_nodeAdded>2) m_nodeAdded=2;
//for a curve shape, the first node is to decide the left-top vertex,
//and the second noe is the last node to decide the right-bottom vertex.
if (m_nodeAdded==1){
m_firstNode.setValue(x,y);
}else if (m_nodeAdded>1){
//start point and end point.
JFPoint pnt1 =m_firstNode;
JFPoint pnt2 =new JFPoint(x,y);
setCurve(pnt1,pnt2);
}
m_nodeList.setZoomScale(getZoomScale());
}
/**
* Finish drawing object.
*
*/
public boolean finishDrawing(){
initNodes();
initPorts();
initLabel();
return true;
}
/**
* Remove last node added by addNode method.
* @return Last node that remained.
*
*/
public Node removeLastNode(){
//for a curve shape, removeLastNode is same as remove all nodes(last node is actually the first one).
m_nodeAdded=0;
return null;
}
/**
* If a curve has been drew.
* @return True when complete, false otherwise.
*
*/
public boolean ifCompleteDrawing(){
//two nodes added will decide a curve.
return (m_nodeAdded==2);
}
/**
* Get which node of current shape that intersects with point pnt.
*
* @param pnt A JFPoint used to test intersection.
* @param usage Purpose to get a node intersected, e.g. move or rotate.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?