jfdiamond.java

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

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

import java.awt.Graphics;

import com.jfimagine.jfgraph.shape.rectangle.AbstractRectangle;
import com.jfimagine.jfgraph.shape.base.Node;
import com.jfimagine.jfgraph.shape.base.Port;
import com.jfimagine.jfgraph.shape.base.AbstractObject;
import com.jfimagine.jfgraph.shape.base.ShapeConst;

import com.jfimagine.jfgraph.geom.JFPoint;
import com.jfimagine.jfgraph.geom.Rect;
import com.jfimagine.jfgraph.geom.Diamond;
 
 
 /**
 * JFDiamond class.  
 * A diamond class used to represents an diamond shape.
 *
 * @author     CookieMaker    
 *
 * @version $Revision: 1.00 $
 */  
 public class JFDiamond extends AbstractRectangle{

   /**
    *   A XML string tag represents a diamond.
    */
   public  static final String	 XML_DIAMOND		="Diamond";

   /**
    *   Constructor for diamond
    */
   public JFDiamond(){
   	m_rect	=new Diamond();
   	setObjectType(ShapeConst.SHAPETYPE_RECT_DIAMOND);
   	setXMLTag(XML_DIAMOND);
   }	
     
   /**
    *   Constructor for diamond.
    *
    *   @param x  X coordiate.
    *
    *   @param y  Y coordiate.
    *
    *   @param w  Width of this rectangle.
    *
    *   @param h  Height of this rectangle.
    *
    */ 	
   public JFDiamond(double x, double y, double w, double h){
   	setObjectType(ShapeConst.SHAPETYPE_RECT_DIAMOND);
   	setXMLTag(XML_DIAMOND);

   	Diamond diamond		=new Diamond();
   	diamond.setValue(x,y,w,h);
   	diamond.initDiamond();
   	
   	m_rect	=diamond;
   	finishDrawing();
   }

   	

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


   /**
    *   Add a new node for current node list.
    *   here this method will always be called by DrawState class or any drawing class.
    * 
    *   @param x, y Coordinates of a new node.
    *
    */ 	
   public void addNode(double x, double y){
	super.addNode(x,y);
	
	if (m_nodeAdded>1){
   		//left-top point and right-bottom point:
   		JFPoint pnt1	=m_firstNode;
   		JFPoint	pnt2	=new JFPoint(x,y);
		    	
   		m_rect.setVertex(Rect.VERTEXTYPE_LEFTTOP,pnt1.getX(),pnt1.getY());
   		m_rect.setVertex(Rect.VERTEXTYPE_RIGHTTOP,x,pnt1.getY());
   		m_rect.setVertex(Rect.VERTEXTYPE_LEFTBOTTOM,pnt1.getX(),y);
   		m_rect.setVertex(Rect.VERTEXTYPE_RIGHTBOTTOM,x,y);

   		initNodes();
   	}
   }  

   /**
    *   Move/adjust a node of current object.
    * 
    *   @param  node Currently moving node.
    *
    *   @param  x, y Moving offsets.
    *
    *   @param g current drawing canvas.
    *
    */ 	
   public void  moveNode(Node node, double x, double y,Graphics g){

   	super.moveNode(node,x,y,g);

	if (node!=null){
		if (node.getXOffset()!=x || node.getYOffset()!=y){
			//move node and draw a new dragging shape.
			int vertexType	=getVertexTypeByNode(node);
   			m_rect.moveVertex(vertexType,x,y,Rect.VERTEXMOVETYPE_RECTANGLE);

   			//init shape nodes.
			initNodes();
		   		
   			//draw current moving node and its relational lines
   			draw(g,true);
   		}
	}
   }

   /**
    *   Init all ports of this diamond
    *   An object will always has its one or more stable ports and some customized ports,
    *   for dimond, it will has four stable ports as below,
    *   port1 at middle left,
    *   port2 at middle top,
    *   port3 at middle right,
    *   port4 at middle bottom,
    */ 	
   protected void initPorts(){

   	if (m_portList.size()==0){
   		try{
			Rect rect	=((Diamond)m_rect).getDiamond();

		
			JFPoint left		=rect.getVertex(Rect.VERTEXTYPE_LEFTBOTTOM);
			JFPoint top		=rect.getVertex(Rect.VERTEXTYPE_LEFTTOP);
			JFPoint right		=rect.getVertex(Rect.VERTEXTYPE_RIGHTTOP);
			JFPoint bottom		=rect.getVertex(Rect.VERTEXTYPE_RIGHTBOTTOM);

  			
   			Port port;
			
			//top left
   			port 	=new Port();
   			port.setFirstPoint(left);
   			port.setSecondPoint(top);
   			port.setPortPoint(left.midPoint(top));
   			port.setParent(this);
   			m_portList.add(port);
   			
   			//top right
   			port 	=new Port();
   			port.setFirstPoint(top);
   			port.setSecondPoint(right);
   			port.setPortPoint(top.midPoint(right));
   			port.setParent(this);
   			m_portList.add(port);

			//bottom left
   			port 	=new Port();
   			port.setFirstPoint(bottom);
   			port.setSecondPoint(left);
   			port.setPortPoint(bottom.midPoint(left));
   			port.setParent(this);
   			m_portList.add(port);

			//bottom right
   			port 	=new Port();
   			port.setFirstPoint(right);
   			port.setSecondPoint(bottom);
   			port.setPortPoint(right.midPoint(bottom));
   			port.setParent(this);
   			m_portList.add(port);
   			
   		}catch(Exception e){
   		}
   	}  
	
	m_portList.setZoomScale(getZoomScale());
  	
   }



   /**
    *   Start move a node.
    * 
    *   @param  node The node will be moved.
    *
    */ 	
   public void  startMoveNode(Node node){
   	m_originalRect	=new Diamond((Diamond)m_rect);
   	startMoveLabel();
   }


   /**
    *   finish move/adjust a node of current object.
    * 
    *   @param  node Currently moving node.
    *
    *   @param  x, y Moving offsets.
    *
    *   @param g current drawing canvas.
    *
    */ 	
   public void  finishMoveNode(Node node, double x, double y,Graphics g){
	finishMoveLabel();
	
	Rect originalRect	=((Diamond)m_originalRect).getDiamond();	
	Rect rect		=((Diamond)m_rect).getDiamond();	


	m_portList.movePort(originalRect.getVertex(Rect.VERTEXTYPE_LEFTTOP),originalRect.getVertex(Rect.VERTEXTYPE_RIGHTTOP),
			    rect.getVertex(Rect.VERTEXTYPE_LEFTTOP),rect.getVertex(Rect.VERTEXTYPE_RIGHTTOP));

	m_portList.movePort(originalRect.getVertex(Rect.VERTEXTYPE_RIGHTTOP),originalRect.getVertex(Rect.VERTEXTYPE_RIGHTBOTTOM),
			    rect.getVertex(Rect.VERTEXTYPE_RIGHTTOP),rect.getVertex(Rect.VERTEXTYPE_RIGHTBOTTOM));

	m_portList.movePort(originalRect.getVertex(Rect.VERTEXTYPE_RIGHTBOTTOM),originalRect.getVertex(Rect.VERTEXTYPE_LEFTBOTTOM),
			    rect.getVertex(Rect.VERTEXTYPE_RIGHTBOTTOM),rect.getVertex(Rect.VERTEXTYPE_LEFTBOTTOM));

	m_portList.movePort(originalRect.getVertex(Rect.VERTEXTYPE_LEFTBOTTOM),originalRect.getVertex(Rect.VERTEXTYPE_LEFTTOP),
			    rect.getVertex(Rect.VERTEXTYPE_LEFTBOTTOM),rect.getVertex(Rect.VERTEXTYPE_LEFTTOP));
   }


     
   
}

⌨️ 快捷键说明

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