abstractrectangle.java

来自「用Java开发的、实现类似Visio功能的应用程序源码」· Java 代码 · 共 1,128 行 · 第 1/3 页

JAVA
1,128
字号
/**
 *    $Id:AbstrractRectangle.java $
 *
 *    Copyright 2004 ~ 2005  JingFei International Cooperation LTD. All rights reserved. *
 */
package com.jfimagine.jfgraph.shape.rectangle;


import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.GeneralPath;
import java.awt.BasicStroke;

import java.util.Iterator;

import com.jfimagine.jfdom.Document;
import com.jfimagine.jfdom.Element;

import com.jfimagine.jfgraph.shape.decorate.FontFormat;
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.shape.base.AbstractShape;
import com.jfimagine.jfgraph.shape.base.Port;
import com.jfimagine.jfgraph.shape.base.Node;
import com.jfimagine.jfgraph.shape.base.JFVersion;
import com.jfimagine.jfgraph.shape.base.ShapeConst;

import com.jfimagine.jfgraph.geom.JFPoint;
import com.jfimagine.jfgraph.geom.LineSeg;
import com.jfimagine.jfgraph.geom.Rect;
import com.jfimagine.jfgraph.geom.GeomConst;
import com.jfimagine.jfgraph.geom.JFVector;
import com.jfimagine.jfgraph.geom.Angle;

 /**
 * Abstract Rectangle class. It's a class as base of Arc, rectangle,ellipse
 *
 * @author     CookieMaker    
 *
 * @version $Revision: 1.00 $
 */  
public abstract class AbstractRectangle extends AbstractShape{

   /**  A XML string tag represents the left-top x coordinate offset of this rectangle  */
   public  static final String   XML_LEFTTOPX		="leftTopX";
   /**  A XML string tag represents the left-top y coordinate offset of this rectangle  */
   public  static final String   XML_LEFTTOPY		="leftTopY";
   /**  A XML string tag represents the right-top x coordinate offset of this rectangle  */
   public  static final String   XML_RIGHTTOPX		="rightTopX";
   /**  A XML string tag represents the right-top y coordinate offset of this rectangle  */
   public  static final String   XML_RIGHTTOPY		="rightTopY";
   /**  A XML string tag represents the left-bottom x coordinate offset of this rectangle  */
   public  static final String   XML_LEFTBOTTOMX	="leftBottomX";
   /**  A XML string tag represents the left-bottom y coordinate offset of this rectangle  */
   public  static final String   XML_LEFTBOTTOMY	="leftBottomY";
   /**  A XML string tag represents the right-bottom x coordinate offset of this rectangle  */
   public  static final String   XML_RIGHTBOTTOMX	="rightBottomX";
   /**  A XML string tag represents the right-bottom y coordinate offset of this rectangle  */
   public  static final String   XML_RIGHTBOTTOMY	="rightBottomY";

  
   
   /**
    *   line format of current line.
    */ 
   protected LineFormat  m_lineFormat = new LineFormat();

   /**
    *   fill format of current fill.
    */ 
   protected FillFormat  m_fillFormat = new FillFormat();

   /**
    *   An rectangle object for the outline of this rectangle.
    */
   protected	Rect	m_rect	=new Rect();

   /**
    *   line segment object for rectangle calculations.
    */
   protected LineSeg m_lineSeg		=new LineSeg();


   /**
    *   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;


   /**
    *   Constructor.
    *
    */ 	
   public AbstractRectangle(){
   	initNodes();  
   }

   /**
    *   Get the x offset of current rectangle. 
    *   The x offset is the left-x coordinate position of this rectangle.
    *
    *   @return  The x offset of current rectangle.
    *
    */ 	
   public double getX(){
   	return m_rect.getX();
   }

   /**
    *   Get the y offset of current rectangle. 
    *   The y offset is the top-y coordinate position of this rectangle.
    *
    *   @return  The y offset of current rectangle.
    *
    */ 	
   public double getY(){
   	return  m_rect.getY();
   }

   /**
    *   Get the width of current rectangle. 
    *
    *   @return  The width of current rectangle 'box'.
    *
    */ 	
   public double getWidth(){
   	return m_rect.getWidth();
   }

   /**
    *   Get the height of current rectangle. 
    *
    *   @return  The height of current rectangle 'box'.
    *
    */ 	
   public double getHeight(){
   	return m_rect.getHeight();
   }

   /**
    *   Set the value of this rectangle.
    *
    *   @param x  X coordiate.
    *
    *   @param y  Y coordiate.
    *
    *   @param w  Width of this rectangle.
    *
    *   @param h  Height of this rectangle.
    *
    */ 	
   public void setRect(double x, double y, double w, double h){
   	m_rect.setValue(x,y,w,h);
   	initNodes();  
   	finishDrawing();
   }

