jfquadrant.java

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

JAVA
855
字号
	//set the default graphics stroke and color.
	if (!isXorMode)
		m_lineFormat.initGraphics(g);        

        if (!isXorMode)
		restoreTransparencyComposite(g);
   	
	if (!isXorMode){   	
   		drawLabel(g); 	
        }

   }


   /**
    *   Test if current object intersects with a specified point.
    * 
    *   @param  pnt A JFPoint used to test intersection.
    *
    */ 	
   public boolean  intersects(JFPoint pnt){ 
	
   	
   	//we decide the 'move method' here while checking intersection.

   	if (m_center.contains(pnt,GeomConst.PICK_OFFSET)){
   		m_moveMethod	=MOVEMETHOD_CENTER;
   		return true;
   	}
   	
   	double centerx	=m_center.getX();
   	double centery	=m_center.getY();
   	
   	//horizontal line
   	LineSeg line	=new LineSeg(0,centery,m_width,centery);
   	if (line.contains(pnt,GeomConst.PICK_OFFSET)){
   		m_moveMethod	=MOVEMETHOD_HORIZONTALLINE;
   		return true;
   	}

	//vertical line   	
   	line.setValue(centerx,0,centerx,m_height);
   	if (line.contains(pnt,GeomConst.PICK_OFFSET)){
   		m_moveMethod	=MOVEMETHOD_VERTICALLINE;
   		return true;
   	}

	m_moveMethod	=MOVEMETHOD_NONE;   		
   	return false;
   }

 
 	
   /**
    *   Test if current object intersects with a specified rectangle.
    * 
    *   @param  rect A Rect used to test intersection.
    *
    */ 	
   public boolean  intersects(Rect rect){
   	return false;
   }


   /**
    *   Scale current object 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){
   	m_center.scaleBy(basePoint,scale);
	initNodes();
   }

   /**
    *   Scale current object 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){
   	m_center.scaleBy(basePoint.getX(),basePoint.getY(),xScale,yScale);
	initNodes();
  }
  
  

   /**
    *   Move current object by an x and y offset.
    * 
    *   @param  x,&nbsp;y Moving offsets.
    *
    */ 	
   public void  moveBy(double x, double y){

	//we move the horizontal line, vertical line or both according to the moveMethod
	switch (m_moveMethod){
		case MOVEMETHOD_HORIZONTALLINE:
			m_center.moveBy(0,y);
		   	m_label.moveBy(0,y);
		   	break;

		case MOVEMETHOD_VERTICALLINE:
			m_center.moveBy(x,0);
		   	m_label.moveBy(x,0);
		   	break;

		case MOVEMETHOD_CENTER:
			m_center.moveBy(x,y);
		   	m_label.moveBy(x,y);
		   	break;
	}
   	initNodes();
   }


   /**
    *   Rotate this object by an angle theta.
    *
    *   @param theta A rotate angle.
    *
    */ 	
   public void rotateBy(double theta){
   	//do nothing.
   }

   /**
    *   Rotate current object by a specified point and an angle theta.
    *
    *   @param baseX,&nbsp;baseY A rotate center coordinates.
    *
    *   @param theta A rotate angle.
    *
    */ 	
   public void rotateBy(double baseX,double baseY, double theta){  
   	//do nothing.
   }


   /**
    *   Mirror this object by a central x coordinate of this object. We make a left-right flip here.
    */ 	
   public void mirrorBy(){
   	//do nothing.
   }

   /**
    *   Mirror this object by a x coordinate. We make a left-right mirror here.
    *
    *   @param baseX  A mirror base x coordinate.
    *
    */ 	
   public void mirrorBy(double baseX){
   	//do nothing.
   }


   /**
    *   Reverse this object by a central y coordinate of this object. We make a up-down flip here.
    */ 	
   public void flipBy(){
   	//do nothing.
   }

   /**
    *   Reverse this object by a y coordinate. We make a up-down flip here.
    *
    *   @param baseY  A flip base y coordinate.
    *
    */ 	
   public void flipBy(double baseY){
   	//do nothing.
   }


   /**
    *   Move/adjust a node of current object.
    * 
    *   @param  node Currently moving node.
    *
    *   @param  x,&nbsp;y new position of this node.
    *
    *   @param g current drawing canvas.
    *
    */ 	
   public void  moveNode(Node node, double x, double y,Graphics g){
	if (node!=null){
		m_moveMethod	=MOVEMETHOD_CENTER;
		moveBy(x-node.getXOffset(),y-node.getYOffset());
		draw(g,true);
	}
   }

   /**
    *   Finish drawing object.
    *
    */ 	
   public boolean finishDrawing(){
   	initNodes();
	initLabel();  	
   	return true;
   }


   /**
    *   Convert this object to String <br>
    * 
    *   @return  An string represents the content of the object
    *
    */ 	
   public String toString(){
   	StringBuffer buf=new StringBuffer();
	
	buf.append(super.toString());
   	buf.append("\n<lineFormat>");
   	buf.append(m_lineFormat.toString());
   	buf.append("\ncenter=");
   	buf.append(m_center);
	buf.append(",width=");
	buf.append(m_width);
	buf.append(",height=");
	buf.append(m_height);
   	return buf.toString();
   }



   /**
    *   Creates a new AbstractObject of the same class and with the same contents as this object.
    *   This method implements the method defined in AbstractObject.
    * 
    *   @return  A clone of this class.
    *
    */ 	
  protected AbstractObject cloneMe() throws CloneNotSupportedException{
  	return new JFQuadrant();
  }
  
  
   /**
    *   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{
  		Object obj =super.clone();
  		if (obj==null){
  			return null;
  		}
  		
  		JFQuadrant quadrant =(JFQuadrant) obj;
  		quadrant.m_center.setValue(m_center);
		quadrant.m_width	=m_width;
		quadrant.m_height	=m_height;
  		quadrant.initNodes();
  		quadrant.m_lineFormat.setValue(m_lineFormat); 
  		
  		return quadrant;
  		
	}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_center.hashCode() ^ 
  		(int)m_width ^ 
  		(int)m_height ^
  		m_lineFormat.hashCode();
  }


   /**
    *   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 (!super.equals(obj))
             return false;
             		
      if (obj == this)
             return true;
      if (!(obj instanceof JFQuadrant))
            return false;

      JFQuadrant  quadrant= (JFQuadrant)obj;
      
      return  	(quadrant.m_center.equals(m_center))&&
      		((int)quadrant.m_width==(int)m_width) &&
      		((int)quadrant.m_height==(int)m_height) &&
        	(quadrant.m_lineFormat.equals(m_lineFormat));
  }


   /**
    *   Append necessary xml child for current element,
    *   this method will be called internally by toDOM.
    * 
    *   @param  element A XML element to append child xml nodes
    *   @param version A file version notification so this object can obey the rules to save data.
    *
    */ 	
  protected void appendChildToDOM(Element element,JFVersion version){
  	if (element==null)
  		return;

  	super.appendChildToDOM(element,version);

    	m_lineFormat.toDOM(element,version);

    	element.addChild(new Element(XML_CENTERX, m_center.getX()));
    	element.addChild(new Element(XML_CENTERY, m_center.getY()));
    	element.addChild(new Element(XML_WIDTH, m_width));
    	element.addChild(new Element(XML_HEIGHT, m_height));
  }


   /**
    *   Extract needed xml child from current element,
    *   this method will be called internally by fromDOM.
    * 
    *   @param  element An element used to extract needed xml child
    *   @param version A file version notification so this object can obey the rules to fetch data.
    *
    */ 	
  protected void extractChildFromDOM(Element element,JFVersion version){

      	if (element==null)
  		return;
  
      	super.extractChildFromDOM(element,version);

      	m_lineFormat.fromDOM(element.getChild(m_lineFormat.getXMLTag()),version);
	
      	double x=0,y=0;
      		      	  
      	x	 	=Element.getDoubleValue(element.getChild(XML_CENTERX));
      	y	 	=Element.getDoubleValue(element.getChild(XML_CENTERY));
      	m_center.setValue(x,y);
      	
      	m_width	 	=Element.getDoubleValue(element.getChild(XML_WIDTH));
      	m_height 	=Element.getDoubleValue(element.getChild(XML_HEIGHT));
     	
      	initNodes();
  }

 
   /**
    *   Save this object to a binary stream 
    * 
    *   @param stream An binary output stream
    *
    *   @param version A file version notification so this object can obey the rules to save data.
    *   @exception  java.io.IOException
    *
    */ 	
  public void saveToStream(com.jfimagine.utils.io.JFWriter stream,JFVersion version) throws IOException{

	super.saveToStream(stream,version);
	    
	m_lineFormat.saveToStream(stream,version);
	    
	stream.writeDouble(m_center.getX());
	stream.writeDouble(m_center.getY());
	    
	stream.writeDouble(m_width);
	stream.writeDouble(m_height);   		    
  }

   /**
    *   Load object data from a binary stream <br>
    * 
    *   @param stream An binary input stream
    *
    *   @param skipHead Skip head 'TYPE' check, an shape object should always 
    *   has its own shape-type stored, if this shape-type has already been readed,
    *   this loadFromStream should/could not read the type anymore.
    *
    *   @param version A file version notification so this object can obey the rules to fetch data.
    *   @exception  java.io.IOException
    *
    */ 	
  public void loadFromStream(com.jfimagine.utils.io.JFReader stream,boolean skipHead,JFVersion version) throws IOException{
	super.loadFromStream(stream,skipHead,version);
	
	m_lineFormat.loadFromStream(stream,false,version);//don't skip head here
	
	double x	=stream.readDouble();
	double y	=stream.readDouble();
	m_center.setValue(x,y);
	
	m_width		=stream.readDouble();
	m_height	=stream.readDouble();

      	initNodes();
  }




 }

⌨️ 快捷键说明

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