⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 label.java

📁 用Java开发的、实现类似Visio功能的应用程序源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
   public Rect getBounds(){ 
   	//create a temporary bufferred image for text font usage.
	BufferedImage bufferedImage = new BufferedImage(100,100, BufferedImage.TYPE_INT_RGB);
        Graphics g = bufferedImage.createGraphics();
        
        //get bounds.
   	Rectangle2D bounds	=getBounds2D(g);
   	if (bounds==null)
   		return new Rect(0,0,0,0);
   	else{
   		double x	=m_labelPoint.getX() * getZoomScale();
   		double y	=m_labelPoint.getY() * getZoomScale();
   		return new Rect(x-bounds.getWidth()/2+bounds.getX(),y+bounds.getY(),bounds.getWidth(),bounds.getHeight());
   	}


   }

   /**
    *   Get bounds in a rectangle2D variable.
    * 
    *   @param  g  A graphic canvas.	
    *   @return A rectangle2D bounds.
    *
    */ 	
   private Rectangle2D getBounds2D(Graphics g){  
   	if (m_text==null || m_text.length()==0)
   		return null;
   	else{
   		Graphics2D g2	=(Graphics2D)g;
        	FontRenderContext frc = g2.getFontRenderContext();
        	return new TextLayout(m_text, getZoomedFont(), frc).getBounds();
        }
   }
   
      	
   /**
    *   Draw current label on graphic canvas.
    * 
    *   @param  g  A graphic canvas.	
    *
    */ 	
   protected void  drawLabel(Graphics g){
   	if (g==null)
   		return;

   	if (m_text==null || m_text.length()==0)
   		return;

	
	Graphics2D g2	=(Graphics2D)g;
	g2.setFont(getZoomedFont());

	JFPoint pnt	=getDrawPoint(g,false);

        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_OFF);
	
	g2.drawString(m_text,(float)pnt.getX(),(float)pnt.getY());
	
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);
	
   }
      
         
   /**
    *   Draw picked state of current object on graphic canvas.
    * 
    *   @param g A graphic canvas.
    *   @param  isXorMode If is in xor mode now.
    *
    */  	
  public void drawPicked(Graphics g,boolean isXorMode){
   	if (m_text==null || m_text.length()==0)
   		return;
        
  	double zoom	=getZoomScale();
  	
	Graphics2D g2	=(Graphics2D)g;
	g2.setFont(getZoomedFont());
	BasicStroke	s;
	if (isXorMode){
		s= new BasicStroke(1);
	}else{
	 	s= new BasicStroke(1,
                            BasicStroke.CAP_SQUARE,   
                            BasicStroke.JOIN_MITER,   
                            10.0f,                    
                            new float[] {2,4},  
                            0.0f);	
	}                        
	g2.setStroke(s);
	        
        Rectangle2D bounds	=getBounds2D(g);
        bounds.setFrame(bounds.getX(),bounds.getY(),bounds.getWidth()+2,bounds.getHeight()+2);

   	float x	=(float)((m_labelPoint.getX()* zoom-bounds.getWidth()/2));
   	float y	=(float)((m_labelPoint.getY())*zoom);

        g2.setPaint(Color.gray);
        AffineTransform xform = AffineTransform.getTranslateInstance(x-1,y-1);
        //bounds.setFrame(bounds.getX(), bounds.getY(), bounds.getWidth()*1, bounds.getHeight()*1);
        g2.draw(xform.createTransformedShape(bounds));
        
        g2.setStroke(new BasicStroke(1));
  }

   /**
    *   Draw current object on graphic canvas.
    * 
    *   @param  g  A graphic canvas.	
    *
    */ 	
   public void  draw(Graphics g){
   	if (g==null)
   		return;

	if (m_visible)	   	
   		drawLabel(g);
   }


   /**
    *   Test if current object intersects with a specified point.
    * 
    *   @param  g A graphics context necessary used to measure intersection.
    *   @param  pnt A JFPoint used to test intersection.
    *
    */ 	
   public boolean  intersects(Graphics g,JFPoint pnt){
   	if (!m_movable)
   		return false;
   		
	if (m_text==null || m_text.length()==0)
   		return false; 
	
	double zoom	=getZoomScale();
	pnt		=new JFPoint(pnt.getX() * zoom, pnt.getY() * zoom);
		
        Rectangle2D bounds  =getBounds2D(g); 
        
   	float x	=(float)(m_labelPoint.getX()* zoom - bounds.getWidth()/2 - bounds.getX());
   	float y	=(float)(m_labelPoint.getY()* zoom - bounds.getHeight());
   	Rect rect	=new Rect(x,y,
        			  bounds.getWidth(),
        			  bounds.getHeight());
	return rect.contains(pnt);        			 
   }


   /**
    *   Test if current object intersects with a specified point.
    * 
    *   @param  g A graphics context necessary used to measure intersection.
    *   @param  rect A rectangle used to test intersection.
    *
    */ 	
   public boolean  intersects(Graphics g,Rect rect){ 
   	if (!m_movable)
   		return false;

	if (m_text==null || m_text.length()==0)
   		return false; 

	double zoom	=getZoomScale();
	rect		=new Rect(rect.getX() * zoom, rect.getY() * zoom, rect.getWidth() * zoom, rect.getHeight() * zoom);
   	
        Rectangle2D bounds  =getBounds2D(g);
   	float x	=(float)(m_labelPoint.getX()* zoom - bounds.getWidth()/2 - bounds.getX());
   	float y	=(float)(m_labelPoint.getY()* zoom - bounds.getHeight());
	bounds.setFrame(x,y,bounds.getWidth(),bounds.getHeight());
  
        return bounds.intersects(rect.getX(),rect.getY(),rect.getWidth(),rect.getHeight()); 
   }
   
   

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


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

   	buf.append(super.toString());
   	buf.append(";xOffset=");	buf.append(m_labelPoint.getX());
   	buf.append(";yOffset=");	buf.append(m_labelPoint.getY());  
   	buf.append(";text=");		buf.append(m_text);
   	buf.append(";visible=");	buf.append(m_visible?"yes":"no");
   	buf.append(";movable=");	buf.append(m_movable?"yes":"no");
   	
   	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 Label();
  }
  
   /**
    *   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{
  		Label	aLabel =(Label) super.clone();
		aLabel.setValue(this);		
  		
  		return aLabel;
  		
	}catch(Exception e){
		throw new CloneNotSupportedException(e.getMessage());
	}
  }


   /**
    *   Returns the hashcode for this Object.
    * 
    *   @return hash code for this Label.
    *
    */ 	
  public int hashCode(){
  	long  x   =Double.doubleToLongBits(m_labelPoint.getX());
  	long  y   =Double.doubleToLongBits(m_labelPoint.getY());
  	
  	return 	super.hashCode() ^ 
  		(int)(x ^ (x >>> 32)) ^ 
  		(int)(y ^ (y >>> 32)) ^
  		m_text.hashCode()^
  		(m_visible?1:0)^
  		(m_movable?1:0)
  		;
  }


   /**
    *   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 Label 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 Label))
            	return false;
            	
      Label  aLabel= (Label)obj;
      return  (Double.doubleToLongBits(m_labelPoint.getX())==Double.doubleToLongBits(aLabel.m_labelPoint.getX())) &&
              (Double.doubleToLongBits(m_labelPoint.getY())==Double.doubleToLongBits(aLabel.m_labelPoint.getY())) &&
              m_text.equals(aLabel.m_text) &&
              m_visible==aLabel.m_visible &&
              m_movable==aLabel.m_movable 
              ;
  }
   

   /**
    *   Append necessary xml child for current element,
    *   this method will be called internally by toDOM.
    * 
    *   @param  element A XML element to append child xml labels
    *   @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);
    	element.addChild(new Element(XML_XOFFSET, m_labelPoint.getX()));
    	element.addChild(new Element(XML_YOFFSET, m_labelPoint.getY()));
    	element.addChild(new Element(XML_TEXT, CommonUtil.strToHex(m_text)));
    	element.addChild(new Element(XML_VISIBLE, m_visible));
    	element.addChild(new Element(XML_MOVABLE, m_movable));
  }


   /**
    *   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_labelPoint.setX(Element.getDoubleValue(element.getChild(XML_XOFFSET)));
      	m_labelPoint.setY(Element.getDoubleValue(element.getChild(XML_YOFFSET)));
      	m_text		=CommonUtil.hexToStr(Element.getStringValue(element.getChild(XML_TEXT)));
      	m_visible       =Element.getBooleanValue(element.getChild(XML_VISIBLE));
      	m_movable       =Element.getBooleanValue(element.getChild(XML_MOVABLE));
  }

   /**
    *   Save this label to a binary stream <br>
    * 
    *   @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);	
	stream.writeDouble(m_labelPoint.getX());
	stream.writeDouble(m_labelPoint.getY());
	stream.writeUTF(m_text);
	stream.writeInt(m_visible?1:0);	
	stream.writeInt(m_movable?1:0);	
  }


   /**
    *   Load label 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_labelPoint.setX(stream.readDouble());
	m_labelPoint.setY(stream.readDouble());
	m_text		=stream.readUTF();
	m_visible       =(stream.readInt()==1)?true:false;
	m_movable       =(stream.readInt()==1)?true:false;
  }

 	
}

⌨️ 快捷键说明

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