jflabelline.java

来自「用Java开发的、实现类似Visio功能的应用程序源码」· Java 代码 · 共 1,053 行 · 第 1/2 页

JAVA
1,053
字号
   }

   /**
    *   Reverse this object by a central y coordinate of this object. We make a up-down flip here.
    */ 	
   public void flipBy(){
   	m_labelLine.flipBy();
   	initNodes();
   }

   /**
    *   Reverse this object by a y coordinate. We make a up-down flip here.
    *
    *   @param baseY  A flip base y coordinate.
    *
    */ 	
   public void flipBy(double baseY){
   	m_labelLine.flipBy(baseY);
   	initNodes();
   }

   /**
    *   Draw current object's arrows.
    */ 	
   protected void  drawArrow(Graphics g, JFPoint startPoint, JFPoint endPoint){
   	int lineWidth	=m_lineFormat.getLineWidth();

   	Color c		=m_lineFormat.getLineColor();
   	int startArrow	=m_arrow.getStartArrow();
   	int endArrow	=m_arrow.getEndArrow();

   	Arrow.drawArrow(startArrow,lineWidth,endPoint,startPoint,g,c);
   	Arrow.drawArrow(endArrow,lineWidth,startPoint,endPoint,g,c);
   }	

   
   /**
    *   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 (!isXorMode)
		setTransparencyComposite(g);

  	double zoom	=getZoomScale();
   	Graphics2D  g2	=(Graphics2D)g;
   	GeneralPath line;
   	
	JFPoint startPoint	=m_labelLine.getPoint1();
	JFPoint endPoint	=m_labelLine.getPoint2();
	JFPoint ctrl1		=m_labelLine.getCtrlPoint1();
	JFPoint ctrl2		=m_labelLine.getCtrlPoint2();
	JFPoint ctrl3		=m_labelLine.getCtrlPoint3();
	JFPoint ctrl4		=m_labelLine.getCtrlPoint4();

	float  startX		=(float)(startPoint.getX() * zoom);
	float  startY		=(float)(startPoint.getY() * zoom);
	float  endX		=(float)(endPoint.getX() * zoom);
	float  endY		=(float)(endPoint.getY() * zoom);

	float  ctrl1X		=(float)(ctrl1.getX() * zoom);
	float  ctrl1Y		=(float)(ctrl1.getY() * zoom);
	float  ctrl2X		=(float)(ctrl2.getX() * zoom);
	float  ctrl2Y		=(float)(ctrl2.getY() * zoom);
	float  ctrl3X		=(float)(ctrl3.getX() * zoom);
	float  ctrl3Y		=(float)(ctrl3.getY() * zoom);
	float  ctrl4X		=(float)(ctrl4.getX() * zoom);
	float  ctrl4Y		=(float)(ctrl4.getY() * zoom);
	
	
	//two control parallel lines.
        line= new GeneralPath(GeneralPath.WIND_EVEN_ODD); 
        line.moveTo(ctrl1X,ctrl1Y);
        line.lineTo(ctrl2X,ctrl2Y);

        line.moveTo(ctrl3X,ctrl3Y);
        line.lineTo(ctrl4X,ctrl4Y);

	if (!isXorMode){
		g2.setStroke(new BasicStroke(1));
		g2.setColor(m_lineFormat.getLineColor());
	}else{
   		g2.setStroke(new BasicStroke(1));
   		g2.setColor(Color.black);
   	}

	g2.draw(line);  
	
	//main label line.
        line= new GeneralPath(GeneralPath.WIND_EVEN_ODD); 
        line.moveTo(startX,startY);
        line.lineTo(endX,endY);

   	if (!isXorMode){
   		//m_lineFormat.setGraphics(g);
   		m_lineFormat.draw(g2,line);
	}else{
   		g2.setStroke(new BasicStroke(1));
   		g2.setColor(Color.black);
   		g2.draw(line);  
   	}

        if (!isXorMode)
		restoreTransparencyComposite(g);
	
	
	//draw arrows
	if (!isXorMode){
		JFPoint newStartPoint	=new JFPoint(startX,startY);
		JFPoint newEndPoint	=new JFPoint(endX,endY);
		drawArrow(g,newStartPoint,newEndPoint);  
		drawLabel(g);
	}

	m_lineFormat.initGraphics(g);	
  }


   /**
    *   Draw a line on graphic canvas.
    * 
    *   @param  g  A graphic canvas.	
    *   @param  startPoint Start point of line.
    *   @param  endPoint end point of line.
    *
    */ 	
   private void  drawLine(Graphics g, JFPoint startPoint, JFPoint endPoint){
  	double zoom	=getZoomScale();
        GeneralPath line= new GeneralPath(GeneralPath.WIND_EVEN_ODD); 

	float  startX		=(float)(startPoint.getX() * zoom);
	float  startY		=(float)(startPoint.getY() * zoom);
	float  endX		=(float)(endPoint.getX() * zoom);
	float  endY		=(float)(endPoint.getY() * zoom);
        
        line.moveTo(startX,startY);
        line.lineTo(endX,endY);
        
	((Graphics2D)g).draw(line);        
   }


   /**
    * When moving a node, in some cases, e.g. ctrl key or shift key pressed to force preserving 
    * the form of a shape, or force equilateral shapes, we need to recalculate the actual moving
    * node's position.
    *
    *  @param movePos Desire moving position of a node.
    *  @param moveCase Move case of a node, normal, shift key pressed or ctrl key pressed
    *  @return The actual moving position of a node.
    *
    */	  
   public JFPoint getMoveNodePos(Node node,JFPoint movePos,int moveCase){
   	if (node==null || movePos==null)
   		return null;
   	
   	switch (moveCase){
   		case ShapeConst.MOVEMENT_NORMAL:
   			return movePos;

   		case ShapeConst.MOVEMENT_SHIFTDOWN:
   		case ShapeConst.MOVEMENT_CTRLDOWN:

			int index	=m_nodeList.getIndexByObjectId(node.getObjectId());
			//only change two end points.
			if (index<0 || index>1) return movePos;

			JFPoint start	=m_labelLine.getPoint1();
			JFPoint end	=m_labelLine.getPoint2();
			JFPoint baseNode;			
			if (index==0)
				baseNode	=end;
			else
				baseNode	=start;
			
   			//offset between current moving position and baseNode
   			double x	=movePos.getX() - baseNode.getX();
   			double y	=movePos.getY() - baseNode.getY();
   			if (Math.abs(x)>Math.abs(y)){
   				movePos.setY(baseNode.getY());
   			}else{
   				movePos.setX(baseNode.getX());
   			}
   			return movePos;			

   	}

   	
   	return movePos;
   }

   
   /**
    *   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 && node instanceof Node){

		int index	=m_nodeList.getIndexByObjectId(node.getObjectId());
		if (index<0) return;
		
		JFPoint start, end;
		switch (index){
			case 0: //moving start point
				m_labelLine.setPoint1(x,y);
				initNodes();
				draw(g,true);
				break;
			case 1: //moving end point
				m_labelLine.setPoint2(x,y);
				initNodes();
				draw(g,true);
				break;
			case 2: //moving control point1
				m_labelLine.setCtrlPoint1(x,y);
				initNodes();
				start	=m_labelLine.getPoint1();
				end	=m_labelLine.getCtrlPoint1();
				drawLine(g,start,end);
				break;
			case 3: //moving control point2
				m_labelLine.setCtrlPoint2(x,y);
				initNodes();
				start	=m_labelLine.getPoint1();
				end	=m_labelLine.getCtrlPoint2();
				drawLine(g,start,end);
				break;
			case 4: //moving control point3
				m_labelLine.setCtrlPoint3(x,y);
				initNodes();
				start	=m_labelLine.getPoint2();
				end	=m_labelLine.getCtrlPoint3();
				drawLine(g,start,end);
				break;
			case 5: //moving control point4
				m_labelLine.setCtrlPoint4(x,y);
				initNodes();
				start	=m_labelLine.getPoint2();
				end	=m_labelLine.getCtrlPoint4();
				drawLine(g,start,end);
				break;
		}
			
		node.draw(g,false);
	}
   }

   /**
    *   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<arrowType>");
   	buf.append(m_arrow.toString());
   	buf.append("\n<lineFormat>");
   	buf.append(m_lineFormat.toString());
	buf.append("\n");
	buf.append(m_labelLine.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 JFLabelLine();
  }
  
  
   /**
    *   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;
  		}
  		
  		JFLabelLine line =(JFLabelLine) obj;
  		line.m_arrow.setValue(m_arrow);
  		line.m_labelLine	=new LabelLine(m_labelLine);
  		line.m_lineFormat.setValue(m_lineFormat);
  		
  		return line;
  		
	}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_labelLine.hashCode() ^ 
  		m_lineFormat.hashCode() ^
  		m_arrow.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 JFLabelLine))
            return false;

      JFLabelLine  line= (JFLabelLine)obj;
      
      return  	(line.m_labelLine.equals(m_labelLine))&&
      		(line.m_arrow.equals(m_arrow)) &&
        	(line.m_lineFormat.equals(m_lineFormat));
  }


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

	JFPoint startPoint	=m_labelLine.getPoint1();
	JFPoint endPoint	=m_labelLine.getPoint2();
	
	boolean ctrl1Direction  =m_labelLine.getCtrl1Direction();
	double ctrl1Len		=m_labelLine.getCtrl1Len();
	double ctrl2Len		=m_labelLine.getCtrl2Len();
	double ctrl3Len		=m_labelLine.getCtrl3Len();
	double ctrl4Len		=m_labelLine.getCtrl4Len();
	
	//start point and end point
    	element.addChild(new Element(XML_STARTX,startPoint.getX()));
    	element.addChild(new Element(XML_STARTY,startPoint.getY()));
    	element.addChild(new Element(XML_ENDX,endPoint.getX()));
    	element.addChild(new Element(XML_ENDY,endPoint.getY()));

	//ctrl1 direction
    	element.addChild(new Element(XML_CTRL1DIRECTION,ctrl1Direction));

	//ctrl1....ctrl4 length
    	element.addChild(new Element(XML_CTRL1LEN,ctrl1Len));
    	element.addChild(new Element(XML_CTRL2LEN,ctrl2Len));
    	element.addChild(new Element(XML_CTRL3LEN,ctrl3Len));
    	element.addChild(new Element(XML_CTRL4LEN,ctrl4Len));

	//others.
	m_arrow.toDOM(element,version);
    	m_lineFormat.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);


	//start point and end point
     	double x1	=Element.getDoubleValue(element.getChild(XML_STARTX));
     	double y1	=Element.getDoubleValue(element.getChild(XML_STARTY));
     	double x2	=Element.getDoubleValue(element.getChild(XML_ENDX));
     	double y2	=Element.getDoubleValue(element.getChild(XML_ENDY));
     	m_labelLine.setValue(x1,y1,x2,y2);

	//ctrl1 direction
	boolean ctrl1Direction 	=Element.getBooleanValue(element.getChild(XML_CTRL1DIRECTION));
     	m_labelLine.setCtrl1Direction(ctrl1Direction);

	//ctrl1....ctrl4 length
    	double ctrl1Len	=Element.getDoubleValue(element.getChild(XML_CTRL1LEN));
    	double ctrl2Len	=Element.getDoubleValue(element.getChild(XML_CTRL2LEN));
    	double ctrl3Len	=Element.getDoubleValue(element.getChild(XML_CTRL3LEN));
    	double ctrl4Len	=Element.getDoubleValue(element.getChild(XML_CTRL4LEN));
    	m_labelLine.setCtrl1Len(ctrl1Len);
    	m_labelLine.setCtrl2Len(ctrl2Len);
    	m_labelLine.setCtrl3Len(ctrl3Len);
    	m_labelLine.setCtrl4Len(ctrl4Len);
	
	m_arrow.fromDOM(element.getChild(m_arrow.getXMLTag()),version);
      	m_lineFormat.fromDOM(element.getChild(m_lineFormat.getXMLTag()),version);
      
      	initNodes();
  }

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

	JFPoint startPoint	=m_labelLine.getPoint1();
	JFPoint endPoint	=m_labelLine.getPoint2();
	
	boolean ctrl1Direction  =m_labelLine.getCtrl1Direction();
	double ctrl1Len		=m_labelLine.getCtrl1Len();
	double ctrl2Len		=m_labelLine.getCtrl2Len();
	double ctrl3Len		=m_labelLine.getCtrl3Len();
	double ctrl4Len		=m_labelLine.getCtrl4Len();
	
	stream.writeDouble(startPoint.getX());
	stream.writeDouble(startPoint.getY());
	stream.writeDouble(endPoint.getX());
	stream.writeDouble(endPoint.getY());
		
	stream.writeBoolean(ctrl1Direction);
	stream.writeDouble(ctrl1Len);
	stream.writeDouble(ctrl2Len);
	stream.writeDouble(ctrl3Len);
	stream.writeDouble(ctrl4Len);
	
	m_arrow.saveToStream(stream,version);
	m_lineFormat.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);

	double x1	=stream.readDouble();
	double y1	=stream.readDouble();
	double x2	=stream.readDouble();
	double y2	=stream.readDouble();
	m_labelLine.setValue(x1,y1,x2,y2);

	boolean ctrl1Direction 	=stream.readBoolean();
	m_labelLine.setCtrl1Direction(ctrl1Direction);

	double ctrl1Len	=stream.readDouble();
	double ctrl2Len	=stream.readDouble();
	double ctrl3Len	=stream.readDouble();
	double ctrl4Len	=stream.readDouble();
	m_labelLine.setCtrl1Len(ctrl1Len);
	m_labelLine.setCtrl1Len(ctrl2Len);
	m_labelLine.setCtrl1Len(ctrl3Len);
	m_labelLine.setCtrl1Len(ctrl4Len);
	
	m_arrow.loadFromStream(stream,false,version);
	m_lineFormat.loadFromStream(stream,false,version);//don't skip head here
      	    
      	initNodes();
  }



 }

⌨️ 快捷键说明

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