labelline.java

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

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


import java.awt.Graphics;


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

 /**
 * LabelLine class.  A class used to represent a line to describe the size of shapes.
 *
 *   <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 LabelLine extends LineSeg {

  /**
   *   A default minimum length of each ctrl point to end point of label line.
   *   For a label line, it will has four control points, two for start point,
   *   and the other two for end point.
   */   
   public final int CTRL_POINT_LEN	=10;
  
   /**
    *   1)Direction that the vector from start point to control point1 compares 
    *   to the vector from start point to end point.
    *   2)False if counter-clockwise, true if clockwise.
    *   3)If ctrl1Direction is under counter-clockwise, then:
    *     ctrl2 is under clockwise, ctrl3 is under counter-clockwise,
    *     ctrl4 is under clockwise.
    *   4)If reverse or mirror this label line, each direction will be !Direction(true to false, false to true).  
    */
   private	boolean	 m_ctrl1Direction	=false;

   /**
    *   Length of control point1 to start point.
    */
   private	double 	m_ctrl1Len	=CTRL_POINT_LEN;
   /**
    *   Length of control point2 to start point.
    */
   private	double 	m_ctrl2Len	=CTRL_POINT_LEN;
   /**
    *   Length of control point3 to end point.
    */
   private	double 	m_ctrl3Len	=CTRL_POINT_LEN;
   /**
    *   Length of control point4 to end point.
    */
   private	double 	m_ctrl4Len	=CTRL_POINT_LEN;


   /**
    *   1)Get the direction that the vector from start point to control point1 compares 
    *   to the vector from start point to end point.
    *   2)False if counter-clockwise, true if clockwise.
    *   3)If ctrl1Direction is under counter-clockwise, then:
    *     ctrl2 is under clockwise, ctrl3 is under counter-clockwise,
    *     ctrl4 is under clockwise.
    *   4)If reverse or mirror this label line, each direction will be !Direction(true to false, false to true).  
    *   
    *   @return  The direction.
    *
    */ 	
   public boolean  getCtrl1Direction(){
   	return m_ctrl1Direction;
   }

   /**	
    *   Set the direction that the vector from start point to control point1 compares 
    *   to the vector from start point to end point.
    *   @param direction A new direction.
    *
    */ 	
   public void  setCtrl1Direction(boolean direction){
   	m_ctrl1Direction	=direction;
   }


   /**
    *   Get the slope of upright line of this line.
    *
    *   @return  The slope.
    *
    */ 	
   private double  getUprightSlope(){
   	//slope of the line from start point to end point
   	double   slope		=getSlope();
   	
   	//slope of the unright line of this line.
   	if (slope==GeomConst.LARGE_VALUE)
   		slope	=0;
   	else if (slope==0)
   		slope   =GeomConst.LARGE_VALUE;
   	else
   		slope	=-1/slope;
   	
   	return slope;
   }


//******************control point 1 ***********************************

   /**
    *   Get the length of control point1 to start point.
    *  @return the lengh of ctrl point1 to start point 
    */
   public	double 	getCtrl1Len(){
   	return m_ctrl1Len;
   }

   /**
    *   Set the length of control point1 to start point.
    *  @param len the lengh of ctrl point1 to start point 
    */
   public	void 	setCtrl1Len(double len){
   	m_ctrl1Len	=len;
   }


   /**
    *   Get the coordinates of control point1.
    *
    *   @return  The coordinates of control point1.
    *
    */ 	
   public JFPoint  getCtrlPoint1(){
   	JFPoint  startPoint	=getPoint1();
   	JFPoint  endPoint	=getPoint2();
   	double   slope		=getUprightSlope();
   	return startPoint.nearPoint(slope,m_ctrl1Len, endPoint,m_ctrl1Direction);
   }

   /**
    *   Set the coordinates of control point1.
    *
    *   @param pnt  A new coordinates of control point1.
    *
    */ 	
   public void  setCtrlPoint1(JFPoint pnt){
   	setCtrlPoint1(pnt.getX(),pnt.getY());
   }

   /**
    *   Set the coordinates of control point1.
    *
    *   @param x,&nbsp;y  A new coordinates of control point1.
    *
    */ 	
   public void  setCtrlPoint1(double x, double y){
   	JFPoint  startPoint	=getPoint1();
   	JFPoint  endPoint	=getPoint2();

   	double   slope	=getUprightSlope();
   	JFPoint  originCtrl1	=startPoint.nearPoint(slope,CTRL_POINT_LEN, endPoint,m_ctrl1Direction);
   	JFPoint  uprightFoot	=LineSeg.uprightFoot(slope,startPoint.getX(),startPoint.getY(),x,y);
   	if (originCtrl1.middleOf(uprightFoot,startPoint))
   		m_ctrl1Len	=startPoint.distance(uprightFoot);
   	else
   		m_ctrl1Len	=CTRL_POINT_LEN;
   }