   /**
    *   Set the value of current rectangle.
    *
    *   @param rect A new rectangle.
    *
    */ 	
   public void setRect(Rect rect){
   	m_rect.setValue(rect);
   	initNodes();
   	finishDrawing();
   }

   /**
    *   Get an original rectangle if this rectangle.
    *   An original rectangle will has no rotation,mirror or flip.
    *
    *   @return The original rectangle of current rectangle.
    *
    */ 	
   public Rect getOriginalRect(){
	JFPoint center		=m_rect.getCenter();
	JFPoint leftTop		=m_rect.getVertex(Rect.VERTEXTYPE_LEFTTOP);
	JFPoint rightTop	=m_rect.getVertex(Rect.VERTEXTYPE_RIGHTTOP);
	JFPoint leftBottom	=m_rect.getVertex(Rect.VERTEXTYPE_LEFTBOTTOM);
	
	double w	=leftTop.distance(rightTop);
	double h	=leftTop.distance(leftBottom);
	double x	=center.getX() - w/2;
	double y	=center.getY() - h/2;
	
	Rect rect	=new Rect();
	rect.setValue(x,y,w,h);
	return rect;
   }

   /**
    *   Get the rotate angle of current rectangle. 
    *   This will be used specially in JFText or JFImage.
    * 
    *   @return The rotate angle.
    *
    */ 	
   public double getRotateAngle(){
	JFPoint leftTop		=m_rect.getVertex(Rect.VERTEXTYPE_LEFTTOP);
	JFPoint rightTop	=m_rect.getVertex(Rect.VERTEXTYPE_RIGHTTOP);
	JFPoint leftBottom	=m_rect.getVertex(Rect.VERTEXTYPE_LEFTBOTTOM);


	boolean clockwise	=JFVector.underClockwiseSide(rightTop,leftTop,leftBottom,leftTop);
	if (clockwise)
		return Angle.getAngle(rightTop,leftTop,false);
	else	
		return Angle.getAngle(leftTop,rightTop,false);
   	
   }

   	
   /**
    *   Get the bounds of this rectangle.
    *
    *   @return The bounds rectangle of current rectangle.
    *
    */ 	
   public Rect getBounds(){
   	return m_rect.getBounds();
   }

   /**
    *   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 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	=lineFormat;
   }


   /**
    *   Init all nodes of this rectangle. 
    *   We will always consider that a rectangle object has four nodes.
    *   node1 as left-top vertex,
    *   node2 as right-top vertex,
    *   node3 as left-bottom vertex,
    *   node4 as right-bottom vertex.
    */ 	
   protected void initNodes(){
   	//add four nodes if not assigned yet.
   	if (m_nodeList.size()<4){
   		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){}
   		try{ m_nodeList.add(new Node()); 	}catch(Exception e){}
   	}
   	
       //set value of each node by m_rect.	
       Node node;

       	try {
       		node	=(Node)m_nodeList.getByIndex(0); 
       		node.setNodePoint(m_rect.getVertex(Rect.VERTEXTYPE_LEFTTOP));
       		node.setParent(this);
       	}catch(Exception e){
       	}
       
       	try{
       		node	=(Node)m_nodeList.getByIndex(1);
       		node.setNodePoint(m_rect.getVertex(Rect.VERTEXTYPE_RIGHTTOP));
       		node.setParent(this);
       	}catch(Exception e){
       	}

       	try{
       		node	=(Node)m_nodeList.getByIndex(2);
       		node.setNodePoint(m_rect.getVertex(Rect.VERTEXTYPE_LEFTBOTTOM));
       		node.setParent(this);
       	}catch(Exception e){
       	}
       	

       try{
       		node	=(Node)m_nodeList.getByIndex(3);
       		node.setNodePoint(m_rect.getVertex(Rect.VERTEXTYPE_RIGHTBOTTOM));
       		node.setParent(this);
       	}catch(Exception e){
       	}
	
	m_nodeList.setZoomScale(getZoomScale());
      	
   }

   /**
    *   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 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);
  			
			//top middle
			m_portList.addPort(new Port(this,Port.PORTTYPE_DEFAULT,leftTop,rightTop,top));
   			
   			//right middle
			m_portList.addPort(new Port(this,Port.PORTTYPE_DEFAULT,rightTop,rightBottom,right));

			//bottom middle
			m_portList.addPort(new Port(this,Port.PORTTYPE_DEFAULT,rightBottom,leftBottom,bottom));

			//left middle
			m_portList.addPort(new Port(this,Port.PORTTYPE_DEFAULT,leftBottom,leftTop,left));
   			
   		}catch(Exception e){
   		}
   	}
	
	m_portList.setZoomScale(getZoomScale());
   }



   /**

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?