📄 jfarc.java
字号:
/**
* $Id:JFArc.java $
*
* Copyright 2004 ~ 2005 JingFei International Cooperation LTD. All rights reserved. *
*/
package com.jfimagine.jfgraph.shape.arc;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.geom.GeneralPath;
import com.jfimagine.jfgraph.geom.JFPoint;
import com.jfimagine.jfgraph.geom.Arc;
import com.jfimagine.jfgraph.geom.JFArcPoint;
import com.jfimagine.jfgraph.geom.JFVector;
import com.jfimagine.jfgraph.geom.GeomConst;
import com.jfimagine.jfgraph.shape.base.Port;
import com.jfimagine.jfgraph.shape.base.Node;
import com.jfimagine.jfgraph.shape.base.ShapeConst;
import com.jfimagine.jfgraph.shape.base.AbstractObject;
import com.jfimagine.jfgraph.shape.arc.AbstractArc;
import com.jfimagine.jfgraph.shape.decorate.Arrow;
import com.jfimagine.jfgraph.shape.decorate.FillFormat;
/**
* JFArc class.
* An arc is part or whole of the curve of a circle.
*
* @author CookieMaker
*
* @version $Revision: 1.00 $
*/
public class JFArc extends AbstractArc{
/**
* A XML string tag represents an Arc
*/
public static final String XML_ARC ="Arc";
/**
* initialization for JFArc
*/
private void initArc(){
setObjectType(ShapeConst.SHAPETYPE_ARC);
setXMLTag(XML_ARC);
m_arc.setArcType(Arc.OPEN);
}
/**
* Constructor for JFArc
*/
public JFArc(){
initArc();
}
/**
* Constructor for JFArc
*/
public JFArc(AbstractArc arc){
setArc(arc);
initArc();
}
/**
* Constructor for JFArc
* @param x1,y1 start point of this arc
* @param x2,y2 end point of this arc
*/
public JFArc(double x1,double y1, double x2, double y2){
initArc();
addNode(x1,y1);
addNode(x2,y2);
finishDrawing();
}
/**
* Constructor for JFArc
* @param centerx,centery center point of this arc
* @param x1,y1 start point of this arc
* @param angle Angle of this arc.
*/
public JFArc(double centerx, double centery,double x1,double y1, double angle){
initArc();
m_arc.setValue(new JFPoint(centerx,centery),new JFPoint(x1,y1),angle);
finishDrawing();
}
/**
* Get the fill format of current object.
*
* @return The fill format.
*
*/
public FillFormat getFillFormat(){return null;}
/**
* Set the fill format of current object.
*
* @param fillFormat A new fill format.
*
*/
public void setFillFormat(FillFormat fillFormat){}
/**
* 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 JFArc();
}
/**
* 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 ports of this shape.
* An object will always has its one or more stable ports and some customized ports,
*/
protected void initPorts(){
if (m_portList.size()==0){
try{
JFPoint startPoint =m_arc.getStartPoint();
JFPoint endPoint =m_arc.getEndPoint();
JFPoint controlPoint =m_arc.getControlPoint();
//all the ports of arc are on the arc curve.
m_portList.addPort(new ArcPort(this,Port.PORTTYPE_DEFAULT,startPoint,0,JFArcPoint.POINTTYPE_CURVE));
m_portList.addPort(new ArcPort(this,Port.PORTTYPE_DEFAULT,endPoint,1,JFArcPoint.POINTTYPE_CURVE));
m_portList.addPort(new ArcPort(this,Port.PORTTYPE_DEFAULT,controlPoint,0.5,JFArcPoint.POINTTYPE_CURVE));
}catch(Exception e){
}
}
m_portList.setZoomScale(getZoomScale());
}
/**
* 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(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;
}
/**
* Draw current object's arrows.
*/
protected void drawArrow(Graphics g){
double zoom =getZoomScale();
int lineWidth =m_lineFormat.getLineWidth();
Color c =m_lineFormat.getLineColor();
int startArrow =m_arrow.getStartArrow();
int endArrow =m_arrow.getEndArrow();
JFPoint startPoint =m_arc.getStartPoint();
JFPoint endPoint =m_arc.getEndPoint();
JFPoint controlPoint =m_arc.getControlPoint();
JFPoint center =m_arc.getCenter();
//zoom all these four points, then re-build them with new instance.
startPoint =new JFPoint(startPoint.getX() * zoom, startPoint.getY() * zoom);
endPoint =new JFPoint(endPoint.getX() * zoom, endPoint.getY() * zoom);
controlPoint =new JFPoint(controlPoint.getX() * zoom, controlPoint.getY() * zoom);
center =new JFPoint(center.getX() * zoom, center.getY() * zoom);
//if control point is under clockwise side of vector from startpoint to endpoint.
boolean clockwise =JFVector.underClockwiseSide(startPoint,controlPoint,startPoint,endPoint);
//calculate the slope of the triangle line accross start point
double slope1 =startPoint.getSlope(center);
if (slope1==0)
slope1 =GeomConst.LARGE_VALUE;
else if (slope1==GeomConst.LARGE_VALUE)
slope1 =0;
else
slope1 =-1/slope1;
//a point on the triangle line1
JFPoint trianglePoint1 =startPoint.nearPoint(slope1,startPoint.distance(controlPoint),controlPoint,clockwise);
//a little movment to statisfy a more correct arrow's start point:
trianglePoint1 =trianglePoint1.nearPoint(controlPoint,lineWidth*4);
//calculate the slope of the triangle line accross end point
double slope2 =endPoint.getSlope(center);
if (slope2==0)
slope2 =GeomConst.LARGE_VALUE;
else if (slope2==GeomConst.LARGE_VALUE)
slope2 =0;
else
slope2 =-1/slope2;
//a point on the triangle line2
JFPoint trianglePoint2 =endPoint.nearPoint(slope2,endPoint.distance(controlPoint),controlPoint,!clockwise);
//a little movment to statisfy a more correct arrow's start point:
trianglePoint2 =trianglePoint2.nearPoint(controlPoint,lineWidth*4);
Arrow.drawArrow(startArrow,lineWidth,trianglePoint1,startPoint,g,c);
Arrow.drawArrow(endArrow,lineWidth,trianglePoint2,endPoint,g,c);
}
/**
* Draw an arc on canvas, by specified line format.
*
* @param g A graphic canvas.
* @param isXorMode If is in xor mode now.
*
*/
public void drawArc(Graphics g, boolean isXorMode){
GeneralPath path =getArcPath();
if (!isXorMode){
m_lineFormat.draw(g,path);
}else
((Graphics2D)g).draw(path);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -