labelline.java

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

JAVA
685
字号

   /**
    *   Set the coordinates of control point4.
    *
    *   @param x, y  A new coordinates of control point4.
    *
    */ 	
   public void  setCtrlPoint4(double x, double y){
   	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
   	JFPoint  originCtrl4	=endPoint.nearPoint(slope,CTRL_POINT_LEN, startPoint,m_ctrl1Direction);
   	JFPoint  uprightFoot	=LineSeg.uprightFoot(slope,endPoint.getX(),endPoint.getY(),x,y);
   	if (originCtrl4.middleOf(uprightFoot,endPoint))
   		m_ctrl4Len	=endPoint.distance(uprightFoot);
   	else
   		m_ctrl4Len	=CTRL_POINT_LEN;
   }


//****************** other methods ***********************************

   /**
    *   Set a label line's value by a new label line.
    *
    *   @param line  A new label line.
    *
    */ 	
   public void setValue(LabelLine line){
   	if (line!=null){
   		super.setValue(line);
   		
   		m_ctrl1Direction	=line.getCtrl1Direction();
   		m_ctrl1Len		=line.getCtrl1Len();
   		m_ctrl2Len		=line.getCtrl2Len();
   		m_ctrl3Len		=line.getCtrl3Len();
   		m_ctrl4Len		=line.getCtrl4Len();
   	}
   }


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

   /**
    *   Constructor for LabelLine.
    *
    *   @param x1,y1  Coordiates of first endpoint.
    *
    *   @param x2,y2  Coordiates of second endpoint.
    *
    */ 	
   public LabelLine(double x1, double y1, double x2, double y2){
   	super.setValue(x1,y1,x2,y2);
   }

   /**
    *   Constructor for LabelLine.
    *
    *   @param startPoint  The first endpoint.
    *
    *   @param endPoint  The second endpoint.
    *
    */ 	
   public LabelLine(JFPoint startPoint, JFPoint endPoint){
   	super.setValue(startPoint,endPoint);
   }

   /**
    *   Constructor for LabelLine.
    *
    *   @param lineSeg A JFLine Segment.
    *
    */ 	
   public LabelLine(LabelLine line){
   	if (line!=null){
   		setValue(line);
   	}
   }


   /**
    *   Get the bounds of this line segment.
    *
    *   @return The bounds rectangle.
    *
    */ 	
   public Rect getBounds(){

	JFPoint ctrl1	=getCtrlPoint1();
	JFPoint ctrl2	=getCtrlPoint2();
	JFPoint ctrl3	=getCtrlPoint3();
	JFPoint ctrl4	=getCtrlPoint4();
	
   	double minx	=Math.min(getX1(),getX2());
   	       minx	=Math.min(minx,ctrl1.getX());
   	       minx	=Math.min(minx,ctrl2.getX());
   	       minx	=Math.min(minx,ctrl3.getX());
   	       minx	=Math.min(minx,ctrl4.getX());
   	       
   	double miny	=Math.min(getY1(),getY2());
   	       miny	=Math.min(miny,ctrl1.getY());
   	       miny	=Math.min(miny,ctrl2.getY());
   	       miny	=Math.min(miny,ctrl3.getY());
   	       miny	=Math.min(miny,ctrl4.getY());

   	double maxx	=Math.max(getX1(),getX2());
   	       maxx	=Math.max(maxx,ctrl1.getX());
   	       maxx	=Math.max(maxx,ctrl2.getX());
   	       maxx	=Math.max(maxx,ctrl3.getX());
   	       maxx	=Math.max(maxx,ctrl4.getX());
   	       
   	double maxy	=Math.max(getY1(),getY2());
   	       maxy	=Math.max(maxy,ctrl1.getY());
   	       maxy	=Math.max(maxy,ctrl2.getY());
   	       maxy	=Math.max(maxy,ctrl3.getY());
   	       maxy	=Math.max(maxy,ctrl4.getY());
   	       	
   	
   	return new Rect(minx,miny,maxx-minx, maxy-miny);
   }

   /**
    *   Mirror this line by a x coordinate. We make a left-right mirror here.
    *
    *   @param baseX  A mirror base x coordinate.
    *
    */ 	
   public void mirrorBy(double baseX){
   	super.mirrorBy(baseX);
   	m_ctrl1Direction	=!m_ctrl1Direction;
   }

   /**
    *   Reverse this line by a y coordinate. We make a up-down flip here.
    *
    *   @param baseY  A flip base y coordinate.
    *
    */ 	
   public void flipBy(double baseY){
   	super.flipBy(baseY);
   	m_ctrl1Direction	=!m_ctrl1Direction;
   }


   /**
    *   Test if a point(x, y coordinates for instead) is on this line.
    *   Here we used an analog offset for 'pick' this line.
    *
    *   @param x   X coordinate of this point.
    *
    *   @param y   Y coordinate of this point.
    *
    *   @return True if the point is on this line, false otherwise.
    *
    */ 	
   public boolean contains(double x, double y,double pickOffset){
   	if (super.contains(x,y,pickOffset))
   		return true;

   	LineSeg	line	=new LineSeg();
	JFPoint ctrl1	=getCtrlPoint1();
	JFPoint ctrl2	=getCtrlPoint2();
	line.setValue(ctrl1,ctrl2);
	if (line.contains(x,y,pickOffset))
		return true;
		
	
	JFPoint ctrl3	=getCtrlPoint3();
	JFPoint ctrl4	=getCtrlPoint4();
	line.setValue(ctrl3,ctrl4);
	if (line.contains(x,y,pickOffset))
		return true;
	
	return false;
   }


    /**
     * Tests if the specified line segment intersects this
     * <code>LabelLine</code>.
     *
     * @param x1,&nbsp;y1 the first endpoint of the specified
     * line segment
     * @param x2,&nbsp;y2 the second endpoint of the specified
     * line segment
     * @return <code>true</code> if the specified line segment intersects
     * the this <code>LabelLine</code>; <code>false</code>
     * otherwise.
     */
    public boolean intersects(double x1, double y1, double x2, double y2) {

  	if (super.intersects(x1,y1,x2,y2))
   		return true;

   	LineSeg	line	=new LineSeg();
	JFPoint ctrl1	=getCtrlPoint1();
	JFPoint ctrl2	=getCtrlPoint2();
	line.setValue(ctrl1,ctrl2);
	if (line.intersects(x1,y1,x2,y2))
		return true;
		
	
	JFPoint ctrl3	=getCtrlPoint3();
	JFPoint ctrl4	=getCtrlPoint4();
	line.setValue(ctrl3,ctrl4);
	if (line.intersects(x1,y1,x2,y2))
		return true;
	
	return false;

  }


    /**
     * Tests if the specified rectangle intersects the interior of this
     * <code>LineSeg</code>.
     * @param rect the specified {@link Rect} to test for intersection
     * with the interior of this <code>LineSeg</code>
     * @return <code>true</code> if the specified <code>Rect</code>
     * intersects the interior of this <code>LineSeg</code>;
     * <code>false</code> otherwise.
     */
    public boolean intersects(Rect rect) {
	
	JFPoint startPoint	=getPoint1();
	JFPoint endPoint	=getPoint2();
	
	JFPoint	ctrl1	=getCtrlPoint1();
	JFPoint	ctrl2	=getCtrlPoint2();
	JFPoint	ctrl3	=getCtrlPoint3();
	JFPoint	ctrl4	=getCtrlPoint4();
			
	LineSeg side	=null;
	//test if each side of rectangle intersects.
	for (int i = Rect.SIDETYPE_LEFT; i<=Rect.SIDETYPE_BOTTOM; i++){
		side	=rect.getSide(i);
		if (side.intersects(startPoint, endPoint) || side.intersects(ctrl1,ctrl2) || side.intersects(ctrl3,ctrl4))
			return true;
	}

	//test if current rectangle is inside the target rectangle.
	return  rect.contains(ctrl1) ||
		rect.contains(ctrl2) ||
		rect.contains(ctrl3) ||
		rect.contains(ctrl4) ||
		rect.contains(startPoint)||
		rect.contains(endPoint);
    }	



   /**
    *   Convert this object to String 
    * 
    *   @return  An string represents the content of the object
    *
    */ 	
   public String toString(){
  	StringBuffer buf =new StringBuffer();
  	buf.append(super.toString());
  	
  	buf.append(";ctrl1Direction=");  	buf.append(m_ctrl1Direction);
  	buf.append(";ctrl1Len=");  		buf.append(m_ctrl1Len);
  	buf.append(";ctrl2Len=");  		buf.append(m_ctrl2Len);
  	buf.append(";ctrl3Len=");  		buf.append(m_ctrl3Len);
  	buf.append(";ctrl4Len=");  		buf.append(m_ctrl4Len);
  	
  	return buf.toString();
   }

 
   /**
    *   Creates a new object of the same class and with the same contents as this object.
    * 
    *   @return  A clone of this instance.
    *
    */ 	
  public Object clone() throws CloneNotSupportedException{
  	try{
  		return new LabelLine(this);
  		
	}catch(Exception e){
		throw new CloneNotSupportedException(e.getMessage());
	}
  }


   /**
    *   Returns the hashcode for this Object.
    * 
    *   @return hash code for this Point2D.
    *
    */ 	
  public int hashCode(){
      	
  	
  	return 	super.hashCode() ^ 
  		(m_ctrl1Direction?1:0) ^
  		(int)m_ctrl1Len^
  		(int)m_ctrl2Len^
  		(int)m_ctrl3Len^
  		(int)m_ctrl4Len
  		;
  }


   /**
    *   Determines whether or not two objects are equal. 
    * 
    * 	@param obj  an object to be compared with this object 
    * 
    *   @return true if the object to be compared is an instance of Port and has the same values; false otherwise.
    *
    */ 	
  public boolean equals(Object obj){

      if (obj == this)
             	return true;

      if (!(obj instanceof LabelLine))
            	return false;

      LabelLine	line	=(LabelLine)obj;
      
      return	m_ctrl1Direction==line.getCtrl1Direction() &&
      		m_ctrl1Len==line.getCtrl1Len()&&      		
      		m_ctrl2Len==line.getCtrl2Len()&&      		
      		m_ctrl3Len==line.getCtrl3Len()&&      		
      		m_ctrl4Len==line.getCtrl4Len();
      		
  }



}

⌨️ 快捷键说明

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