regularline.java

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

JAVA
1,911
字号
	}
   }


   /**
    *   Scale current rectangle by specified points and scale percent.
    *   We only support a concurrent width-height scale here, suppose width as the length from
    *   basePoint to refPoint1, height as the length from basePoint to refPoint2, and 
    *   one scale percent acts on both width and height.
    *
    *   @param basePoint A base point that is unmovable.
    *   @param refPoint1 A 'width' reference point.
    *   @param refPoint2 A 'height' reference point.
    *   @param scale A reference scale percent.
    *
    */ 	
   public void scaleBy(JFPoint basePoint, JFPoint refPoint1, JFPoint refPoint2, double scale){

   	JFPoint center	=getCenter();

	Iterator it	=m_nodeList.iterator();
	while (it!=null && it.hasNext()){
		JFPointNode node=(JFPointNode)it.next();
		node.scaleBy(center.getX(),center.getY(),scale);
	}

  
	JFPoint newCenter	=Rect.newScaleCenter(center,basePoint,refPoint1,refPoint2,scale);

	//move center
	moveBy(newCenter.getX()-center.getX(),newCenter.getY()-center.getY());
	
   }


   /**
    *   Scale current line by a specified x and y scale.<br>
    *   This is a special scale method used to scale a shape in arbitrary x and y scale.<br>
    *   Please see AbstractShape.scaleBy for detailed description.
    *
    *   @param basePoint A base scale point for scaling reference.
    *   @param xScale  A scale percentage in x coordinate, default to 1.0
    *   @param yScale  A scale percentage in y coordinate, default to 1.0
    *
    */ 	
   public void scaleBy(JFPoint basePoint,double xScale, double yScale){
   	if (basePoint==null)
   		return;
   	double x	=basePoint.getX();
   	double y	=basePoint.getY();

	Iterator it	=m_nodeList.iterator();
	while (it!=null && it.hasNext()){
		JFPointNode node=(JFPointNode)it.next();
		node.scaleBy(x,y,xScale,yScale);
	}
   	
   }	
   
   

   /**
    *   Test if a point is on this line.
    *
    *   @param pnt A point to be measured.
    *
    *   @return True if the point is on this line, false otherwise.
    *
    */ 	
   public boolean contains(JFPoint pnt){
   	if (pnt==null)
   		return false;
   	else
   		return contains(pnt,0);
   }

   /**
    *   Test if a point is on this line.
    *   Here we used an analog offset for 'pick' this line.
    *
    *   @param pnt A point to be measured.
    *
    *   @param pickOffset An analog offset for 'pick' this line.
    *
    *   @return True if the point is on this line, false otherwise.
    *
    */ 	
   public boolean contains(JFPoint pnt,double pickOffset){
   	if (pnt==null)
   		return false;
   	else
   		return contains(pnt.getX(),pnt.getY(),pickOffset);
   }

   /**
    *   Test if a point(x, y coordinates for instead) is on 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){
   	return contains(x,y,0);
   }

   /**
    *   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){
   	LineSeg line	=new LineSeg();
   	JFPointNode lastNode=getNode(0);
   	if (lastNode==null)
   		return false;
   		
   	while (true){
		JFPointNode	node=getNextNode(m_nodeList,lastNode,false);
		if (node==null)
			return false;
			
		line.setPoint1(lastNode);
		line.setPoint2(node);
		if (line.contains(x,y,pickOffset))
			return true;
		else
			lastNode	=node;
   	}
   }

   /**
    *   Pick a line segment of this regularLine by a point with a pickoffset.
    *   @param x   X coordinate of this point.
    *   @param y   Y coordinate of this point.
    *   @param pickOffset An analog pick offset that great equal than 0.
    *
    *   @return A line segment picked.
    *
    */ 	
   public LineSeg pickLine(double x, double y,double pickOffset){
        List lineList   =pickLineList(x,y,pickOffset,false);
        if (lineList!=null && lineList.size()>0){
                try{
                        return  (LineSeg)lineList.get(0);
                }catch(Exception e){
                }
        }
        
        return null;
   }

   /**
    *   Pick a line segment of this regularLine by specified a point with a pickoffset, and start point and end point restricted.
    *   @param x   X coordinate of this point.
    *   @param y   Y coordinate of this point.
    *   @param pickOffset An analog pick offset that great equal than 0.
    *   @param startPoint   Start point of a line segment.
    *   @param endPoint  End point of a line segment.
    *
    *   @return A line segment picked.
    *
    */ 	
   public LineSeg pickLine(double x, double y, double pickOffset,JFPoint startPoint, JFPoint endPoint){
        List lineList   =pickLineList(x,y,pickOffset,true);
        if (lineList==null || lineList.size()==0){
                return null;
        }

        LineSeg line;
        JFPoint pnt1,pnt2;

        //complete same start point and end point                
        Iterator it     =lineList.iterator();
        while (it!=null && it.hasNext()){
                line    =(LineSeg)it.next();
                pnt1    =line.getPoint1();
                pnt2    =line.getPoint2();
                if (pnt1.equals(startPoint,true) && pnt2.equals(endPoint,true)||
                    pnt2.equals(startPoint,true) && pnt1.equals(endPoint,true)){
                        return line;
                }
        }

        //partly same start point or end point                
        it     =lineList.iterator();
        while (it!=null && it.hasNext()){
                line    =(LineSeg)it.next();
                if (line.contains(startPoint) && line.contains(endPoint)){
                        return line;
                }
        }


        //a little same start point or end point                
        it     =lineList.iterator();
        while (it!=null && it.hasNext()){
                line    =(LineSeg)it.next();
                if (line.contains(startPoint,GeomConst.PICK_OFFSET) && line.contains(endPoint,GeomConst.PICK_OFFSET)){
                        return line;
                }
        }
        
        //return one line segment of the list.
        try{
                line    =(LineSeg)lineList.get(0);

        }catch(Exception e){
                line    =null;
        }

        return line;
   }



   /**
    *   Pick a line segment list of this regularline by a point with a pickoffset.
    *   @param x   X coordinate of this point.
    *   @param y   Y coordinate of this point.
    *   @param pickOffset An analog pick offset that great equal than 0.
    *   @param pickAll Pick all line matched.
    *
    *   @return A line segment list picked.
    *
    */ 	
   private List pickLineList(double x, double y,double pickOffset,boolean pickAll){
   	List ret	=new ArrayList();
   	LineSeg line	=new LineSeg();
   	JFPointNode lastNode=getNode(0);
   	JFPointNode node;
   	if (lastNode==null)
   		return ret;
  	
	while (true){
		node=getNextNode(lastNode,false);
		if (node==null)
			break;
		

  		line.setPoint1(lastNode);
  		line.setPoint2(node);
  		if (line.contains(x,y,pickOffset)){
  			ret.add(new LineSeg(line));
  			if (!pickAll)
  				break;
  		}
		
  		lastNode  =node;
	}
  	
  	return ret;
   }
   
   
   /**
    *   Test if current regularline can add a port according to the position x,y.
    *   @param x   X coordinate of the port.
    *   @param y   Y coordinate of the port.
    *   @param startPoint,endPoint,portPoint Three major parameters of a port.
    *
    *   @return True if can add a new port, false otherwise.
    *
    */ 	
   public boolean canAddPort(double x, double y,JFPoint startPoint, JFPoint endPoint, JFPoint portPoint){
	LineSeg line	=pickLine(x,y,GeomConst.PICK_OFFSET);
	if (line==null)
		return false;
	
	startPoint.setValue(line.getPoint1());
	endPoint.setValue(line.getPoint2());
	portPoint.setValue(line.uprightFoot(x,y));
	return true;
   }


 
    /**
     * Tests if the specified line segment intersects the this 
     * <code>RegularLine</code>.
     * @param l the specified {@link RegularLine} to test for intersection
     * with this <code>RegularLine</code>
     * @return <code>true</code> if the specified <code>RegularLine</code>
     * intersects this <code>RegularLine</code>;
     * <code>false</code> otherwise.
     * @since 1.2
     */
    public boolean intersects(LineSeg l) {
    	if (l==null)
    		return false;
    	else
		return intersects(l.getX1(), l.getY1(), l.getX2(), l.getY2());
    }


    /**
     * Tests if the specified line segment intersects this
     * <code>RegularLine</code>.
     *
     * @param startPoint the first endpoint of the specified
     * line segment
     * @param endPoint the second endpoint of the specified
     * line segment
     * @return <code>true</code> if the specified line segment intersects
     * the this <code>RegularLine</code>; <code>false</code>
     * otherwise.
     */
    public boolean intersects(JFPoint startPoint, JFPoint endPoint) {
    	return intersects(startPoint.getX(),startPoint.getY(),endPoint.getX(),endPoint.getY());
   }

    /**
     * Tests if the specified line segment intersects this
     * <code>RegularLine</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>RegularLine</code>; <code>false</code>
     * otherwise.
     */
    public boolean intersects(double x1, double y1, double x2, double y2) {
   	LineSeg line	=new LineSeg();
   	JFPointNode lastNode=getNode(0);
   	if (lastNode==null)
   		return false;
   		
   	while (true){
		JFPointNode	node=getNextNode(m_nodeList,lastNode,false);
		if (node==null)
			return false;
			
		line.setPoint1(lastNode);
		line.setPoint2(node);
		if (line.intersects(x1,y1,x2,y2))
			return true;
		else
			lastNode	=node;
   	}

   }

    /**
     * Tests if the specified rectangle intersects the interior of this
     * <code>RegularLine</code>.
     * @param rect the specified {@link RegularLine} to test for intersection
     * with the interior of this <code>RegularLine</code>
     * @return <code>true</code> if the specified <code>RegularLine</code>
     * intersects the interior of this <code>RegularLine</code>;
     * <code>false</code> otherwise.
     */
    public boolean intersects(Rect rect) {
	
	//test if any node of this regular line is inside the rectangle.
	Iterator it	=m_nodeList.iterator();
	while (it!=null && it.hasNext()){
		JFPointNode node=(JFPointNode)it.next();
		if (rect.contains(node))
			return true;
	}

	//test each side of this regular line intersects with the rectangle.
   	LineSeg line	=new LineSeg();
   	JFPointNode lastNode=getNode(0);
   	if (lastNode==null)
   		return false;
   		
   	while (true){
		JFPointNode	node=getNextNode(m_nodeList,lastNode,false);
		if (node==null)
			return false;
			
		line.setPoint1(lastNode);
		line.setPoint2(node);
		if (rect.intersects(line))
			return true;
		else
			lastNode	=node;
   	}

    }


   /**
    *   Convert this object to String 
    * 
    *   @return  An string represents the content of the object
    *
    */ 	
   public String toString(){
  	StringBuffer buf =new StringBuffer();

	Iterator it	=m_nodeList.iterator();
	int i=0;
	while (it!=null && it.hasNext()){
		JFPointNode node=(JFPointNode)it.next();
		i++;
		buf.append("\nnode");
		buf.append(i);
		buf.append("=");
		buf.append(node.toString());
	}
  	
  	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 RegularLine(this);
  		
	}catch(Exception e){
		throw new CloneNotSupportedException(e.getMessage());
	}
  }


   /**
    *   Returns the hashcode for this Object.
    * 
    *   @return hash code for this Point2D.
    *
    */ 	
  public int hashCode(){
	int code=0;
	
	//test if any node of this regular line is inside the rectangle.
	Iterator it	=m_nodeList.iterator();
	while (it!=null && it.hasNext()){
		JFPointNode node=(JFPointNode)it.next();
		code	^=node.hashCode();
	}
	
	return code;	       	
  }


   /**
    *   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 RegularLine))
            	return false;

      RegularLine	line	=(RegularLine)obj;
      
      return	m_nodeList.equals(line.m_nodeList);
  }




}

⌨️ 快捷键说明

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