jfellipse.java
来自「用Java开发的、实现类似Visio功能的应用程序源码」· Java 代码 · 共 380 行
JAVA
380 行
/**
* $Id:JFEllipse.java $
*
* Copyright 2004 ~ 2005 JingFei International Cooperation LTD. All rights reserved. *
*/
package com.jfimagine.jfgraph.shape.rectangle;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.BasicStroke;
import java.awt.geom.GeneralPath;
import java.util.Iterator;
import com.jfimagine.jfgraph.shape.rectangle.AbstractRectangle;
import com.jfimagine.jfgraph.shape.rectangle.EllipsePort;
import com.jfimagine.jfgraph.shape.base.Node;
import com.jfimagine.jfgraph.shape.base.Port;
import com.jfimagine.jfgraph.shape.base.AbstractObject;
import com.jfimagine.jfgraph.shape.base.ShapeConst;
import com.jfimagine.jfgraph.geom.JFPoint;
import com.jfimagine.jfgraph.geom.JFEllipsePoint;
import com.jfimagine.jfgraph.geom.LineSeg;
import com.jfimagine.jfgraph.geom.Rect;
import com.jfimagine.jfgraph.geom.Ellipse;
import com.jfimagine.jfgraph.geom.Arc;
import com.jfimagine.jfgraph.geom.GeomConst;
import com.jfimagine.utils.log.*;
/**
* JFEllipse class.
* A ellipse class used to represents an ellipse shape.
*
* @author CookieMaker
*
* @version $Revision: 1.00 $
*/
public class JFEllipse extends AbstractRectangle{
/**
* A XML string tag represents a ellipse.
*/
public static final String XML_ELLIPSE ="JFEllipse";
/**an internal log utility*/
private JFLogger m_logger=JFLogManager.getLogger(this.getClass());
/**
* Constructor for ellipse
*/
public JFEllipse(){
setObjectType(ShapeConst.SHAPETYPE_ELLIPSE);
setXMLTag(XML_ELLIPSE);
m_rect =new Ellipse();
}
/**
* Constructor for ellipse.
*
* @param x X coordiate.
*
* @param y Y coordiate.
*
* @param w Width of this rectangle.
*
* @param h Height of this rectangle.
*
*/
public JFEllipse(double x, double y, double w, double h){
setObjectType(ShapeConst.SHAPETYPE_ELLIPSE);
setXMLTag(XML_ELLIPSE);
Ellipse ellipse =new Ellipse();
ellipse.setValue(x,y,w,h);
m_rect =ellipse;
finishDrawing();
}
/**
* 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 JFEllipse();
}
/**
* Init all ports of this rectangle.
* An object will always has its one or more stable ports and some customized ports,
* for rectangle, it will has four stable ports as below,
* port1 at middle top,
* port2 at middle right,
* port3 at middle bottom,
* port4 at middle left,
*/
protected void initPorts(){
if (m_portList.size()==0){
try{
JFPoint center =m_rect.getCenter();
JFPoint leftTop =m_rect.getVertex(Rect.VERTEXTYPE_LEFTTOP);
JFPoint rightTop =m_rect.getVertex(Rect.VERTEXTYPE_RIGHTTOP);
JFPoint rightBottom =m_rect.getVertex(Rect.VERTEXTYPE_RIGHTBOTTOM);
JFPoint leftBottom =m_rect.getVertex(Rect.VERTEXTYPE_LEFTBOTTOM);
JFPoint top =leftTop.midPoint(rightTop);
JFPoint right =rightTop.midPoint(rightBottom);
JFPoint bottom =rightBottom.midPoint(leftBottom);
JFPoint left =leftTop.midPoint(leftBottom);
EllipsePort port;
//top middle
port =new EllipsePort(this,Port.PORTTYPE_DEFAULT,center,top,top,Rect.SIDETYPE_TOP);
m_portList.add(port);
//right middle
//port =(EllipsePort)addPort(right.getX(),right.getY());
//port.setPortType(Port.PORTTYPE_DEFAULT);
port =new EllipsePort(this,Port.PORTTYPE_DEFAULT,center,right,right,Rect.SIDETYPE_RIGHT);
m_portList.add(port);
//bottom middle
port =new EllipsePort(this,Port.PORTTYPE_DEFAULT,center,bottom,bottom,Rect.SIDETYPE_BOTTOM);
m_portList.add(port);
//left middle
port =new EllipsePort(this,Port.PORTTYPE_DEFAULT,center,left,left,Rect.SIDETYPE_LEFT);
m_portList.add(port);
}catch(Exception e){
}
}
m_portList.setZoomScale(getZoomScale());
}
/**
* 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){
super.addNode(x,y);
if (m_nodeAdded>1){
//left-top point and right-bottom point:
JFPoint pnt1 =m_firstNode;
//JFPoint pnt2 =new JFPoint(x,y);
m_rect.setVertex(Rect.VERTEXTYPE_LEFTTOP,pnt1.getX(),pnt1.getY());
m_rect.setVertex(Rect.VERTEXTYPE_RIGHTTOP,x,pnt1.getY());
m_rect.setVertex(Rect.VERTEXTYPE_LEFTBOTTOM,pnt1.getX(),y);
m_rect.setVertex(Rect.VERTEXTYPE_RIGHTBOTTOM,x,y);
initNodes();
}
}
/**
* 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){
super.moveNode(node,x,y,g);
if (node!=null){
if (node.getXOffset()!=x || node.getYOffset()!=y){
//move node and draw a new dragging shape.
int vertexType =getVertexTypeByNode(node);
m_rect.moveVertex(vertexType,x,y,Rect.VERTEXMOVETYPE_RECTANGLE);
//init shape nodes.
initNodes();
//draw current moving node and its relational lines
draw(g,true);
}
}
}
/**
* 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);
double zoom =getZoomScale();
//draw each arc of this ellipse.
Ellipse ellipse =(Ellipse)m_rect;
if (zoom!=1.0){
//zoom ellipse firstly.
ellipse =new Ellipse(ellipse);
JFPoint vertex;
vertex =ellipse.getVertex(Rect.VERTEXTYPE_LEFTTOP);
vertex.setValue(vertex.getX() * zoom, vertex.getY() * zoom);
vertex =ellipse.getVertex(Rect.VERTEXTYPE_RIGHTTOP);
vertex.setValue(vertex.getX() * zoom, vertex.getY() * zoom);
vertex =ellipse.getVertex(Rect.VERTEXTYPE_LEFTBOTTOM);
vertex.setValue(vertex.getX() * zoom, vertex.getY() * zoom);
vertex =ellipse.getVertex(Rect.VERTEXTYPE_RIGHTBOTTOM);
vertex.setValue(vertex.getX() * zoom, vertex.getY() * zoom);
}
GeneralPath path =Ellipse.getEllipsePath(ellipse);
if (path!=null){
if (!isXorMode){
Rect rect =getBounds();
rect.setValue(rect.getX() * zoom, rect.getY() * zoom, rect.getWidth() * zoom, rect.getHeight() * zoom);
m_fillFormat.draw(g,path,rect);
m_lineFormat.draw(g,path);
}else{
Graphics2D g2=(Graphics2D)g;
g2.setStroke(new BasicStroke(1));
g2.setColor(Color.black);
g2.draw(path);
}
}
if (!isXorMode)
restoreTransparencyComposite(g);
if (!isXorMode){
drawPort(g);
drawLabel(g);
}
}
/**
* Add a new port to this shape.
* @param x,y Current picking position.
*
*/
public Port addPort(double x,double y){
Ellipse ellipse =(Ellipse)m_rect;
JFEllipsePoint portPoint =ellipse.intersectsAt(x,y,GeomConst.PICK_OFFSET);
if (portPoint==null)
return null;
JFPoint center =ellipse.getCenter();
EllipsePort port =new EllipsePort(this,Port.PORTTYPE_CUSTOM,
center,
portPoint.getTargetPoint(),
portPoint,
portPoint.getTargetSideType());
return m_portList.addPort(port);
}
/**
* Start move a node.
*
* @param node The node will be moved.
*
*/
public void startMoveNode(Node node){
m_originalRect =new Ellipse((Ellipse)m_rect);
startMoveLabel();
}
/**
* finish move all ports of this curve.
*/
public void finishMovePort(){
finishMoveLabel();
Ellipse originalEllipse =(Ellipse)m_originalRect;
Ellipse ellipse =(Ellipse)m_rect;
JFPoint center =((Ellipse)m_rect).getCenter();
Iterator it =m_portList.getList().iterator();
while (it!=null && it.hasNext()){
EllipsePort port =(EllipsePort)it.next();
JFPoint oldTargetPoint =port.getSecondPoint();
LineSeg oldSide =originalEllipse.getSide(port.getTargetSideType());
LineSeg newSide =ellipse.getSide(port.getTargetSideType());
double oldDist =oldSide.getPoint1().distance(oldTargetPoint);
double oldDist1 =oldSide.getLength();
if (oldDist1<1) oldDist1 =1;
double scale =oldDist/oldDist1;
JFPoint portPoint;
JFPoint newTargetPoint;
if (scale==0.5){
portPoint =newSide.getPoint1().midPoint(newSide.getPoint2());
newTargetPoint =portPoint;
}else{
double newDist1 =newSide.getLength();
double newDist =newDist1 * scale;
newTargetPoint =newSide.getPoint1().nearPoint(newSide.getPoint2(),newDist);
if (newTargetPoint==null){
m_logger.error("finishMovePort: New target point is null!");
}
portPoint =ellipse.getPoint(newTargetPoint);
}
port.setFirstPoint(center);
port.setSecondPoint(newTargetPoint);
port.setPortPoint(portPoint);
}
}
/**
* 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){
finishMovePort();
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?