//******************control point 2 ***********************************

   /**
    *   Get the length of control point2 to start point.
    *  @return the lengh of ctrl point3 to start point 
    */
   public	double 	getCtrl2Len(){
   	return m_ctrl2Len;
   }

   /**
    *   Set the length of control point2 to start point.
    *  @param len the lengh of ctrl point3 to start point 
    */
   public	void 	setCtrl2Len(double len){
   	m_ctrl2Len	=len;
   }


   /**
    *   Get the coordinates of control point2.
    *
    *   @return  The coordinates of control point2.
    *
    */ 	
   public JFPoint  getCtrlPoint2(){
   	JFPoint  startPoint	=getPoint1();
   	JFPoint  endPoint	=getPoint2();
   	double   slope		=getUprightSlope();
   	//ctrl2Direction =!ctrl1Direction
   	return startPoint.nearPoint(slope,m_ctrl2Len, endPoint,!m_ctrl1Direction);
   }

   /**
    *   Set the coordinates of control point2.
    *
    *   @param pnt  A new coordinates of control point2.
    *
    */ 	
   public void  setCtrlPoint2(JFPoint pnt){
   	setCtrlPoint2(pnt.getX(),pnt.getY());
   }

   /**
    *   Set the coordinates of control point2.
    *
    *   @param x,&nbsp;y  A new coordinates of control point2.
    *
    */ 	
   public void  setCtrlPoint2(double x, double y){
   	JFPoint  startPoint	=getPoint1();
   	JFPoint  endPoint	=getPoint2();
   	double   slope		=getUprightSlope();
   	//ctrl2Direction =!ctrl1Direction
   	JFPoint  originCtrl2	=startPoint.nearPoint(slope,CTRL_POINT_LEN, endPoint,!m_ctrl1Direction);
   	JFPoint  uprightFoot	=LineSeg.uprightFoot(slope,startPoint.getX(),startPoint.getY(),x,y);
   	if (originCtrl2.middleOf(uprightFoot,startPoint))
   		m_ctrl2Len	=startPoint.distance(uprightFoot);
   	else
   		m_ctrl2Len	=CTRL_POINT_LEN;
   }

//******************control point 3 ***********************************

   /**
    *   Get the length of control point3 to end point.
    *  @return the lengh of ctrl point3 to end point.
    */
   public	double 	getCtrl3Len(){
   	return m_ctrl3Len;
   }

   /**
    *   Set the length of control point3 to end point.
    *  @param len the lengh of ctrl point3 to end point.
    */
   public	void 	setCtrl3Len(double len){
   	m_ctrl3Len	=len;
   }

   /**
    *   Get the coordinates of control point3.
    *
    *   @return  The coordinates of control point3.
    *
    */ 	
   public JFPoint  getCtrlPoint3(){
   	JFPoint  startPoint	=getPoint1();
   	JFPoint  endPoint	=getPoint2();
   	double   slope		=getUprightSlope();
   	//although ctrl3Direction =ctrl1Direction, but here we used a vector from end point to start point for reference,
   	//so use !ctrl3Direction
   	return endPoint.nearPoint(slope,m_ctrl3Len, startPoint,!m_ctrl1Direction);
   }

   /**
    *   Set the coordinates of control point3.
    *
    *   @param pnt  A new coordinates of control point3.
    *
    */ 	
   public void  setCtrlPoint3(JFPoint pnt){
   	setCtrlPoint3(pnt.getX(),pnt.getY());
   }

   /**
    *   Set the coordinates of control point3.
    *
    *   @param x,&nbsp;y  A new coordinates of control point3.
    *
    */ 	
   public void  setCtrlPoint3(double x, double y){
   	JFPoint  startPoint	=getPoint1();
   	JFPoint  endPoint	=getPoint2();
   	double   slope		=getUprightSlope();
   	//although ctrl3Direction =ctrl1Direction, but here we used a vector from end point to start point for reference,
   	//so use !ctrl3Direction
   	JFPoint  originCtrl3	=endPoint.nearPoint(slope,CTRL_POINT_LEN, startPoint,!m_ctrl1Direction);
   	JFPoint  uprightFoot	=LineSeg.uprightFoot(slope,endPoint.getX(),endPoint.getY(),x,y);
   	if (originCtrl3.middleOf(uprightFoot,endPoint))
   		m_ctrl3Len	=endPoint.distance(uprightFoot);
   	else
   		m_ctrl3Len	=CTRL_POINT_LEN;
   }


//******************control point 4 ***********************************

   /**
    *   Get the length of control point4 to end point.
    *  @return the lengh of ctrl point4 to end point.
    */
   public	double 	getCtrl4Len(){
   	return m_ctrl4Len;
   }

   /**
    *   Set the length of control point4 to end point.
    *  @param len the lengh of ctrl point4 to end point.
    */
   public	void 	setCtrl4Len(double len){
   	m_ctrl4Len	=len;
   }

   /**
    *   Get the coordinates of control point4.
    *
    *   @return  The coordinates of control point4.
    *
    */ 	
   public JFPoint  getCtrlPoint4(){
   	JFPoint  startPoint	=getPoint1();
   	JFPoint  endPoint	=getPoint2();
   	double   slope		=getUprightSlope();
   	//although ctrl4Direction =!ctrl1Direction, but here we used a vector from end point to start point for reference,
   	//so use !ctrl4Direction
   	return endPoint.nearPoint(slope,m_ctrl4Len, startPoint,m_ctrl1Direction);
   }

   /**
    *   Set the coordinates of control point4.
    *
    *   @param pnt  A new coordinates of control point4.
    *
    */ 	
   public void  setCtrlPoint4(JFPoint pnt){
   	setCtrlPoint4(pnt.getX(),pnt.getY());
   }

⌨️ 快捷键说明

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