jfcurve.java
来自「用Java开发的、实现类似Visio功能的应用程序源码」· Java 代码 · 共 1,212 行 · 第 1/3 页
JAVA
1,212 行
switch (moveCase){
case ShapeConst.MOVEMENT_NORMAL:
return movePos;
case ShapeConst.MOVEMENT_SHIFTDOWN:
case ShapeConst.MOVEMENT_CTRLDOWN:
try{
Node node1 =(Node)m_nodeList.getByIndex(0);
Node node2 =(Node)m_nodeList.getByIndex(1);
Node node3 =(Node)m_nodeList.getByIndex(2);
Node node4 =(Node)m_nodeList.getByIndex(3);
//unchange two control points.
if (node.equals(node2) || node.equals(node3))
return movePos;
JFPoint baseNode;
if (node.equals(node1))
baseNode =m_curve.getEndPoint();
else
baseNode =m_curve.getStartPoint();
//offset between current moving position and baseNode
double x =movePos.getX() - baseNode.getX();
double y =movePos.getY() - baseNode.getY();
if (Math.abs(x)>Math.abs(y)){
movePos.setY(baseNode.getY());
}else{
movePos.setX(baseNode.getX());
}
return movePos;
}catch(Exception e){
}
}
return movePos;
}
/**
* Start move a node.
*
* @param node The node will be moved.
*
*/
public void startMoveNode(Node node){
startMoveLabel();
}
/**
* finish move/adjust a node of current object.
*
* @param node Currently moving node.
*
* @param x, y Moving offsets.
*
* @param g current drawing canvas.
*
*/
public void finishMoveNode(Node node, double x, double y,Graphics g){
finishMoveLabel();
moveNode(node,x,y,g);
finishMovePort();
}
/**
* Move/adjust a node of current object.
*
* @param node Currently moving node.
*
* @param x, y Moving offsets.
*
* @param g current drawing canvas.
*
*/
public void moveNode(Node node, double x, double y,Graphics g){
if (node!=null){
try{
boolean dragControl =false;
if (node.equals((Node)m_nodeList.getByIndex(0))){
JFPoint startPoint =new JFPoint(x,y);
JFPoint endPoint =m_curve.getEndPoint();
m_curve.setValueByKeepingShape(startPoint,endPoint);
}else if (node.equals((Node)m_nodeList.getByIndex(1))){
m_curve.setControlPoint1(x,y);
dragControl =true;
}else if (node.equals((Node)m_nodeList.getByIndex(2))){
m_curve.setControlPoint2(x,y);
dragControl =true;
}else if (node.equals((Node)m_nodeList.getByIndex(3))){
JFPoint startPoint =m_curve.getStartPoint();
JFPoint endPoint =new JFPoint(x,y);
m_curve.setValueByKeepingShape(startPoint,endPoint);
}else
return;
initNodes();
draw(g,true);
if (dragControl)
drawControlHandles(g,true);
}catch(Exception e){
}
}
}
/**
* Move/adjust a port of current object.
* A move port event will always occured by relative objects' moving event.
*
* @param port Currently moving port.
* @param x, y new position of this port.
* @return True if successfully moved, false otherwise.
*
*/
public boolean movePort(Port port, double x, double y){
if (port==null || port.getParentId()!=getObjectId())
return false;
//not allowed to move customized ports.
if (port.getPortType()==Port.PORTTYPE_CUSTOM)
return false;
int portIndex =m_portList.getIndex(port);
Node node;
try{
if (portIndex==0){
//the start node.
node =(Node)m_nodeList.getByIndex(0);
}else if (portIndex==1){
//the end node.
node =(Node)m_nodeList.getByIndex(m_nodeList.size()-1);
}else{
return false;
}
}catch(Exception e){
return false;
}
//move the port relative nodes will move both nodes and ports.
startMoveNode(node);
moveNode(node,x,y,null);
finishMoveNode(node,x,y,null);
return true;
}
/**
* Convert this object to String <br>
*
* @return An string represents the content of the object
*
*/
public String toString(){
StringBuffer buf=new StringBuffer();
buf.append(super.toString());
buf.append("\n<arrowType>");
buf.append(m_arrow.toString());
buf.append("\n<lineFormat>");
buf.append(m_lineFormat.toString());
buf.append(m_curve.toString());
return buf.toString();
}
/**
* Creates a new AbstractObject of the same class and with the same contents as this object.
* This method implements the method defined in AbstractObject.
*
* @return A clone of this class.
*
*/
protected AbstractObject cloneMe() throws CloneNotSupportedException{
return new JFCurve();
}
/**
* Creates a new object of the same class and with the same contents as this object.
*
* @return A clone of this instance.
*
*/
public Object clone() throws CloneNotSupportedException{
try{
Object obj =super.clone();
if (obj==null){
return null;
}
JFCurve curve =(JFCurve) obj;
curve.m_arrow.setValue(m_arrow);
curve.m_lineFormat.setValue(m_lineFormat);
curve.m_curve.setValue(m_curve);
return curve;
}catch(Exception e){
throw new CloneNotSupportedException(e.getMessage());
}
}
/**
* Returns the hashcode for this Object.
*
* @return hash code for this Point2D.
*
*/
public int hashCode(){
return super.hashCode() ^
m_arrow.hashCode()^
m_lineFormat.hashCode()^
m_curve.hashCode();
}
/**
* Determines whether or not two objects are equal.
*
* @param obj an object to be compared with this object
*
* @return true if the object to be compared is an instance of Port and has the same values; false otherwise.
*
*/
public boolean equals(Object obj){
if (!super.equals(obj))
return false;
if (obj == this)
return true;
if (!(obj instanceof JFCurve))
return false;
JFCurve curve= (JFCurve)obj;
return (curve.m_arrow.equals(m_arrow)) &&
(curve.m_lineFormat.equals(m_lineFormat)) &&
(curve.m_curve.equals(m_curve));
}
/**
* Append necessary xml child for current element,
* this method will be called internally by toDOM.
*
* @param element A XML element to append child xml nodes
* @param version A file version notification so this object can obey the rules to save data.
*
*/
protected void appendChildToDOM(Element element,JFVersion version){
if (element==null)
return;
super.appendChildToDOM(element,version);
m_arrow.toDOM(element,version);
m_lineFormat.toDOM(element,version);
JFPoint startPoint =m_curve.getStartPoint();
element.addChild(new Element(XML_X1, startPoint.getX()));
element.addChild(new Element(XML_Y1, startPoint.getY()));
JFPoint controlPoint1 =m_curve.getControlPoint1();
element.addChild(new Element(XML_X2, controlPoint1.getX()));
element.addChild(new Element(XML_Y2, controlPoint1.getY()));
JFPoint controlPoint2 =m_curve.getControlPoint2();
element.addChild(new Element(XML_X3, controlPoint2.getX()));
element.addChild(new Element(XML_Y3, controlPoint2.getY()));
JFPoint endPoint =m_curve.getEndPoint();
element.addChild(new Element(XML_X4, endPoint.getX()));
element.addChild(new Element(XML_Y4, endPoint.getY()));
}
/**
* Extract needed xml child from current element,
* this method will be called internally by fromDOM.
*
* @param element An element used to extract needed xml child
* @param version A file version notification so this object can obey the rules to fetch data.
*
*/
protected void extractChildFromDOM(Element element,JFVersion version){
if (element==null)
return;
super.extractChildFromDOM(element,version);
m_arrow.fromDOM(element.getChild(m_arrow.getXMLTag()),version);
m_lineFormat.fromDOM(element.getChild(m_lineFormat.getXMLTag()),version);
double x=0, y=0;
x =Element.getDoubleValue(element.getChild( XML_X1));
y =Element.getDoubleValue(element.getChild( XML_Y1));
m_curve.setStartPoint(x,y);
x =Element.getDoubleValue(element.getChild( XML_X2));
y =Element.getDoubleValue(element.getChild( XML_Y2));
m_curve.setControlPoint1(x,y);
x =Element.getDoubleValue(element.getChild( XML_X3));
y =Element.getDoubleValue(element.getChild( XML_Y3));
m_curve.setControlPoint2(x,y);
x =Element.getDoubleValue(element.getChild( XML_X4));
y =Element.getDoubleValue(element.getChild( XML_Y4));
m_curve.setEndPoint(x,y);
initNodes();
}
/**
* Save this object to a binary stream
*
* @param stream An binary output stream
*
* @param version A file version notification so this object can obey the rules to save data.
* @exception java.io.IOException
*
*/
public void saveToStream(com.jfimagine.utils.io.JFWriter stream,JFVersion version) throws IOException{
super.saveToStream(stream,version);
m_arrow.saveToStream(stream,version);
m_lineFormat.saveToStream(stream,version);
JFPoint startPoint=m_curve.getStartPoint();
stream.writeDouble(startPoint.getX());
stream.writeDouble(startPoint.getY());
JFPoint controlPoint1=m_curve.getControlPoint1();
stream.writeDouble(controlPoint1.getX());
stream.writeDouble(controlPoint1.getY());
JFPoint controlPoint2=m_curve.getControlPoint2();
stream.writeDouble(controlPoint2.getX());
stream.writeDouble(controlPoint2.getY());
JFPoint endPoint=m_curve.getEndPoint();
stream.writeDouble(endPoint.getX());
stream.writeDouble(endPoint.getY());
}
/**
* Load object data from a binary stream <br>
*
* @param stream An binary input stream
*
* @param skipHead Skip head 'TYPE' check, an shape object should always
* has its own shape-type stored, if this shape-type has already been readed,
* this loadFromStream should/could not read the type anymore.
*
* @param version A file version notification so this object can obey the rules to fetch data.
* @exception java.io.IOException
*
*/
public void loadFromStream(com.jfimagine.utils.io.JFReader stream,boolean skipHead,JFVersion version) throws IOException{
super.loadFromStream(stream,skipHead,version);
m_arrow.loadFromStream(stream,false,version);
m_lineFormat.loadFromStream(stream,false,version);//don't skip head here
double x=0,y=0;
x =stream.readDouble();
y =stream.readDouble();
m_curve.setStartPoint(x,y);
x =stream.readDouble();
y =stream.readDouble();
m_curve.setControlPoint1(x,y);
x =stream.readDouble();
y =stream.readDouble();
m_curve.setControlPoint2(x,y);
x =stream.readDouble();
y =stream.readDouble();
m_curve.setEndPoint(x,y);
initNodes();
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?