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

📄 fillformat.java

📁 用Java开发的、实现类似Visio功能的应用程序源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
   public 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,Rect rect){  
   	


	Graphics2D  g2	=(Graphics2D)g;
	
	if (m_fillStyle==FILLSTYLE_EMPTY)
		return;

	if (m_fillStyle==FILLSTYLE_SOLID){
		g2.setColor(m_fillColor);	
		return;
	}
	

	switch (m_fillStyle){
		case FILLSTYLE_SLASH:
                case FILLSTYLE_REVERSESLASH:
                case FILLSTYLE_VERTICALLINE: 
                case FILLSTYLE_HORIZONTALLINE:
                case FILLSTYLE_GRID:
                case FILLSTYLE_INCLINEDGRID:	


                        int width	=40;
                        int height	=40;

	                //use a buffered image for texture paint.       
    	                BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    	
    	                //background color
	                Graphics2D fillg = bi.createGraphics();
	                fillg.setColor(m_fillColor);
	                fillg.fillRect(0, 0, width, height);
	

	                //forgound lines.
	                int w1	=10;
	
	                int w=0;
	                int max=(int)Math.max(width,height);


   	                //pick up each nodes pair, and draw each line segment between a nodes pair.
                        GeneralPath line= new GeneralPath(GeneralPath.WIND_EVEN_ODD); 
	                switch (m_fillStyle){
		                case  FILLSTYLE_SLASH:
		                        w       =5; 
			                while (w<max*2){
				                line.moveTo(w,0);
				                line.lineTo(0,w);
				                w	+=w1;
			                }
			                break;                  
			
                                case FILLSTYLE_REVERSESLASH:
			                while (w<max*2){
				                w	+=w1;
				                line.moveTo(max-w,0);
				                line.lineTo(max,w);
			                }
                
                                        break;
                                case FILLSTYLE_VERTICALLINE: 
                                        w   =4;
		                        w1  =8;
			                while (w<max){
				                line.moveTo(w,0);
				                line.lineTo(w,max);
				                w	+=w1;
			                }

                                        break;
                                case FILLSTYLE_HORIZONTALLINE:
                                        w   =4;
		                        w1  =8;
			                while (w<max){
				                line.moveTo(0,w);
				                line.lineTo(max,w);
				                w	+=w1;
			                }
                
                                        break;
                                case FILLSTYLE_GRID:
        
                                        w   =4;
		                        w1  =8;
			                while (w<max){
				                line.moveTo(w,0);
				                line.lineTo(w,max);
				                w	+=w1;
			                }

                                        w   =4;
			                while (w<max){
				                line.moveTo(0,w);
				                line.lineTo(max,w);
				                w	+=w1;
			                }

                                        break;

                                case FILLSTYLE_INCLINEDGRID:	

		                        w       =5;
			                while (w<max*2){
				                line.moveTo(w,0);
				                line.lineTo(0,w);
				                w	+=w1;
			                }

                                        w       =0;			
			                while (w<max*2){
				                w	+=w1;
				                line.moveTo(max-w,0);
				                line.lineTo(max,w);
			                }

                                        break;
	                }
        
	                LineFormat.setGraphics(fillg,1,m_fillLineStyle,m_fillLineColor);
	                fillg.draw(line);
                        LineFormat.initGraphics(fillg);
	
	                Rectangle r = new Rectangle(0,0,width,height);
	                g2.setPaint(new TexturePaint(bi, r));        
	                
	                break;



                case  FILLSTYLE_GRADIENT_LEFTRIGHT:
                case  FILLSTYLE_GRADIENT_RIGHTLEFT:
                case  FILLSTYLE_GRADIENT_UPDOWN:
                case  FILLSTYLE_GRADIENT_DOWNUP:
                case  FILLSTYLE_GRADIENT_UPLEFT_RIGHTDOWN:
                case  FILLSTYLE_GRADIENT_UPRIGHT_LEFTDOWN:
                case  FILLSTYLE_GRADIENT_DOWNLEFT_RIGHTUP:
                case  FILLSTYLE_GRADIENT_DOWNRIGHT_LEFTUP:

                        JFPoint leftTop         =rect.getVertex(Rect.VERTEXTYPE_LEFTTOP);
                        JFPoint rightBottom     =rect.getVertex(Rect.VERTEXTYPE_RIGHTBOTTOM);
                        
                        //leftTop
                        float x1		=(float)leftTop.getX();
                        float y1		=(float)leftTop.getY();
                        //rightBottom
                        float x2		=(float)rightBottom.getX();
                        float y2		=(float)rightBottom.getY();
			
			//center                        
                        float x0		=(float)((x1+x2)/2);
                        float y0		=(float)((y1+y2)/2);
			
			Color c1		=m_fillColor;
			Color c2		=m_fillColor2;
			GradientPaint	gp=null;
			switch (m_fillStyle){
                		case  FILLSTYLE_GRADIENT_LEFTRIGHT:
                        		gp	=new GradientPaint(x1,y0,c1,x2,y0,c2);
                        		break;
                		case  FILLSTYLE_GRADIENT_RIGHTLEFT:
                        		gp	=new GradientPaint(x2,y0,c1,x1,y0,c2);
                        		break;
                		case  FILLSTYLE_GRADIENT_UPDOWN:
                        		gp	=new GradientPaint(x0,y1,c1,x0,y2,c2);
                        		break;
                		case  FILLSTYLE_GRADIENT_DOWNUP:
                        		gp	=new GradientPaint(x0,y2,c1,x0,y1,c2);
                        		break;
                		case  FILLSTYLE_GRADIENT_UPLEFT_RIGHTDOWN:
                        		gp	=new GradientPaint(x1,y1,c1,x2,y2,c2);
                        		break;
                		case  FILLSTYLE_GRADIENT_UPRIGHT_LEFTDOWN:
                        		gp	=new GradientPaint(x2,y1,c1,x1,y2,c2);
                        		break;
                		case  FILLSTYLE_GRADIENT_DOWNLEFT_RIGHTUP:
                        		gp	=new GradientPaint(x1,y2,c1,x2,y1,c2);
                        		break;
                		case  FILLSTYLE_GRADIENT_DOWNRIGHT_LEFTUP:
                        		gp	=new GradientPaint(x2,y2,c1,x1,y1,c2);
                        		break;
                       }
                       
                       if (gp!=null)
                       		g2.setPaint(gp);
        }                        
   }             

 
        
   /**
    *   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(";fillStyle=");  	buf.append(m_fillStyle);
  	buf.append(";fillColor=");  	buf.append(m_fillColor);
  	buf.append(";fillColor2=");  	buf.append(m_fillColor2);
  	buf.append(";fillLineStyle="); 	buf.append(m_fillLineStyle);
  	buf.append(";fillLineColor="); 	buf.append(m_fillLineColor);
  	
  	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 FillFormat();
  }
  
     /**
    *   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{
  		FillFormat	fillFormat =(FillFormat) super.clone();

  		fillFormat.m_fillStyle		=this.m_fillStyle;
  		fillFormat.m_fillColor		=new Color(this.m_fillColor.getRGB());
  		fillFormat.m_fillColor2		=new Color(this.m_fillColor2.getRGB());
		fillFormat.m_fillLineStyle  	=this.m_fillLineStyle;
		fillFormat.m_fillLineColor	=new Color(this.m_fillLineColor.getRGB());
 		
  		return fillFormat;
  		
	}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_fillStyle ^
  		m_fillColor.hashCode() ^
  		m_fillColor2.hashCode() ^
  		m_fillLineStyle ^
  		m_fillLineColor.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 FillFormat))
            	return false;

      FillFormat fillFormat= (FillFormat)obj;
      
      return	(m_fillStyle==fillFormat.m_fillStyle) &&
      		(m_fillColor.equals(fillFormat.m_fillColor)) &&
      		(m_fillColor2.equals(fillFormat.m_fillColor2)) &&
      		(m_fillLineStyle==fillFormat.m_fillLineStyle) &&
      		(m_fillLineColor.equals(fillFormat.m_fillLineColor))
              	;
  }


   /**
    *   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_FILLSTYLE, m_fillStyle));
    	element.addChild(new Element(XML_FILLCOLOR, m_fillColor.getRGB()));
    	element.addChild(new Element(XML_FILLCOLOR2, m_fillColor2.getRGB()));
    	element.addChild(new Element(XML_FILLLINESTYLE, m_fillLineStyle));
    	element.addChild(new Element(XML_FILLLINECOLOR, m_fillLineColor.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_fillStyle	=Element.getIntValue(element.getChild(XML_FILLSTYLE));
      m_fillColor	=new Color(Element.getIntValue(element.getChild(XML_FILLCOLOR)));
      m_fillColor2	=new Color(Element.getIntValue(element.getChild(XML_FILLCOLOR2)));
      m_fillLineStyle	=Element.getIntValue(element.getChild(XML_FILLLINESTYLE));
      m_fillLineColor	=new Color(Element.getIntValue(element.getChild(XML_FILLLINECOLOR)));
  }

 
   /**
    *   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_fillStyle);
	stream.writeInt(m_fillColor.getRGB());
	stream.writeInt(m_fillColor2.getRGB());
	stream.writeInt(m_fillLineStyle);
	stream.writeInt(m_fillLineColor.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_fillStyle		=stream.readInt();
	m_fillColor		=new Color(stream.readInt());
	m_fillColor2		=new Color(stream.readInt());
	m_fillLineStyle		=stream.readInt();
	m_fillLineColor		=new Color(stream.readInt());
  }


}

⌨️ 快捷键说明

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