📄 abstractarc.java
字号:
/**
* $Id:AbstractArc.java $
*
* Copyright 2004 ~ 2005 JingFei International Cooperation LTD. All rights reserved. *
*/
package com.jfimagine.jfgraph.shape.arc;
import java.util.Iterator;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.GeneralPath;
import java.awt.Color;
import java.awt.BasicStroke;
import com.jfimagine.jfdom.Document;
import com.jfimagine.jfdom.Element;
import com.jfimagine.jfgraph.shape.base.AbstractShape;
import com.jfimagine.jfgraph.shape.base.AbstractObject;
import com.jfimagine.jfgraph.shape.base.ShapeConst;
import com.jfimagine.jfgraph.shape.base.Node;
import com.jfimagine.jfgraph.shape.base.Port;
import com.jfimagine.jfgraph.shape.base.ObjectList;
import com.jfimagine.jfgraph.shape.base.JFVersion;
import com.jfimagine.jfgraph.shape.decorate.LineFormat;
import com.jfimagine.jfgraph.shape.decorate.FillFormat;
import com.jfimagine.jfgraph.shape.decorate.Arrow;
import com.jfimagine.jfgraph.geom.JFPoint;
import com.jfimagine.jfgraph.geom.JFArcPoint;
import com.jfimagine.jfgraph.geom.LineSeg;
import com.jfimagine.jfgraph.geom.Rect;
import com.jfimagine.jfgraph.geom.Arc;
import com.jfimagine.jfgraph.geom.GeomConst;
import com.jfimagine.jfgraph.geom.Angle;
/**
* AbstractArc class.
* An arc is part or whole of the curve of a circle.
*
* @author CookieMaker
*
* @version $Revision: 1.00 $
*/
public abstract class AbstractArc extends AbstractShape{
/** A XML string tag represents the startPoint x coordinate offset of this arc */
public static final String XML_STARTPOINTX ="startPointX";
/** A XML string tag represents the startPoint y coordinate offset of this arc */
public static final String XML_STARTPOINTY ="startPointY";
/** A XML string tag represents the endPoint x coordinate offset of this arc */
public static final String XML_ENDPOINTX ="endPointX";
/** A XML string tag represents the endPoint y coordinate offset of this arc */
public static final String XML_ENDPOINTY ="endPointY";
/** A XML string tag represents the controlPoint x coordinate offset of this arc */
public static final String XML_CONTROLPOINTX ="controlPointX";
/** A XML string tag represents the controlPoint y coordinate offset of this arc */
public static final String XML_CONTROLPOINTY ="controlPointY";
/** A XML string tag represents the type of this arc */
public static final String XML_ARCTYPE ="arcType";
/**
* arc object for an AbstractArc
*/
protected Arc m_arc =new Arc();
/**
* arrow format of current line.
*/
protected Arrow m_arrow =new Arrow();
/**
* fill format of current fill.
*/
protected FillFormat m_fillFormat = new FillFormat();
/**
* line format of current line.
*/
protected 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;
/**
* Because of Graphics.drawArc cannot be applied a double angle, so we use an Arc2D object for
* drawing double angle arcs.
*/
protected java.awt.geom.Arc2D.Double doubleArc=new java.awt.geom.Arc2D.Double();
/**
* Constructor for AbstractArc
*/
public AbstractArc(){
initNodes();
}
/**
* Get start point of this arc.
*
* @return the start point.
*
*/
public JFPoint getStartPoint(){
return m_arc.getStartPoint();
}
/**
* Get end point of this arc.
*
* @return the end point.
*
*/
public JFPoint getEndPoint(){
return m_arc.getEndPoint();
}
/**
* Get control point of this arc.
*
* @return the control point.
*
*/
public JFPoint getControlPoint(){
return m_arc.getControlPoint();
}
/**
* Get center point of this arc.
*
* @return the center point.
*
*/
public JFPoint getCenterPoint(){
return m_arc.getCenter();
}
/**
* Get the angle of this arc.
*
* @return the angle.
*
*/
public double getAngle(){
return m_arc.getAngle();
}
/**
* get radius of current arc.
*
* @return the radius.
*
*/
public double getRadius(){
return m_arc.getRadius();
}
/**
* Get arc type of this arc. OPEN,CHORD or PIE.
*
* @return The arc type.
*
*/
public int getArcType(){
return m_arc.getArcType();
}
/**
* Get the bounds of this arc.
*
* @return The bounds rectangle of current arc.
*
*/
public Rect getBounds(){
return m_arc.getBounds();
}
/**
* Set the value of current arc.
*
* @param arc A new AbstractArc object.
*
*/
public void setArc(AbstractArc arc){
m_arc.setValue(arc.m_arc);
initNodes();
}
/**
* Set the value of current arc.
*
* @param arc A new arc.
*
*/
public void setArc(Arc arc){
m_arc.setValue(arc);
initNodes();
}
/**
* Set value of current Arc.
* Attention: The distance from startPoint to center must equal to the distance from endPoint to center.
* Otherwise we will make a correction for the difference.
*
* @param center The center point of this arc.
*
* @param startPoint The start point of this arc.
*
* @param angle The angle of this arc.
*
*/
public void setArc(JFPoint center,JFPoint startPoint,double angle){
m_arc.setValue(center,startPoint,angle,getArcType());
initNodes();
}
/**
* Set value of current Arc.
* We decide an arc by a startpoint and an endpoint. This method is always used while
* drawing an arc when two end points are decided.
*
* @param startPoint The start point of this arc.
*
* @param endPoint The end point of this arc.
*
*/
public void setArc(JFPoint startPoint, JFPoint endPoint){
m_arc.setValueByDraw(startPoint,endPoint,getArcType());
initNodes();
}
/**
* 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 fill format of current line.
*
* @return The fill format.
*
*/
public FillFormat getFillFormat(){
return m_fillFormat;
}
/**
* Set the fill format of current line.
*
* @param fillFormat A new fill format.
*
*/
public void setFillFormat(FillFormat fillFormat){
m_fillFormat.setValue(fillFormat);
}
/**
* Get the line format of current arc.
*
* @return The line format.
*
*/
public LineFormat getLineFormat(){
return m_lineFormat;
}
/**
* Set the line format of current arc.
*
* @param lineFormat A new line format.
*
*/
public void setLineFormat(LineFormat lineFormat){
m_lineFormat =lineFormat;
}
/**
* Init all nodes of this arc.
* We will always consider that an arc object has three nodes: startpoint,endpoint and controlpoint.
* node1 as start point,
* node2 as end point,
* node3 as control point
*/
protected void initNodes(){
//add three nodes if not assigned yet.
if (m_nodeList.size()<3){
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){}
}
//set value of each node by m_arc.
Node node;
try {
node =(Node)m_nodeList.getByIndex(0);
node.setNodePoint(m_arc.getStartPoint());
node.setParent(this);
}catch(Exception e){
}
try{
node =(Node)m_nodeList.getByIndex(1);
node.setNodePoint(m_arc.getEndPoint());
node.setParent(this);
}catch(Exception e){
}
try{
node =(Node)m_nodeList.getByIndex(2);
node.setNodePoint(m_arc.getControlPoint());
node.setParent(this);
}catch(Exception e){
}
m_nodeList.setZoomScale(getZoomScale());
}
/**
* Init all ports of this shape.
* An object will always has its one or more stable ports and some customized ports,
*/
protected void initPorts(){
//sub classes will fulfill the initPorts details.
}
/**
* 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 rectangular 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);
setArc(pnt1,pnt2);
initNodes();
}
}
/**
* Remove last node added by addNode method.
* @return Last node that remained.
*
*/
public Node removeLastNode(){
//for arc shape, removeLastNode is same as remove all nodes(last node is actually the first one).
m_nodeAdded=0;
return null;
}
/**
* If an arc has been drew.
* @return True when complete, false otherwise.
*
*/
public boolean ifCompleteDrawing(){
//two nodes added will decide an arc.
return (m_nodeAdded==2);
}
/**
* Finish drawing object.
*
*/
public boolean finishDrawing(){
initNodes();
initPorts();
initLabel();
return true;
}
/**
* Draw current object's arrows.
*/
protected void drawArrow(Graphics g){
}
/**
* Get a general path for drawing this arc.
* @return the path.
*/
protected GeneralPath getArcPath(){
double zoom =getZoomScale();
Rect rect =m_arc.getBounds();
double startAngle =m_arc.getStartAngle() / Angle.PI * 180;
double arcAngle =m_arc.getAngle() / Angle.PI * 180;
//we used a Arc2D.double objects here for drawing double angle arcs(note: Graphics.drawArc cannot process double angles).
doubleArc.setArc(rect.getX()* zoom,rect.getY() * zoom,rect.getWidth() * zoom,rect.getHeight() * zoom,startAngle,arcAngle,java.awt.geom.Arc2D.OPEN);
return new GeneralPath(doubleArc);
}
/**
* 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){
}
/**
* Draw current object on graphic canvas.
*
* @param g A graphic canvas.
* @param isXorMode If is in xor mode now.
*
*/
public void draw(Graphics g,boolean isXorMode){
if (g==null)
return;
//if user hide this shape, we'll draw an 'invisible' bounds here.
if (isInvisible()){
drawInvisibleBounds(g,isXorMode);
return;
}
if (!isXorMode)
setTransparencyComposite(g);
if (!isXorMode){
m_lineFormat.setGraphics(g);
}else{
Graphics2D g2=(Graphics2D)g;
g2.setStroke(new BasicStroke(1));
g2.setColor(Color.black);
}
drawArc(g,isXorMode);
//set the default graphics stroke and color.
if (!isXorMode)
m_lineFormat.initGraphics(g);
if (!isXorMode)
restoreTransparencyComposite(g);
if (!isXorMode){
drawPort(g);
drawArrow(g);
drawLabel(g);
}
}
/**
* Test if current object intersects with a specified point.
*
* @param pnt A JFPoint used to test intersection.
*
*/
public boolean intersects(JFPoint pnt){
if (isInvisible())
return getBounds().contains(pnt);
else
return m_arc.intersects(pnt.getX(),pnt.getY(),GeomConst.PICK_OFFSET) || m_arc.contains(pnt);
}
/**
* Test if current object intersects with a specified rectangle.
*
* @param rect A Rect used to test intersection.
*
*/
public boolean intersects(Rect rect){
if (isInvisible())
return getBounds().intersects(rect);
else
return m_arc.intersects(rect);
}
/**
* Draw a moving node of current shape and it's relation shape lines.
*
* @param g A graphic canvas.
*
* @param node A moving node of current object.
*
*/
private void drawMovingNode(Graphics g, Node node){
Node node1=null;
//start point
try {
node1 =(Node)m_nodeList.getByIndex(0);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -