📄 abstractarc.java
字号:
if (node1.equals(node)){
m_arc.moveStartPointTo(node.getNodePoint());
initNodes();
draw(g,true);
return;
}
}catch(Exception e){
}
//end point
try{
node1 =(Node)m_nodeList.getByIndex(1);
if (node1.equals(node)){
m_arc.moveEndPointTo(node.getNodePoint());
initNodes();
draw(g,true);
return;
}
}catch(Exception e){
}
//control point
try{
node1 =(Node)m_nodeList.getByIndex(2);
if (node1.equals(node)){
m_arc.moveControlPointTo(node.getNodePoint());
initNodes();
draw(g,true);
return;
}
}catch(Exception e){
}
//center point
try{
if (m_nodeList.size()>3){
node1 =(Node)m_nodeList.getByIndex(3);
if (node1.equals(node)){
m_arc.moveCenterPointTo(node.getNodePoint());
initNodes();
draw(g,true);
return;
}
}
}catch(Exception e){
}
}
/**
* Add a new port to this shape.
* @param x,y Current picking position.
*
*/
public Port addPort(double x,double y){
JFArcPoint portPoint =m_arc.intersectsAt(x,y,GeomConst.PICK_OFFSET);
if (portPoint==null)
return null;
Port newPort =m_portList.addPort(new ArcPort(this,Port.PORTTYPE_CUSTOM,portPoint,portPoint.getScale(),portPoint.getType()));
m_portList.setZoomScale(getZoomScale());
return newPort;
}
/**
* When moving a node, in some cases, e.g. ctrl key or shift key pressed to force preserving
* the form of a shape, or force equilateral shapes, we need to recalculate the actual moving
* node's position.
*
* @param movePos Desire moving position of a node.
* @param moveCase Move case of a node, normal, shift key pressed or ctrl key pressed
* @return The actual moving position of a node.
*
*/
public JFPoint getMoveNodePos(Node node,JFPoint movePos,int moveCase){
if (node==null || movePos==null)
return null;
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);
//only change start node and end node.
if (!node.equals(node1) && !node.equals(node2))
return movePos;
JFPoint baseNode;
if (node.equals(node1))
baseNode =node2.getNodePoint();
else
baseNode =node1.getNodePoint();
//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;
}
/**
* 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){
if (node.getXOffset()==x && node.getYOffset()==y){
//only eliminate old dragging shape here
if (g!=null)
draw(g,true);
}else{
node.setXOffset(x);
node.setYOffset(y);
drawMovingNode(g,node);
}
}
}
/**
* 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){
moveNode(node,x,y,g);
finishMoveLabel();
//re-set all ports by values of m_arc.
Iterator it =m_portList.getList().iterator();
while (it!=null && it.hasNext()){
ArcPort port=(ArcPort)it.next();
port.setPortPoint(m_arc.getPoint(port.getPointType(),port.getScale()));
}
m_portList.setZoomScale(getZoomScale());
if (g!=null)
draw(g,false);
}
/**
* Scale current object by specified points and scale percent.
* We only support a concurrent width-height scale here, suppose width as the length from
* basePoint to refPoint1, height as the length from basePoint to refPoint2, and
* one scale percent acts on both width and height.
*
* @param basePoint A base point that is unmovable.
* @param refPoint1 A 'width' reference point.
* @param refPoint2 A 'height' reference point.
* @param scale A reference scale percent.
*
*/
public void scaleBy(JFPoint basePoint, JFPoint refPoint1, JFPoint refPoint2, double scale){
startMoveLabel();
m_arc.scaleBy(basePoint,refPoint1,refPoint2,scale);
JFPoint center =m_arc.getCenter();
m_portList.scaleBy(center,basePoint,refPoint1,refPoint2,scale);
initNodes();
finishMoveLabel();
}
/**
* Scale current object by a specified x and y scale.<br>
* This is a special scale method used to scale a shape in arbitrary x and y scale.<br>
* Please see AbstractShape.scaleBy for detailed description.
*
* @param basePoint A base scale point for scaling reference.
* @param xScale A scale percentage in x coordinate, default to 1.0
* @param yScale A scale percentage in y coordinate, default to 1.0
*
*/
public void scaleBy(JFPoint basePoint,double xScale, double yScale){
startMoveLabel();
m_arc.scaleBy(basePoint,xScale,yScale);
m_portList.scaleBy(basePoint,xScale,yScale);
initNodes();
finishMoveLabel();
}
/**
* Move current object by an x and y offset.
*
* @param x, y Moving offsets.
*
*/
public void moveBy(double x, double y){
m_arc.moveBy(x,y);
initNodes();
m_portList.moveBy(x,y);
m_label.moveBy(x,y);
}
/**
* Rotate current object by a specified point and an angle theta.
*
* @param baseX, baseY A rotate center coordinates.
*
* @param theta A rotate angle.
*
*/
public void rotateBy(double baseX,double baseY, double theta){
startMoveLabel();
m_arc.rotateBy(baseX,baseY,theta);
initNodes();
m_portList.rotateBy(baseX,baseY,theta);
finishMoveLabel();
}
/**
* Rotate this rectangle by an angle theta.
*
* @param theta A rotate angle.
*
*/
public void rotateBy(double theta){
JFPoint center =m_arc.getCenter();
rotateBy(center.getX(),center.getY(),theta);
}
/**
* Mirror this rectangle by a central x coordinate of this rectangle. We make a left-right flip here.
*/
public void mirrorBy(){
JFPoint center =m_arc.getCenter();
mirrorBy(center.getX());
}
/**
* Mirror this rectangle by a x coordinate. We make a left-right mirror here.
*
* @param baseX A mirror base x coordinate.
*
*/
public void mirrorBy(double baseX){
startMoveLabel();
m_arc.mirrorBy(baseX);
initNodes();
m_portList.mirrorBy(baseX);
finishMoveLabel();
}
/**
* Reverse this rectangle by a central y coordinate of this rectangle. We make a up-down flip here.
*/
public void flipBy(){
JFPoint center =m_arc.getCenter();
flipBy(center.getY());
}
/**
* Reverse this rectangle by a y coordinate. We make a up-down flip here.
*
* @param baseY A flip base y coordinate.
*
*/
public void flipBy(double baseY){
startMoveLabel();
m_arc.flipBy(baseY);
initNodes();
m_portList.flipBy(baseY);
finishMoveLabel();
}
/**
* 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(m_arc.toString());
buf.append("\n<arrowType>");
buf.append(m_arrow.toString());
buf.append("\n<fillFormat>");
buf.append(m_fillFormat.toString());
buf.append("\n<lineFormat>");
buf.append(m_lineFormat.toString());
return buf.toString();
}
/**
* 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;
}
AbstractArc arc =(AbstractArc) obj;
arc.m_arc.setValue(m_arc);
arc.m_arrow.setValue(m_arrow);
arc.m_lineFormat.setValue(m_lineFormat);
arc.m_fillFormat.setValue(m_fillFormat);
return arc;
}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_arc.hashCode() ^
m_lineFormat.hashCode()^
m_fillFormat.hashCode()^
m_arrow.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 AbstractArc))
return false;
AbstractArc arc= (AbstractArc)obj;
return (arc.m_arc.equals(m_arc))&&
(arc.m_arrow.equals(m_arrow)) &&
(arc.m_lineFormat.equals(m_lineFormat))&&
(arc.m_fillFormat.equals(m_fillFormat));
}
/**
* 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);
JFPoint startPoint =m_arc.getStartPoint();
JFPoint endPoint =m_arc.getEndPoint();
JFPoint controlPoint =m_arc.getControlPoint();
element.addChild(new Element(XML_STARTPOINTX, startPoint.getX()));
element.addChild(new Element(XML_STARTPOINTY, startPoint.getY()));
element.addChild(new Element(XML_ENDPOINTX, endPoint.getX()));
element.addChild(new Element(XML_ENDPOINTY, endPoint.getY()));
element.addChild(new Element(XML_CONTROLPOINTX, controlPoint.getX()));
element.addChild(new Element(XML_CONTROLPOINTY, controlPoint.getY()));
element.addChild(new Element(XML_ARCTYPE, m_arc.getArcType()));
m_arrow.toDOM(element,version);
m_lineFormat.toDOM(element,version);
m_fillFormat.toDOM(element,version);
}
/**
* 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);
double startPointX =Element.getDoubleValue(element.getChild(XML_STARTPOINTX));
double startPointY =Element.getDoubleValue(element.getChild(XML_STARTPOINTY));
double endPointX =Element.getDoubleValue(element.getChild(XML_ENDPOINTX));
double endPointY =Element.getDoubleValue(element.getChild(XML_ENDPOINTY));
double controlPointX =Element.getDoubleValue(element.getChild(XML_CONTROLPOINTX));
double controlPointY =Element.getDoubleValue(element.getChild(XML_CONTROLPOINTY));
int arcType =Element.getIntValue(element.getChild(XML_ARCTYPE));
m_arc.setStartPoint(new JFPoint(startPointX,startPointY));
m_arc.setEndPoint(new JFPoint(endPointX,endPointY));
m_arc.setControlPoint(new JFPoint(controlPointX,controlPointY));
m_arc.setArcType(arcType);
m_arrow.fromDOM(element.getChild(m_arrow.getXMLTag()),version);
m_lineFormat.fromDOM(element.getChild(m_lineFormat.getXMLTag()),version);
m_fillFormat.fromDOM(element.getChild(m_fillFormat.getXMLTag()),version);
}
/**
* 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);
JFPoint startPoint =m_arc.getStartPoint();
JFPoint endPoint =m_arc.getEndPoint();
JFPoint controlPoint =m_arc.getControlPoint();
stream.writeDouble(startPoint.getX());
stream.writeDouble(startPoint.getY());
stream.writeDouble(endPoint.getX());
stream.writeDouble(endPoint.getY());
stream.writeDouble(controlPoint.getX());
stream.writeDouble(controlPoint.getY());
stream.writeInt(m_arc.getArcType());
m_arrow.saveToStream(stream,version);
m_lineFormat.saveToStream(stream,version);
m_fillFormat.saveToStream(stream,version);
}
/**
* 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);
double startPointX =stream.readDouble();
double startPointY =stream.readDouble();
double endPointX =stream.readDouble();
double endPointY =stream.readDouble();
double controlPointX =stream.readDouble();
double controlPointY =stream.readDouble();
int arcType =stream.readInt();
m_arc.setStartPoint(new JFPoint(startPointX,startPointY));
m_arc.setEndPoint(new JFPoint(endPointX,endPointY));
m_arc.setControlPoint(new JFPoint(controlPointX,controlPointY));
m_arc.setArcType(arcType);
m_arrow.loadFromStream(stream,false,version);
m_lineFormat.loadFromStream(stream,false,version);//don't skip head here
m_fillFormat.loadFromStream(stream,false,version);//don't skip head here
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -