abstractrectangle.java

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

JAVA
1,128
字号
    */ 	
   public void scaleBy(JFPoint basePoint, JFPoint refPoint1, JFPoint refPoint2, double scale){
   	startMoveNode(null);
	JFPoint center	=m_rect.getCenter();
	
	m_rect.scaleBy(basePoint,refPoint1,refPoint2,scale);   	
	m_portList.scaleBy(center,basePoint,refPoint1,refPoint2,scale);
	initNodes();
   	finishMoveNode(null,0,0,null);
   }

   /**
    *   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){
   	startMoveNode(null);
	m_rect.scaleBy(basePoint,xScale,yScale);
	m_portList.scaleBy(basePoint,xScale,yScale);
	initNodes();
   	finishMoveNode(null,0,0,null);
  }

 
  

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

   /**
    *   Rotate this rectangle by an angle theta.
    *
    *   @param theta A rotate angle.
    *
    */ 	
   public void rotateBy(double theta){
	JFPoint center		=m_rect.getCenter();
	rotateBy(center.getX(),center.getY(),theta);
   }

   /**
    *   Rotate this line 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){
   	startMoveLabel();
   	m_rect.rotateBy(baseX,baseY,theta);
   	initNodes();
   	m_portList.rotateBy(baseX,baseY,theta);  
	finishMoveLabel();	
   }


   /**
    *   Mirror this rectangle by a central x coordinate of this rectangle. We make a left-right flip here.
    */ 	
   public void mirrorBy(){
   	JFPoint center	=m_rect.getCenter();
   	mirrorBy(center.getX());
   }


   /**
    *   Mirror this rectangle by a x coordinate. We make a left-right mirror here.
    *
    *   @param baseX  A mirror base x coordinate.
    *
    */ 	
   public void mirrorBy(double baseX){
   	startMoveLabel();
   	m_rect.mirrorBy(baseX);
   	initNodes();
   	m_portList.mirrorBy(baseX);
   	finishMoveLabel();
   }


   /**
    *   Reverse this rectangle by a central y coordinate of this rectangle. We make a up-down flip here.
    */ 	
   public void flipBy(){
   	JFPoint center	=m_rect.getCenter();
   	flipBy(center.getY());
   }


   /**
    *   Reverse this rectangle by a y coordinate. We make a up-down flip here.
    *
    *   @param baseY  A flip base y coordinate.
    *
    */ 	
   public void flipBy(double baseY){
   	startMoveLabel();
   	m_rect.flipBy(baseY);
   	initNodes();
   	m_portList.flipBy(baseY);
   	finishMoveLabel();
   }

  
   /**
    *   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(m_rect.toString());      
	buf.append(m_lineFormat.toString());
	buf.append(m_fillFormat.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{
  		Object obj =super.clone();
  		if (obj==null){
  			return null;
  		}
  		
  		AbstractRectangle rect=(AbstractRectangle) obj;
  		rect.m_rect.setValue(m_rect);
  		rect.m_lineFormat.setValue(m_lineFormat);
  		rect.m_fillFormat.setValue(m_fillFormat);

  		return rect;
  		
	}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_rect.hashCode() ^ m_lineFormat.hashCode()^ m_fillFormat.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 AbstractRectangle))
            return false;

      AbstractRectangle  rect= (AbstractRectangle)obj;
      
      return  	m_rect.equals(rect.m_rect) &&
      		m_lineFormat.equals(rect.m_lineFormat) &&
      		m_fillFormat.equals(rect.m_fillFormat);
  }


   /**
    *   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);
  		
  		//append m_rect to dom.
  		JFPoint pnt;

  		pnt	=m_rect.getVertex(Rect.VERTEXTYPE_LEFTTOP);
    		element.addChild(new Element(XML_LEFTTOPX, pnt.getX()));
    		element.addChild(new Element(XML_LEFTTOPY, pnt.getY()));

    		
  		pnt	=m_rect.getVertex(Rect.VERTEXTYPE_RIGHTTOP);
    		element.addChild(new Element(XML_RIGHTTOPX, pnt.getX()));
    		element.addChild(new Element(XML_RIGHTTOPY, pnt.getY()));


  		pnt	=m_rect.getVertex(Rect.VERTEXTYPE_LEFTBOTTOM);
    		element.addChild(new Element(XML_LEFTBOTTOMX, pnt.getX()));
    		element.addChild(new Element(XML_LEFTBOTTOMY, pnt.getY()));



  		pnt	=m_rect.getVertex(Rect.VERTEXTYPE_RIGHTBOTTOM);
    		element.addChild(new Element(XML_RIGHTBOTTOMX, pnt.getX()));
    		element.addChild(new Element(XML_RIGHTBOTTOMY, pnt.getY()));

    		
    		//append m_lineformat to dom.
    		m_lineFormat.toDOM(element,version);

    		m_fillFormat.toDOM(element,version);

  }


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

      
      	double x=0,y=0;
      		      	  
      	x	 	=Element.getDoubleValue(element.getChild( XML_LEFTTOPX));
      	y	 	=Element.getDoubleValue(element.getChild( XML_LEFTTOPY));
      	m_rect.getVertex(Rect.VERTEXTYPE_LEFTTOP).setValue(x,y);

      
      	x	 	=Element.getDoubleValue(element.getChild( XML_RIGHTTOPX));
      	y	 	=Element.getDoubleValue(element.getChild( XML_RIGHTTOPY));
      	m_rect.getVertex(Rect.VERTEXTYPE_RIGHTTOP).setValue(x,y);


      	x	 	=Element.getDoubleValue(element.getChild( XML_LEFTBOTTOMX));
      	y	 	=Element.getDoubleValue(element.getChild( XML_LEFTBOTTOMY));
      	m_rect.getVertex(Rect.VERTEXTYPE_LEFTBOTTOM).setValue(x,y);


      	x	 	=Element.getDoubleValue(element.getChild( XML_RIGHTBOTTOMX));
      	y	 	=Element.getDoubleValue(element.getChild( XML_RIGHTBOTTOMY));
      	m_rect.getVertex(Rect.VERTEXTYPE_RIGHTBOTTOM).setValue(x,y);


      	initNodes();

        
        m_lineFormat.fromDOM(element.getChild(m_lineFormat.getXMLTag()),version);

        m_fillFormat.fromDOM(element.getChild(m_fillFormat.getXMLTag()),version);

  }

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

	 //append m_rect to stream			
	 JFPoint pnt;	    	

  	 pnt	=m_rect.getVertex(Rect.VERTEXTYPE_LEFTTOP);
	 stream.writeDouble(pnt.getX());
	 stream.writeDouble(pnt.getY());
	 
  	 pnt	=m_rect.getVertex(Rect.VERTEXTYPE_RIGHTTOP);
	 stream.writeDouble(pnt.getX());
	 stream.writeDouble(pnt.getY());

  	 pnt	=m_rect.getVertex(Rect.VERTEXTYPE_LEFTBOTTOM);
	 stream.writeDouble(pnt.getX());
	 stream.writeDouble(pnt.getY());

  	 pnt	=m_rect.getVertex(Rect.VERTEXTYPE_RIGHTBOTTOM);
	 stream.writeDouble(pnt.getX());
	 stream.writeDouble(pnt.getY());

	 //append m_lineFormat to stream.
	 m_lineFormat.saveToStream(stream,version);
	 m_fillFormat.saveToStream(stream,version);

  }

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

	double x=0,y=0;
	    	
	x	=stream.readDouble();
	y	=stream.readDouble();
      	m_rect.getVertex(Rect.VERTEXTYPE_LEFTTOP).setValue(x,y);

	x	=stream.readDouble();
	y	=stream.readDouble();
      	m_rect.getVertex(Rect.VERTEXTYPE_RIGHTTOP).setValue(x,y);

	x	=stream.readDouble();
	y	=stream.readDouble();
      	m_rect.getVertex(Rect.VERTEXTYPE_LEFTBOTTOM).setValue(x,y);

	x	=stream.readDouble();
	y	=stream.readDouble();
      	m_rect.getVertex(Rect.VERTEXTYPE_RIGHTBOTTOM).setValue(x,y);
      	
      	initNodes();

      	m_lineFormat.loadFromStream(stream,false,version);
      	m_fillFormat.loadFromStream(stream,false,version);
     	
     	
  }



 }

⌨️ 快捷键说明

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