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

📄 lineformat.java

📁 用Java开发的、实现类似Visio功能的应用程序源码
💻 JAVA
字号:
/**
 *    $Id:LineFormat.java $
 *
 *    Copyright 2004 ~ 2005  JingFei International Cooperation LTD. All rights reserved. *
 */
package com.jfimagine.jfgraph.shape.decorate;


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

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.Stroke;
import java.awt.BasicStroke;
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.ShapeConst;
import com.jfimagine.jfgraph.shape.base.JFVersion;
 
 /**
 * Line format class. All lines or curves or circlles should use this class for their lines format.
 *
 * @author     CookieMaker    
 *
 * @version $Revision: 1.00 $
 */  
 public class LineFormat extends AbstractObject{
  
   //*****************************************************************
   //         XML Tags
   //*****************************************************************

   /**
    *   A XML string tag represents a line format.
    */
   public  static final String	 XML_LINEFORMAT		="LineFormat";

   /**
    *   A XML string tag represents the line style
    */
   public  static final String   XML_LINESTYLE		="lineStyle";
   /**
    *   A XML string tag represents a line's width
    */
   public  static final String   XML_LINEWIDTH		="lineWidth";
   /**
    *   A XML string tag represents a line's color
    */
   public  static final String   XML_LINECOLOR		="lineColor";


   //*****************************************************************
   //         Const definitions
   //*****************************************************************

   /**
    *   A const value represent SOLID line style.
    */
   public  static final int   LINESTYLE_SOLID		=0;

   /**
    *   A const value represent DOT line style.
    */
   public  static final int   LINESTYLE_DOT		=1;

   /**
    *   A const value represent DASH line style.
    */
   public  static final int   LINESTYLE_DASH		=2;


   /**
    *   A const value represent DASH DOT line style.
    */
   public  static final int   LINESTYLE_DASHDOT		=3;

   /**
    *   A const value represent DASH DOT DOT line style.
    */
   public  static final int   LINESTYLE_DASHDOTDOT	=4;
   

   private static LineFormat m_defaultLineFormat	=new LineFormat();

   /**
    *   set global default line format
    *   @param lineFormat A new line format
    */
   public static void setDefaultLineFormat(LineFormat lineFormat){
   	m_defaultLineFormat.setValue(lineFormat);
   }

   /**
    *   get global default line format
    *   @return The global default line format
    */
   public static LineFormat getDefaultLineFormat(){
   	return m_defaultLineFormat;
   }


   /**
    *   get an invisible bounds line format for invisibled shapes.
    *   @return A new invisible line format.
    */
   public static LineFormat getInvisibleLineFormat(){
   	LineFormat lineFormat	=new LineFormat();
   	lineFormat.setLineWidth(1);
   	lineFormat.setLineStyle(LINESTYLE_DASH);
   	lineFormat.setLineColor(Color.lightGray);
   	return lineFormat;
   }
 

   /**
    *   Line style variable.
    */
   private	int	m_lineStyle=LINESTYLE_SOLID;

   /**
    *   Line width variable,default to 1 pixel.
    */
   private	int	m_lineWidth=1;

   /**
    *   Line color variable,default to black.
    */
   private	Color	m_lineColor=Color.black;

	
   /**
    *   Constructor for LineFormat
    */
   public LineFormat(){
   	setObjectType(ShapeConst.DECORATETYPE_LINEFORMAT);
   	setXMLTag(XML_LINEFORMAT);
   	
   	if (m_defaultLineFormat!=null){ 
   		setValue(m_defaultLineFormat);
   	}
   }	
   
   /**
    *  clear format, then set to default values
    */
   public void clearFormat(){
   	m_lineStyle	=LINESTYLE_SOLID;
   	m_lineWidth	=1;
   	m_lineColor	=Color.black;   	
   }

   /**
    *   Get current line style.
    *
    *   @return  The line style.
    *
    */ 	
   public int getLineStyle(){
   	return m_lineStyle;
   }

   /**
    *   Set current line style.
    *
    *   @param lineStyle  A new line style.
    *
    */ 	
   public void setLineStyle(int lineStyle){
   	m_lineStyle	=lineStyle;
   }


   /**
    *   Get current line width.
    *
    *   @return  The line width.
    *
    */ 	
   public int getLineWidth(){
   	return m_lineWidth;
   }

   /**
    *   Set current line width.
    *
    *   @param lineWidth  A new line width.
    *
    */ 	
   public void setLineWidth(int lineWidth){
   	m_lineWidth	=lineWidth;
   }

   /**
    *   Get current line color.
    *
    *   @return  The line color.
    *
    */ 	
   public Color getLineColor(){
   	return m_lineColor;
   }

   /**
    *   Set current line color.
    *
    *   @param lineColor  A new line color.
    *
    */ 	
   public void setLineColor(Color lineColor){
   	if (lineColor!=null)
   		m_lineColor	=new Color(lineColor.getRGB());
   }


   /**
    *   Set new value for this lineFormat.
    *
    *   @param lineFormat A new line format.
    *
    */ 	
   public void setValue(LineFormat lineFormat){
   	if (lineFormat!=null){
   		setLineStyle(lineFormat.getLineStyle());
   		setLineWidth(lineFormat.getLineWidth());
   		setLineColor(lineFormat.getLineColor());
   	}
   }


   /**
    *   Draw a path on specified graphics context.
    *  @param g A specified graphics
    *  @param path A general path.
    *
    */ 	
   public void draw(Graphics g, GeneralPath path){
   	if (g==null || m_lineWidth<1)
   		return;

   	Graphics2D g2=(Graphics2D)g;
   	setGraphics(g2);
   	g2.draw(path);
   	initGraphics(g);
   }



   /**
    *   Set graphics to a default configuration.
    *
    */ 	
   public static void initGraphics(Graphics g){
   	Graphics2D g2=(Graphics2D)g;
   	g2.setStroke(new BasicStroke(1));
   	g2.setColor(Color.black);
   }


   /**
    *   Set graphics according to current line format.
    *
    */ 	
   public void setGraphics(Graphics g){
   	setGraphics(g,m_lineWidth,m_lineStyle,m_lineColor);
   }		

   /**
    *   Set graphics according to specified line format.
    *   @param lineWidth the line width to be drawn.
    *   @param lineStyle the line style
    *   @param lineColor the line color 
    */ 	
   public static void setGraphics(Graphics g,int lineWidth, int lineStyle,Color lineColor){

	Stroke s;
	
	float w1	=1 * lineWidth; 
	float w3	=3 * lineWidth; 
	float w5	=5 * lineWidth; 
	
	if (lineWidth<=1){
		w1	=2;
		w3	=2 * 3;
		w5	=2 * 5;		
	}
	
		
	switch (lineStyle){
   		case LINESTYLE_SOLID:
		    	s = new BasicStroke(lineWidth,
                            BasicStroke.CAP_SQUARE,     
                            BasicStroke.JOIN_MITER);    
   			break;
   			
   		case LINESTYLE_DOT:
			s = new BasicStroke(lineWidth,
                            BasicStroke.CAP_SQUARE,   
                            BasicStroke.JOIN_MITER,   
                            10.0f,                    
                            new float[] {w1,w3},  
                            0.0f);                    
                        break;    

   		case LINESTYLE_DASH:
			s = new BasicStroke(lineWidth,
                            BasicStroke.CAP_SQUARE,   
                            BasicStroke.JOIN_MITER,   
                            10.0f,                    
                            new float[] {w5,w3},  
                            0.0f);                    
                        break;    

   		case LINESTYLE_DASHDOT:
			s = new BasicStroke(lineWidth,
                            BasicStroke.CAP_SQUARE,   
                            BasicStroke.JOIN_MITER,   
                            10.0f,                    
                            new float[] {w5,w3,w1,w3},  
                            0.0f);                    
                        break;    

   		case LINESTYLE_DASHDOTDOT:
			s = new BasicStroke(lineWidth,
                            BasicStroke.CAP_SQUARE,   
                            BasicStroke.JOIN_MITER,   
                            10.0f,                    
                            new float[] {w5,w3,w1,w3,w1,w3},  
                            0.0f);                    
                        break;    
		
		default:
			return;                        
	}                        
	
       Graphics2D  g2	=(Graphics2D)g;
       g2.setStroke(s);
       g2.setColor(lineColor);                       	
   }
   

   /**
    *   Convert this object to String 
    * 
    *   @return  An string represents the content of the object
    *
    */ 	
   public String toString(){
  	StringBuffer buf =new StringBuffer();
  	buf.append(super.toString());
  	buf.append(";lineStyle=");  	buf.append(m_lineStyle);
  	buf.append(";lineWidth=");  	buf.append(m_lineWidth);
  	buf.append(";lineColor=");  	buf.append(m_lineColor.getRGB());
  	
  	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 LineFormat();
  }
  
     /**
    *   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{
  		LineFormat	lineFormat =(LineFormat) super.clone();
		lineFormat.setValue(this);  		
  		return lineFormat;
  		
	}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_lineStyle ^
  		m_lineWidth ^
  		m_lineColor.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 LineFormat))
            	return false;

      LineFormat lineFormat= (LineFormat)obj;
      
      return	(m_lineStyle==lineFormat.m_lineStyle) &&
      		(m_lineWidth==lineFormat.m_lineWidth) &&
      		(m_lineColor.equals(lineFormat.m_lineColor))
              	;
  }


   /**
    *   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_LINESTYLE, m_lineStyle));
    	element.addChild(new Element(XML_LINEWIDTH, m_lineWidth));
    	element.addChild(new Element(XML_LINECOLOR, m_lineColor.getRGB()));
  }

   /**
    *   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_lineStyle	=Element.getIntValue(element.getChild(XML_LINESTYLE));
      m_lineWidth	=Element.getIntValue(element.getChild(XML_LINEWIDTH));
      m_lineColor	=new Color(Element.getIntValue(element.getChild(XML_LINECOLOR)));
  }

 
   /**
    *   Save this object 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_lineStyle);
	stream.writeInt(m_lineWidth);
	stream.writeInt(m_lineColor.getRGB());
  }

   /**
    *   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_lineStyle		=stream.readInt();
	m_lineWidth		=stream.readInt();
	m_lineColor		=new Color(stream.readInt());
  }



}

⌨️ 快捷键说明

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