jfline.java
来自「用Java开发的、实现类似Visio功能的应用程序源码」· Java 代码 · 共 1,258 行 · 第 1/3 页
JAVA
1,258 行
/**
* $Id:JFLine.java $
*
* Copyright 2004 ~ 2005 JingFei International Cooperation LTD. All rights reserved. *
*/
package com.jfimagine.jfgraph.shape.line;
import java.util.List;
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.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.NodeList;
import com.jfimagine.jfgraph.shape.base.Port;
import com.jfimagine.jfgraph.shape.base.PortList;
import com.jfimagine.jfgraph.shape.decorate.LineFormat;
import com.jfimagine.jfgraph.shape.decorate.Arrow;
import com.jfimagine.jfgraph.geom.JFPoint;
import com.jfimagine.jfgraph.geom.JFPointNode;
import com.jfimagine.jfgraph.geom.LineSeg;
import com.jfimagine.jfgraph.geom.PolyLine;
import com.jfimagine.jfgraph.geom.Rect;
import com.jfimagine.jfgraph.geom.GeomConst;
import com.jfimagine.jfgraph.shape.line.PolyNode;
import com.jfimagine.utils.log.*;
/**
* JFLine class.
* A line should be a two points one(one for beginning and the other for end), or n-points polyline.
*
* @author CookieMaker
*
* @version $Revision: 1.00 $
*/
public class JFLine extends AbstractShape{
/**an internal log utility*/
private JFLogger m_logger=JFLogManager.getLogger(this.getClass());
/**
* A XML string tag represents a Line
*/
public static final String XML_LINE ="Line";
/**
* arrow format of current line.
*/
protected Arrow m_arrow =new Arrow();
/**
* line format of current line.
*/
protected LineFormat m_lineFormat = new LineFormat();
/**
* Internal PolyLine object.
*/
protected PolyLine m_polyLine;
/**
* Constructor for Line
*/
public JFLine(){
setObjectType(ShapeConst.SHAPETYPE_LINE);
setXMLTag(XML_LINE);
m_polyLine =new PolyLine();
}
/**
* Constructor for Line
*
* @param xpoints an array of x coordinates
* @param ypoints an array of y coordinates
* @param npoints the total number of points in the Polygon
*
*/
public JFLine(int[] xpoints, int[] ypoints, int npoints){
setObjectType(ShapeConst.SHAPETYPE_LINE);
setXMLTag(XML_LINE);
m_polyLine =new PolyLine(xpoints,ypoints,npoints);
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.setValue(lineFormat);
}
/**
* Get the bounds of this rectangle.
*
* @return The bounds rectangle of current rectangle.
*
*/
public Rect getBounds(){
return m_polyLine.getBounds();
}
/**
* 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 poly line.
*/
private void initNodes(){
int size =m_polyLine.getNodeCount();
while (m_nodeList.size()<size){
try{ m_nodeList.add(new PolyNode()); }catch(Exception e){break;}
}
while (m_nodeList.size()>size){
try{ m_nodeList.removeByIndex(0); }catch(Exception e){ break; }
}
JFPointNode srcNode;
PolyNode destNode;
for (int i=0; i<size; i++){
try{
srcNode =m_polyLine.getNode(i);
destNode =(PolyNode)m_nodeList.getByIndex(i);
destNode.setXOffset(srcNode.getX());
destNode.setYOffset(srcNode.getY());
destNode.setNodeType(srcNode.getNodeType());
destNode.setParent(this);
}catch(Exception e){
break;
}
}
m_nodeList.setZoomScale(getZoomScale());
}
/**
* Set a node list of this shape.
*
* @param nodeList A new node list.
*
*/
public void setNodeList(ObjectList nodeList){
if (nodeList==null)
return;
try{
m_nodeList =(NodeList)nodeList.clone();
//re-set parent.
Iterator it =m_nodeList.getList().iterator();
while (it!=null && it.hasNext()){
PolyNode node =(PolyNode)it.next();
node.setParent(this);
}
recallNodes();
}catch(Exception e){
}
}
/**
* Recall point nodes of polyLine from m_nodesList.
* when restore nodes from xml documents or binary files, we will
* call this method for recovering point nodes.
*/
private void recallNodes(){
int size =m_nodeList.size();
m_polyLine.clearNodes();
List pointNodeList=m_polyLine.getNodeList();
PolyNode srcNode;
JFPointNode destNode;
for (int i=0; i<size; i++){
try{
srcNode =(PolyNode)m_nodeList.getByIndex(i);
destNode =new JFPointNode(srcNode.getXOffset(),srcNode.getYOffset(),srcNode.getNodeType());
pointNodeList.add(destNode);
}catch(Exception e){
break;
}
}
}
/**
* 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){
m_polyLine.addNode(x,y);
initNodes();
}
/**
* Remove last node added by addNode method.
* @return Last node that remained.
*
*/
public Node removeLastNode(){
m_polyLine.cancelLastNode();
initNodes();
if (m_nodeList.size()==0)
return null;
else{
try{
return (Node)m_nodeList.getByIndex(m_nodeList.size()-1);
}catch(Exception e){
return null;
}
}
}
/**
* If a polyline has been drew.
* @return True when complete, false otherwise.
*
*/
public boolean ifCompleteDrawing(){
//two nodes added will decide a polyline.
return (m_polyLine.getNodeCount()>=2);
}
/**
* 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();
int nodeCount =m_polyLine.getNodeCount();
JFPoint startPoint1 =m_polyLine.getNode(0);
JFPoint startPoint2 =m_polyLine.getNextNode(0,false);
JFPoint endPoint1 =m_polyLine.getNode(nodeCount-1);
JFPoint endPoint2 =m_polyLine.getNextNode(nodeCount-1,true);
if (zoom!=1.0){
startPoint1 =new JFPoint(startPoint1.getX() * zoom, startPoint1.getY() * zoom);
startPoint2 =new JFPoint(startPoint2.getX() * zoom, startPoint2.getY() * zoom);
endPoint1 =new JFPoint(endPoint1.getX() * zoom, endPoint1.getY() * zoom);
endPoint2 =new JFPoint(endPoint2.getX() * zoom, endPoint2.getY() * zoom);
}
Arrow.drawArrow(startArrow,lineWidth,startPoint2,startPoint1,g,c);
Arrow.drawArrow(endArrow,lineWidth,endPoint2,endPoint1,g,c);
}
/**
* 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);
JFPointNode node;
double zoom =getZoomScale();
//pick up each nodes pair, and draw each line segment between a nodes pair.
GeneralPath line= new GeneralPath(GeneralPath.WIND_EVEN_ODD);
int cnt=0;
for (int i=0; i<m_polyLine.getNodeCount(); i++){
node =m_polyLine.getNode(i);
if (node.getNodeType()==JFPointNode.NODETYPE_END){
float x =(float)(node.getX() * zoom);
float y =(float)(node.getY() * zoom);
if (cnt==0){
line.moveTo(x,y);
}else{
line.lineTo(x,y);
}
cnt++;
}
}
Graphics2D g2=(Graphics2D)g;
if (!isXorMode){
//m_lineFormat.setGraphics(g);
//g2.draw(line);
m_lineFormat.draw(g,line);
}else{
g2.setStroke(new BasicStroke(1));
g2.setColor(Color.black);
g2.draw(line);
}
//set the default graphics stroke and color.
if (!isXorMode)
m_lineFormat.initGraphics(g);
if (!isXorMode)
restoreTransparencyComposite(g);
if (!isXorMode){
//draw arrows
drawArrow(g);
//draw ports.
drawPort(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_polyLine.contains(pnt,GeomConst.PICK_OFFSET);
}
/**
* Test if current object intersects with a specified rectangle.
*
* @param rect A Rect used to test intersection.
*
*/
public boolean intersects(Rect rect){
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?