jfpolygon.java

来自「用Java开发的、实现类似Visio功能的应用程序源码」· Java 代码 · 共 385 行

JAVA
385
字号
/**
 *    $Id:JFPolygon.java $
 *
 *    Copyright 2004 ~ 2005  JingFei International Cooperation LTD. All rights reserved. *
 */
package com.jfimagine.jfgraph.shape.polygon;


import java.util.List;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.GeneralPath;


import com.jfimagine.jfdom.Document;
import com.jfimagine.jfdom.Element;


import com.jfimagine.jfgraph.shape.base.AbstractObject;
import com.jfimagine.jfgraph.shape.base.AbstractShape;
import com.jfimagine.jfgraph.shape.base.ShapeConst;
import com.jfimagine.jfgraph.shape.base.ObjectList;
import com.jfimagine.jfgraph.shape.base.JFVersion;

import com.jfimagine.jfgraph.shape.base.Node;
import com.jfimagine.jfgraph.shape.decorate.LineFormat;
import com.jfimagine.jfgraph.shape.decorate.FillFormat;
import com.jfimagine.jfgraph.shape.decorate.Arrow;

import com.jfimagine.jfgraph.shape.line.JFLine;

import com.jfimagine.jfgraph.geom.JFPoint;
import com.jfimagine.jfgraph.geom.PolyLine;
import com.jfimagine.jfgraph.geom.Rect;
import com.jfimagine.jfgraph.geom.GeomConst;
 
 
 /**
 * JFPolygon class.  
 * A line should be a two points one(one for beginning and the other for end), or n-points polyline.
 *
 * @author     CookieMaker    
 *
 * @version $Revision: 1.00 $
 */  
 public class JFPolygon extends JFLine{


   /**
    *   A XML string tag represents a polygon
    */
   public  static final String	 XML_POLYGON			="Polygon";

   /**
    *   fill format of current fill.
    */ 
   protected FillFormat  m_fillFormat = new FillFormat();


   /**
    *   Constructor for Polygon
    */
   public JFPolygon(){
   	setObjectType(ShapeConst.SHAPETYPE_POLYGON);
   	setXMLTag(XML_POLYGON);
   	m_polyLine	=new PolyLine();
   }	

   /**
    *   Constructor for Polygon
    *
    *   @param xpoints  an array of x coordinates
    *   @param ypoints  an array of y coordinates
    *   @param npoints  the total number of points in the Polygon
    *
    */ 	
   public JFPolygon(int[] xpoints, int[] ypoints, int npoints){   	
   	setObjectType(ShapeConst.SHAPETYPE_POLYGON);
   	setXMLTag(XML_POLYGON);
   	m_polyLine	=new PolyLine(xpoints,ypoints,npoints);
   	m_polyLine.closePolygon();
   	finishDrawing();
   }  


   /**
    *   Ask if this object is an open shape,e.g. line,curve,arc,etc.
    *
    *   @return true if open,false closed.
    *
    */ 	
   public boolean isOpenShape(){
   	return false;
   }

   /**
    *   Get the arrow format of current object.
    * 
    *   @return  The arrow format.
    *
    */ 	
   public Arrow getArrow(){
   	return null;
   }

   /**
    *   Set the arrow format of current object.
    *
    *   @param arrow  A new arrow format object.
    *
    */ 	
   public void setArrow(Arrow arrow){
  }

   /**
    *   Get the fill format of current line.
    * 
    *   @return  The fill format.
    *
    */ 	
   public FillFormat getFillFormat(){
   	return m_fillFormat;
   }

   /**
    *   Set the fill format of current line.
    *
    *   @param fillFormat A new fill format.
    *
    */ 	
   public void setFillFormat(FillFormat fillFormat){
   	m_fillFormat.setValue(fillFormat);
   }
   
   /**
    *   If a polyline has been drew.
    *   @return True when complete, false otherwise.
    *
    */ 	
   public boolean ifCompleteDrawing(){
   	//two nodes added will decide a polyline.
   	return (m_polyLine.getNodeCount()>=3);
   }


   /**
    *   Finish drawing polygon
    *
    */ 	
   public boolean finishDrawing(){ 
   	if (!m_polyLine.closePolygon())
   		return false;
   	else{
   		return super.finishDrawing();
   	}
   }


   /**
    *   Draw current object on graphic canvas.
    * 
    *   @param  g  A graphic canvas.	
    *   @param  isXorMode If is in xor mode now.
    *
    */ 	
   public void  draw(Graphics g,boolean isXorMode){
   	
   	if (g==null)
   		return;

   	//if user hide this shape, we'll draw an 'invisible' bounds here.	
   	if (isInvisible()){
   		drawInvisibleBounds(g,isXorMode);
   		return;
   	}	

        if (!isXorMode)
		setTransparencyComposite(g);
   	
   	JFPoint node;
  	double zoom	=getZoomScale();
   	
   	//pick up each nodes pair, and draw each line segment between a nodes pair.
        GeneralPath line= new GeneralPath(GeneralPath.WIND_EVEN_ODD); 
   	for (int i=0; i<m_polyLine.getNodeCount(); i++){
		node	=m_polyLine.getNode(i);
		float x	=(float)(node.getX() * zoom);
		float y	=(float)(node.getY() * zoom);
		
		if (i==0){
			line.moveTo(x,y);
		}else{
			line.lineTo(x,y);
		}
   	}
   	if (m_polyLine.isPolygon())
   		line.closePath();
   	
   	if (!isXorMode){
   		Rect rect	=getBounds();
   		rect.setValue(rect.getX() * zoom, rect.getY() * zoom, rect.getWidth() * zoom, rect.getHeight() * zoom);
   		m_fillFormat.draw(g,line,rect);
   		m_lineFormat.draw(g,line);
   	}else
   		((Graphics2D)g).draw(line);

        if (!isXorMode)
		restoreTransparencyComposite(g);

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



   /**
    *   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<fillFormat>");
	buf.append(m_fillFormat.toString());

   	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 JFPolygon();
  }
  
  
   /**
    *   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;
  		}
  		
  		JFPolygon poly =(JFPolygon) obj;
  		poly.m_fillFormat.setValue(m_fillFormat);
  		
  		return poly;
  		
	}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_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 JFPolygon))
            return false;

      JFPolygon  poly= (JFPolygon)obj;
      
      return  	(poly.m_fillFormat.equals(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);
    		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);

      m_fillFormat.fromDOM(element.getChild(m_fillFormat.getXMLTag()),version);
      m_polyLine.closePolygon();
  }

 
   /**
    *   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_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);
	
	m_fillFormat.loadFromStream(stream,false,version);//don't skip head here
	m_polyLine.closePolygon();
  }



 }

⌨️ 快捷键说明

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