outputlist.java

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

JAVA
220
字号

/**
 *    $Id: OutputList.java $
 *
 *    Copyright 2004 ~ 2005 JingFei International Cooperation LTD. All rights reserved.
 *
 */
package com.jfimagine.jfgraph.transfer;


import java.util.Iterator;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.RenderingHints;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.geom.AffineTransform;


import com.jfimagine.jfgraph.shape.decorate.JFRuler;
import com.jfimagine.jfgraph.shape.decorate.GridFormat;
import com.jfimagine.jfgraph.shape.decorate.JFPageFormat;
import com.jfimagine.jfgraph.shape.union.JFPage; 

import com.jfimagine.jfgraph.geom.Rect;

import com.jfimagine.jfgraph.shape.base.AbstractShape;
import com.jfimagine.jfgraph.shape.base.ObjectList;

import com.jfimagine.utils.log.*;

 /**
 * OutputList is used to output an object list to a graphics context, or get an image object on this object list.
 *
 * @author     CookieMaker    
 *
 * @version $Revision: 1.1.2 $
 */ 
 
public class OutputList {

    	/**an internal log utility*/
    	private JFLogger m_logger	=JFLogManager.getLogger(this.getClass());


    	/** An object list to output. */
	private ObjectList m_objList=null;

    	/** export width.*/
    	private int 	m_width			=0;

    	/** export height.*/
    	private int 	m_height		=0;
        
        
        /** if prepared export*/
        private boolean m_prepared		=false;

	/** get export width */	
	public int getExportWidth(){
		return m_width;
	}    	

	/** get export height */
	public int getExportHeight(){
		return m_height;
	}    	

	/**
	 * get current object list.
	 */
	public ObjectList getList(){
		return m_objList;
	}
	    	
    	/** set current drawing page.
     	 *  @param page A new page.
     	 *  @param zoomScale Zoom scale of current page.
     	 *  @param cloneNew If need to clone a new object list to output.
     	 */	
    	public void setList(ObjectList list, double zoomScale, boolean cloneNew){
    		try{
    			if (cloneNew)
				m_objList	=(ObjectList)list.clone();
			else
				m_objList	=list;
				
			m_objList.setShowDesign(false);
			m_objList.setZoomScale(zoomScale);
			
			m_prepared	=false;			
			prepareExport();
		}catch(Exception e){
		}
	}
	
	
   	/** 
    	*  Draw an object list to an internal buffered image.
    	*  @return A image that generated.
    	*/
   	public BufferedImage getImage() {
   		try{
   			if (m_objList==null)
   				return null;
			
			//prepare images in list, then get the size of image.
			prepareExport();
			
        		// Create a buffered image in which to draw
        		BufferedImage bufferedImage = new BufferedImage(m_width,m_height, BufferedImage.TYPE_INT_RGB);
        		// Create a graphics contents on the buffered image
        		Graphics2D g2 = bufferedImage.createGraphics();
        		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);

        		// Draw graphics
        		g2.setColor(Color.white);
        		g2.fillRect(0, 0, m_width, m_height);

                        draw(g2);

        		// Graphics context no longer needed so dispose it
        		g2.dispose();

			return bufferedImage;

		}catch(Exception e){
			m_logger.error("getImage: "+e);
			return null;
		}

   	}

   	/** 
    	*  Draw an object list to a specified graphics context.
    	*  @param g A specified graphics context.
    	*/
   	public void draw(Graphics g) {
		
		Graphics2D g2	=(Graphics2D)g;    
        	
   		Iterator it=m_objList.getList().iterator();
   		while (it!=null && it.hasNext()){
   			AbstractShape shape	=(AbstractShape)it.next();
   			if (shape!=null){
   				shape.draw(g2,false);
   			}
   		}
        }
    

   	
   	/** 
    	*  Prepare an objectlist to draw.
    	*/
   	protected void prepareExport(){

	  try{
		if (m_prepared) return;
				
		//get bounds that contains shapes and labels.
   		double minx=10000,miny=10000,maxx=0,maxy=0;
   		AbstractShape shape;
   		com.jfimagine.jfgraph.shape.base.Label label;
   		Iterator it=m_objList.getList().iterator();
   		while (it!=null && it.hasNext()){
   			shape	=(AbstractShape)it.next();
   			if (shape!=null){
   				//shape bounds
   				Rect bounds	=shape.getBounds();
   				minx		=Math.min(minx,bounds.getX());
   				miny		=Math.min(miny,bounds.getY());
   				maxx		=Math.max(maxx,bounds.getX()+bounds.getWidth());
   				maxy		=Math.max(maxy,bounds.getY()+bounds.getHeight());
                                
                                //label bounds
				label		=shape.getLabel();   				
				bounds		=label.getBounds();
				if (bounds.getWidth()>0 || bounds.getHeight()>0){
   					minx		=Math.min(minx,bounds.getX());
   					miny		=Math.min(miny,bounds.getY());
   					maxx		=Math.max(maxx,bounds.getX()+bounds.getWidth());
   					maxy		=Math.max(maxy,bounds.getY()+bounds.getHeight());
   				}
   			}
   		}  	
   
       		if (minx>maxx)	minx=maxx;
       		if (miny>maxy)  miny=maxy;
       		
       		//an offset used to enlarge the final output image.
       		int offset	=10;
		
		//move selection objects       		
   		it=m_objList.getList().iterator();
   		while (it!=null && it.hasNext()){
   			shape	=(AbstractShape)it.next();
   			if (shape!=null){
   				shape.moveBy(-minx+offset,-miny+offset);
   			}
   		}
                
                //output image size.
        	m_width		=(int)(maxx-minx+ 2*offset);
        	m_height	=(int)(maxy-miny+ 2*offset);

		m_prepared	=true;
		                
	  }catch(Exception e){
	  	m_logger.error("prepareExport: "+e);
	  }
      
    }


}

⌨️ 快捷键说明

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