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

📄 jfgroup.java

📁 用Java开发的、实现类似Visio功能的应用程序源码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    *   @return The bounds rectangle of current rectangle.
    *
    */ 	
   public Rect getBounds(){
   	return m_rect.getBounds();
   }

   /**
    *   Get the maximum bounds of all objects.
    *
    *   @return The bounds group of current shape.
    *
    */ 	
   private Rect getBoundsOfObjects(){
   
   	double left	=GeomConst.LARGE_VALUE;
   	double top	=GeomConst.LARGE_VALUE;
   	double right	=-GeomConst.LARGE_VALUE;
   	double bottom	=-GeomConst.LARGE_VALUE;
   	
   	
   	Iterator it =m_objList.getList().iterator();
   	while (it!=null && it.hasNext()){
   		AbstractShape	aShape	=(AbstractShape)it.next();
   		Rect rect	=aShape.getBounds();
   		double x	=rect.getX();
   		double y	=rect.getY();
   		double w	=rect.getWidth();
   		double h	=rect.getHeight();
   		
   		if (x<left)
   			left	=x;
   		
   		if (y<top)
   			top	=y;
   		
   		if (x+w>right)
   			right	=x+w;
   		
   		if (y+h>bottom)
   			bottom	=y+h;
	}
	
	return new Rect(left,top,right-left,bottom-top);
   }

   /**
    *   Init all nodes of this rectangle. 
    *   We will always consider that a rectangle object has four nodes.
    *   node1 as left-top vertex,
    *   node2 as right-top vertex,
    *   node3 as left-bottom vertex,
    *   node4 as right-bottom vertex.
    */ 	
   protected void initNodes(){
   	//add four nodes if not assigned yet.
   	if (m_nodeList.size()<4){
   		try{ m_nodeList.clear(); 		}catch(Exception e){}
   		try{ m_nodeList.add(new Node()); 	}catch(Exception e){}
   		try{ m_nodeList.add(new Node()); 	}catch(Exception e){}
   		try{ m_nodeList.add(new Node()); 	}catch(Exception e){}
   		try{ m_nodeList.add(new Node()); 	}catch(Exception e){}
   	}
   	
       //set value of each node by m_rect.	
       Node node;

       	try {
       		node	=(Node)m_nodeList.getByIndex(0); 
       		node.setNodePoint(m_rect.getVertex(Rect.VERTEXTYPE_LEFTTOP));
       		node.setParent(this);
       	}catch(Exception e){
       	}
       
       	try{
       		node	=(Node)m_nodeList.getByIndex(1);
       		node.setNodePoint(m_rect.getVertex(Rect.VERTEXTYPE_RIGHTTOP));
       		node.setParent(this);
       	}catch(Exception e){
       	}

       	try{
       		node	=(Node)m_nodeList.getByIndex(2);
       		node.setNodePoint(m_rect.getVertex(Rect.VERTEXTYPE_LEFTBOTTOM));
       		node.setParent(this);
       	}catch(Exception e){
       	}
       	

       try{
       		node	=(Node)m_nodeList.getByIndex(3);
       		node.setNodePoint(m_rect.getVertex(Rect.VERTEXTYPE_RIGHTBOTTOM));
       		node.setParent(this);
       	}catch(Exception e){
       	}

	m_nodeList.setZoomScale(getZoomScale());
   }


   /**
    *   Get a node by vertex type.
    *   
    *   @param vertexType Vertex type of current node.
    *   @return A node of current rectangle.
    */ 	
   public Node getNodeByVertexType(int vertexType){
   	try{
   	   	switch(vertexType){
   	   		case Rect.VERTEXTYPE_LEFTTOP:
   	   			return (Node)m_nodeList.getByIndex(0);
   	   		case Rect.VERTEXTYPE_RIGHTTOP:
	   	   		return (Node)m_nodeList.getByIndex(1);
   	   		case Rect.VERTEXTYPE_LEFTBOTTOM:
   	   			return (Node)m_nodeList.getByIndex(2);
   	   		case Rect.VERTEXTYPE_RIGHTBOTTOM:
   	   			return (Node)m_nodeList.getByIndex(3);
   	   	}
   	 }catch(Exception e){
   	 }
   	return null;
   }

   /**
    *   Get vertex type by a node.
    *   
    *   @param node A node of this rectangle.
    *   @return the vertex type of this node.
    */ 	
   public int getVertexTypeByNode(Node node){

   	int vertexType = Rect.VERTEXTYPE_LEFTTOP;
	
	if (node!=null)
   		if (node.equals(getNodeByVertexType(Rect.VERTEXTYPE_LEFTTOP)))
   			vertexType =Rect.VERTEXTYPE_LEFTTOP;
   		else if (node.equals(getNodeByVertexType(Rect.VERTEXTYPE_RIGHTTOP)))
   			vertexType =Rect.VERTEXTYPE_RIGHTTOP;
   		else if (node.equals(getNodeByVertexType(Rect.VERTEXTYPE_LEFTBOTTOM)))
   			vertexType =Rect.VERTEXTYPE_LEFTBOTTOM;
   		else if (node.equals(getNodeByVertexType(Rect.VERTEXTYPE_RIGHTBOTTOM)))
   			vertexType =Rect.VERTEXTYPE_RIGHTBOTTOM;
   	
   	return vertexType;
   }


   /**
    *  fetch all sub objects of this objects,then add them into objList.
    */ 	
   public void fetchSubObjects(ObjectList objList){
   	Iterator it	=m_objList.getList().iterator();
   	while (it!=null && it.hasNext()){
   		AbstractShape shape	=(AbstractShape)it.next();
   		objList.add(shape);
   		shape.fetchSubObjects(objList);
	}
   }


   /**
    *   A loadFromStream method should only load an parentId-objectId list of ports attached,
    *   So here we use an attachRealPort to ACTUALLY attach some ports to the ports in list.
    * 
    *   @param shapeList  A shapeList used to pick out their ports for ports' attached list
    *
    */ 	
  public void attachRealPort(ObjectList shapeList){ 
  	super.attachRealPort(shapeList);
  	Iterator it	=m_objList.getList().iterator();
  	while (it!=null && it.hasNext()){
  		AbstractShape shape	=(AbstractShape)it.next();
  		shape.attachRealPort(shapeList);
	}
  }


   /**
    *   Finish drawing object.
    *
    */ 	
   public boolean finishDrawing(){
   	m_rect.setValue(getBoundsOfObjects());
   	initNodes();
   	initLabel();
   	return true;
   }

   /**
    *   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){
   		Iterator it =m_objList.getList().iterator();
   		while (it!=null && it.hasNext()){
   			AbstractShape	aShape	=(AbstractShape)it.next();
   			aShape.draw(g,isXorMode);
		}
	}
        
        if (isShowDesign() || isXorMode){
  		double zoom	=getZoomScale();
		
   		GeneralPath path= new GeneralPath(GeneralPath.WIND_EVEN_ODD); 
        
   		LineSeg left	 	=m_rect.getSide(Rect.SIDETYPE_LEFT);
   		LineSeg top	 	=m_rect.getSide(Rect.SIDETYPE_TOP);
   		LineSeg right	 	=m_rect.getSide(Rect.SIDETYPE_RIGHT);
   		LineSeg bottom		=m_rect.getSide(Rect.SIDETYPE_BOTTOM);

   		path.moveTo((float)(left.getX1()*zoom),(float)(left.getY1()*zoom));
   		path.lineTo((float)(left.getX2()*zoom),(float)(left.getY2()*zoom));
   		path.lineTo((float)(top.getX2()*zoom),(float)(top.getY2()*zoom));
   		path.lineTo((float)(right.getX2()*zoom),(float)(right.getY2()*zoom));
   		path.lineTo((float)(bottom.getX2()*zoom),(float)(bottom.getY2()*zoom));
   		path.closePath();  

		Graphics2D g2	=(Graphics2D)g;
	
		if (!isXorMode){
			BasicStroke	s = new BasicStroke(1,
                            	BasicStroke.CAP_SQUARE,   
                            	BasicStroke.JOIN_MITER,   
                            	10.0f,                    
                            	new float[] {2,4},  
                            	0.0f);                    
			g2.setStroke(s);
		}else{
			g2.setStroke(new BasicStroke(1));
		}
		g2.setColor(Color.gray);
		g2.draw(path);
	}
	

	
	if (!isXorMode){
		drawLabel(g);
	}
   }
 
  
   /**
    *   Add a new port to this shape.
    *   @param x,y Current picking position.
    *
    */ 	
   public Port addPort(double x,double y){
 
   	Iterator it =m_objList.getList().iterator();
   	while (it!=null && it.hasNext()){
   		AbstractShape	aShape	=(AbstractShape)it.next();
   		Port port	=aShape.addPort(x,y);
   		if (port!=null)
   			return port;
	}
	
	return null;
   }

   /**
    *   Get which port of current shape that intersects with point pnt.
    * 
    *   @param  pnt A JFPoint used to test intersection.
    *   @param  avoidParent A specified avoided parent to avoid connected to self.
    *
    *   @return A port of current shape.
    *
    */ 	
   public Port  portIntersects(JFPoint pnt,AbstractObject avoidParent){ 

        if (avoidParent!=null && this.equals(avoidParent))
                return null;

   	Iterator it =m_objList.getList().iterator();
   	while (it!=null && it.hasNext()){
   		AbstractShape	aShape	=(AbstractShape)it.next();
   		Port port	=aShape.portIntersects(pnt,avoidParent);
   		if (port!=null)
   			return port;
	}
	
   	return null;
   }

   /**
    *   Test if current object intersects with a specified point.
    * 
    *   @param  pnt A JFPoint used to test intersection.
    *
    */ 	
   public boolean  intersects(JFPoint pnt){ 
   	if (isInvisible()){
   		return getBounds().contains(pnt);
   	}else{
   		//we use intersects but not contains here, for an accurate pick.
   		return m_rect.intersects(pnt,GeomConst.PICK_OFFSET) || m_rect.contains(pnt);
   	}
   }

   /**
    *   Test if current object intersects with a specified rectangle.
    * 
    *   @param  rect A Rect used to test intersection.
    *
    */ 	
   public boolean  intersects(Rect rect){
   	if (isInvisible())
   		return getBounds().intersects(rect);
   	else
   		return m_rect.intersects(rect);
   }


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

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

	if (node!=null){
		if (node.getXOffset()==x && node.getYOffset()==y){
			draw(g,true);
		}else{
			//move node and draw a new dragging shape.
			int vertexType	=getVertexTypeByNode(node);
   			m_rect.moveVertex(vertexType,x,y,Rect.VERTEXMOVETYPE_SCALE);

   			//init shape nodes.
			initNodes();
		   		
   			//draw current moving node and its relational lines
   			draw(g,true);
   		}
	}
   }
   
   /**
    *   finish move/adjust a node of current object.
    * 
    *   @param  node Currently moving node.
    *
    *   @param  x,&nbsp;y Moving offsets.
    *
    *   @param g current drawing canvas.
    *
    */ 	
   public void  finishMoveNode(Node node, double x, double y,Graphics g){
   	if (node==null) return;
   		
   	finishMoveLabel();

  	double zoom	=getZoomScale();
	
	JFPoint leftTop		=m_originalRect.getVertex(Rect.VERTEXTYPE_LEFTTOP);
	JFPoint	rightTop	=m_originalRect.getVertex(Rect.VERTEXTYPE_RIGHTTOP);
	JFPoint	leftBottom	=m_originalRect.getVertex(Rect.VERTEXTYPE_LEFTBOTTOM);
	JFPoint	rightBottom	=m_originalRect.getVertex(Rect.VERTEXTYPE_RIGHTBOTTOM);
	
	JFPoint leftTop1	=m_rect.getVertex(Rect.VERTEXTYPE_LEFTTOP);
	JFPoint	rightTop1	=m_rect.getVertex(Rect.VERTEXTYPE_RIGHTTOP);
	JFPoint	leftBottom1	=m_rect.getVertex(Rect.VERTEXTYPE_LEFTBOTTOM);
	JFPoint	rightBottom1	=m_rect.getVertex(Rect.VERTEXTYPE_RIGHTBOTTOM);
	
	int vertexType	=getVertexTypeByNode(node);
	JFPoint basePoint=null, refPoint1=null,refPoint2=null;
	double scale=1.0;
	//we will force an same width and height scale for JFGroup.
	double w1	=leftTop.distance(rightTop);
	double w2	=leftTop1.distance(rightTop1);
	if (w1<1) w1=1;
	scale	=w2/w1;
	
	switch(vertexType){
		case Rect.VERTEXTYPE_LEFTTOP:
			basePoint	=rightBottom;
			refPoint1	=leftBottom;
			refPoint2	=rightTop;
			break;
						
		case Rect.VERTEXTYPE_RIGHTTOP:
			basePoint	=leftBottom;
			refPoint1	=rightBottom;
			refPoint2	=leftTop;
			break;

		case Rect.VERTEXTYPE_LEFTBOTTOM:
			basePoint	=rightTop;
			refPoint1	=leftTop;
			refPoint2	=rightBottom;
			break;

		case Rect.VERTEXTYPE_RIGHTBOTTOM:
			basePoint	=leftTop;
			refPoint1	=rightTop;
			refPoint2	=leftBottom;
			break;
	}
	
	if (basePoint==null || refPoint1==null || refPoint2==null)

⌨️ 快捷键说明

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