polynode.java

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

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

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;


import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

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

import com.jfimagine.jfgraph.shape.base.AbstractObject;
import com.jfimagine.jfgraph.shape.base.ShapeConst;
import com.jfimagine.jfgraph.shape.base.Node;
import com.jfimagine.jfgraph.shape.base.JFVersion;

import com.jfimagine.jfgraph.geom.GeomConst;
import com.jfimagine.jfgraph.geom.JFPoint;
import com.jfimagine.jfgraph.geom.JFPointNode;
import com.jfimagine.jfgraph.geom.Rect;

/**
 * Regular Node Class. A regular node is used by Regular line. to construct, adjust or transform a regularLine.
 *
 * @author     CookieMaker    
 *
 * @version $Revision: 1.00 $
 */  
 public class PolyNode extends Node {

   /**
    *   A XML string tag represents a regularNode
    */
   public  static final String	 XML_POLYNODE	="PolyNode";

   /**
    *   A XML string tag represents the type of PolyNode
    */
   public  static final String	 XML_NODETYPE		="NodeType";

   /**
    *  Color for drawing end node, middle node or sub middle node.
    */	
   public static final Color	COLOR_ENDNODE	=Color.yellow; 
   public static final Color	COLOR_MIDNODE	=new Color(200,200,200);
   public static final Color	COLOR_SUBMIDNODE=new Color(230,230,230);


   /**
    *   Node type of current node.
    *   JFRegularLine, a node type will be end, middle or submiddle one.
    */
   private	int m_nodeType		=0;

	

   /**
    *   Constructor for PolyNode
    */
   public PolyNode(){
   	setObjectType(ShapeConst.SHAPETYPE_POLYNODE);
   	setXMLTag(XML_POLYNODE);
   }	

   /**
    *   Constructor for PolyNode
    */
   public PolyNode(double x,double y, int nodeType){
   	setObjectType(ShapeConst.SHAPETYPE_POLYNODE);
   	setXMLTag(XML_POLYNODE);
   	m_nodePoint.setValue(x,y);
   	m_nodeType	=nodeType;
   }

   
   /**
    *   Get the node type of current node.
    *
    *   @return  The node type.
    *
    */ 	
   public int getNodeType(){
   	return m_nodeType;
   }

   /**
    *   Set the node type of current node.
    *
    *   @param  A new node type of current node.
    *
    */ 	
   public void setNodeType(int nodeType){
   	m_nodeType	=nodeType;
   }

   /**
    *   Draw current object on graphic canvas.
    * 
    *   @param  g  A graphic canvas.	
    *   @param ifRotate If user's operation or other actions force objects to be rotated.
    *
    */ 	
   public void  draw(Graphics g, boolean ifRotate){
   	if (g==null)
   		return;

   	if (ifRotate){
   		switch (m_nodeType){
   			case JFPointNode.NODETYPE_END:
				drawNodeCircle(g,Color.lightGray);
   				drawRotateSign(g);
   				break;
   		}

	}else{
   		switch (m_nodeType){
   			case JFPointNode.NODETYPE_END:
				drawNodeRect(g,COLOR_ENDNODE,Color.black);   		
   				break;
   			case JFPointNode.NODETYPE_MIDDLE:
				drawNodeRect(g,COLOR_MIDNODE,Color.black);   		
   				break;
   			case JFPointNode.NODETYPE_SUBMIDDLE:
				drawNodeRect(g,COLOR_SUBMIDNODE,Color.black);   		
   				break;
   		}
   	}
   }


   /**
    *   Ask if this node is rotatable.
    *
    *   @return true if rotatable, false otherwise.
    *
    */ 	
   public boolean isRotatable(){
   	return (m_nodeType==JFPointNode.NODETYPE_END);
   }


   /**
    *  Draw a rotation arrow on current node.
    */	
   public  void drawRotateSign(Graphics g){
   	//only allowed NODETYPE_END node to be drawing rotation arrow.
  	if (isRotatable())
  		super.drawRotateSign(g);
   }


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

   	buf.append(super.toString());
   	buf.append(";nodeType=");	buf.append(m_nodeType);
   	
   	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 PolyNode();
  }
  
   /**
    *   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{
  		PolyNode	node =(PolyNode) super.clone();
		node.m_nodeType	=this.m_nodeType;
  		return node;
  		
	}catch(Exception e){
		throw new CloneNotSupportedException(e.getMessage());
	}
  }


   /**
    *   Returns the hashcode for this Object.
    * 
    *   @return hash code for this Node.
    *
    */ 	
  public int hashCode(){
  	return super.hashCode() ^ m_nodeType;
  }


   /**
    *   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 Node 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 PolyNode))
            	return false;
            	
      PolyNode  node= (PolyNode)obj;
      return node.m_nodeType==m_nodeType;
  }
   

   /**
    *   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);
    		element.addChild(new Element(XML_NODETYPE, m_nodeType));
  }


   /**
    *   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_nodeType	=Element.getIntValue(element.getChild(XML_NODETYPE));
  }

   /**
    *   Save this node 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.writeInt(m_nodeType);
  }


   /**
    *   Load node 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_nodeType	=stream.readInt();
  }


 	
 }

⌨️ 快捷键说明

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