rect.java

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

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

import com.jfimagine.jfgraph.geom.JFDimension;
import com.jfimagine.jfgraph.geom.JFPoint;
import com.jfimagine.jfgraph.geom.LineSeg;

 
 /**
 * Rect class.  A class used to represent a rectangle in the plane with double coordinates. 
 *   <p> Attention: Here we used a clockwise quadrant system.
 *   And the first quadrant is under right bottom corner.
 *
 * @author     CookieMaker    
 *
 * @version $Revision: 1.00 $
 */  
 public class Rect implements Cloneable{

   /** A const vertext type represents the left-top vertex of a rectangle*/	
   public static final int 	VERTEXTYPE_LEFTTOP	=1;	
   /** A const vertext type represents the right-top vertex of a rectangle*/	
   public static final int 	VERTEXTYPE_RIGHTTOP	=2;	
   /** A const vertext type represents the left-bottom vertex of a rectangle*/	
   public static final int 	VERTEXTYPE_LEFTBOTTOM	=3;	
   /** A const vertext type represents the right-bottom vertex of a rectangle*/	
   public static final int 	VERTEXTYPE_RIGHTBOTTOM	=4;


   /** A const side type represents the left side of a rectangle*/	
   public static final int 	SIDETYPE_LEFT		=1;	
   /** A const side type represents the top side of a rectangle*/	
   public static final int 	SIDETYPE_TOP		=2;	
   /** A const side type represents the right side of a rectangle*/	
   public static final int 	SIDETYPE_RIGHT		=3;	
   /** A const side type represents the bottom side of a rectangle*/	
   public static final int 	SIDETYPE_BOTTOM		=4;	

   /** A rectangular scale vertex moving type of rectangle.*/	
   public static final int 	VERTEXMOVETYPE_SCALE		=1;	
   /** A rectangular vertex moving type of rectangle.*/	
   public static final int 	VERTEXMOVETYPE_RECTANGLE	=2;	
   /** A parallelogram vertex moving type of rectangle.*/	
   public static final int 	VERTEXMOVETYPE_PARALLEL		=3;	
   /** A trapezoid vertex moving type of rectangle.*/	
   public static final int 	VERTEXMOVETYPE_TRAPEZOID	=4;	
   /** A isosceles trapezoid vertex moving type of rectangle.*/	
   public static final int 	VERTEXMOVETYPE_ISOSCELESTRAPEZOID	=5;	

   /**
    *   Left-top vertex of rectangle.
    */
   protected   JFPoint	m_leftTop	=new JFPoint();
    
   /**
    *   Right-top vertex of rectangle.
    */
   protected   JFPoint	m_rightTop	=new JFPoint();

   /**
    *   Left-bottom vertex of rectangle.
    */
   protected   JFPoint	m_leftBottom	=new JFPoint();

   /**
    *   Right-bottom vertex of rectangle.
    */
   protected   JFPoint	m_rightBottom	=new JFPoint();
   
   /**
    *   Get x coordiate of this rectangle.
    *   Consider this rectangle should be scaled or rotated, so 
    *   return a minimum x coordinate of all vertexs here.
    *
    *   @return  The x coordiate.
    *
    */ 	
   public double getX(){
   	return Math.min(Math.min(m_leftTop.getX(),m_rightTop.getX()),
   			Math.min(m_leftBottom.getX(),m_rightBottom.getX())
   			);
   }

   /**
    *   Get y coordiate of this rectangle.
    *   Consider this rectangle should be scaled or rotated, so 
    *   return a minimum y coordinate of all vertexs here.
    *
    *   @return  The y coordiate.
    *
    */ 	
   public double getY(){
   	return Math.min(Math.min(m_leftTop.getY(),m_rightTop.getY()),
   			Math.min(m_leftBottom.getY(),m_rightBottom.getY())
   			);
   }

   /**
    *   Get width of this rectangle.
    *   Consider this rectangle should be scaled or rotated, so 
    *   return a maxmum width of this rectangle.
    *
    *   @return  Width of this rectangle.
    *
    */ 	
   public double getWidth(){
   	double maxX	=Math.max(Math.max(m_leftTop.getX(),m_rightTop.getX()),
   			 	  Math.max(m_leftBottom.getX(),m_rightBottom.getX())
   				 );
   	return maxX - getX();
   }

   /**
    *   Get height of this rectangle.
    *   Consider this rectangle should be scaled or rotated, so 
    *   return a maxmum height of this rectangle.
    *
    *   @return  Height of this rectangle.
    *
    */ 	
   public double getHeight(){
   	double maxY	=Math.max(Math.max(m_leftTop.getY(),m_rightTop.getY()),
   			 	  Math.max(m_leftBottom.getY(),m_rightBottom.getY())
   				 );
   	return maxY - getY();
   }


   /**
    *   Constructor for Rect.
    */ 	
   public Rect(){
   }


   /**
    *   Constructor for Rect.
    *   
    *   @param rect A rectangle object.
    *
    */ 	
   public Rect(Rect rect){
   	setValue(rect);
   }


   /**
    *   Constructor for Rect.
    *
    *   @param x  X coordiate.
    *
    *   @param y  Y coordiate.
    *
    *   @param w  Width of this rectangle.
    *
    *   @param h  Height of this rectangle.
    *
    */ 	
   public Rect(double x, double y, double w, double h){
   		setValue(x,y,w,h);
   }

   /**
    *   Set the bounds of current rectangle.
    *
    *   @param x  X coordiate.
    *
    *   @param y  Y coordiate.
    *
    *   @param w  Width of this rectangle.
    *
    *   @param h  Height of this rectangle.
    *
    */ 	
   public void setValue(double x, double y, double w, double h){
   		m_leftTop.setX(x);
   		m_leftTop.setY(y);
   			
   		m_rightTop.setX(x+w);
   		m_rightTop.setY(y);

   		m_leftBottom.setX(x);
   		m_leftBottom.setY(y+h);

   		m_rightBottom.setX(x+w);
   		m_rightBottom.setY(y+h);
   }

   /**
    *   Set the value of current rectangle.
    *
    *   @param rect A new rectangle.
    *
    */ 	
   public void setValue(Rect rect){
   	if (rect!=null){
   		this.m_leftTop.setX(rect.m_leftTop.getX());
   		this.m_leftTop.setY(rect.m_leftTop.getY());
   			
   		this.m_rightTop.setX(rect.m_rightTop.getX());
   		this.m_rightTop.setY(rect.m_rightTop.getY());

   		this.m_leftBottom.setX(rect.m_leftBottom.getX());
   		this.m_leftBottom.setY(rect.m_leftBottom.getY());

   		this.m_rightBottom.setX(rect.m_rightBottom.getX());
   		this.m_rightBottom.setY(rect.m_rightBottom.getY());
   	}
   }

          
   /**
    *   Constructor for Rect.
    *
    *   @param startPoint   A startPoint for one endpoint of this rectangle.
    *
    *   @param endPoint   An endPoint for another endpoint of this rectangle.
    *
    */ 	
   public Rect(JFPoint startPoint, JFPoint endPoint){
   	if (startPoint!=null && endPoint!=null){
   		double x1 =startPoint.getX();
   		double y1 =startPoint.getY();
   		double x2 =endPoint.getX();
   		double y2 =endPoint.getY();
   		double tmp=0;
	
		//swap the two x coordinates, so always let x2>x1   	
   		if (x1>x2){
   			tmp	=x1;
   			x1	=x2;
   			x2	=tmp;
   		}
   	
		//swap the two y coordinates, so always let y2>y1   	
   		if (y1>y2){
   			tmp	=y1;
   			y1	=y2;
   			y2	=tmp;
   		}
	
		//initiate parameters of this rectangle   	
   		m_leftTop.setX(x1);
   		m_leftTop.setY(y1);
   			
   		m_rightTop.setX(x2);
   		m_rightTop.setY(y1);

   		m_leftBottom.setX(x1);
   		m_leftBottom.setY(y2);

   		m_rightBottom.setX(x2);
   		m_rightBottom.setY(y2);
   	}
   }


   /**
    *   Get the bounds of this rectangle.
    *
    *   @return The bounds rectangle.
    *
    */ 	
   public Rect getBounds(){
   	return new Rect(getX(),getY(),getWidth(),getHeight());
   }


   /**
    *   Get a vertex point of this rectangle.
    *
    *   @return A vertex point.
    *
    */ 	
   public JFPoint getVertex(int vertexType){
	
	switch (vertexType){
		case VERTEXTYPE_LEFTTOP:
			return m_leftTop;
			
		case VERTEXTYPE_RIGHTTOP:
			return m_rightTop;
			
		case VERTEXTYPE_LEFTBOTTOM:
			return m_leftBottom;
			
		case VERTEXTYPE_RIGHTBOTTOM:
			return m_rightBottom;
			
		default:
			return null;
	}
   }

   /**
    *   Set a vertex point of this rectangle.
    *
    *   @param vertexType An vertex type of a rectangle.
    *
    *   @param pnt  A new vertex point.
    *
    */ 	
   public void  setVertex(int vertexType,JFPoint pnt){
   	if (pnt!=null){
   		setVertex(vertexType,pnt.getX(),pnt.getY());
   	}
   }

   /**
    *   Set a vertex point of this rectangle.
    *
    *   @param vertexType An vertex type of a rectangle.
    *
    *   @param x,&nbsp;y  Coordinates of a new vertex point.
    *
    */ 	
   public void  setVertex(int vertexType,double x, double y){
	JFPoint pnt =getVertex(vertexType);
	if (pnt!=null)
		pnt.setValue(x,y);
   }

   /**
    *   Move a vertex of a rectangle.
    *   This will occurr under different requirements. One is for moving
    *   a vertex of a rectangle, so we get a parallelogram or trapezoid; the other
    *   is for scaling the rectangle but not change it's shape.
    *
    *   @param vertexType An vertex type of a rectangle.
    *
    *   @param x,&nbsp;y  Coordinates of a new vertex point.
    *
    *   @param moveType  scale, rectangular, parallel or trapezoid  vertex moving type.
    *
    */
    public void moveVertex(int vertexType,double x, double y, int moveType){
	
	//current moving vertex	
	JFPoint currVertex;
	//two neighbour vertexes, and a peer vertex within a rectangle.
	JFPoint	neighborVertex1,neighborVertex2,peerVertex;
	
	//get the four vertexes.	    	
	switch (vertexType){
		case VERTEXTYPE_LEFTTOP:
			currVertex	=m_leftTop;
			neighborVertex1	=m_leftBottom;
			neighborVertex2	=m_rightTop;
			peerVertex	=m_rightBottom;
			break;
			
		case VERTEXTYPE_RIGHTTOP:
			currVertex	=m_rightTop;
			neighborVertex1	=m_leftTop;
			neighborVertex2	=m_rightBottom;
			peerVertex	=m_leftBottom;
			break;
			
		case VERTEXTYPE_LEFTBOTTOM:
			currVertex	=m_leftBottom;
			neighborVertex1	=m_rightBottom;
			neighborVertex2	=m_leftTop;
			peerVertex	=m_rightTop;
			break;
			
		case VERTEXTYPE_RIGHTBOTTOM:
			currVertex	=m_rightBottom;
			neighborVertex1	=m_rightTop;

⌨️ 快捷键说明

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