outputpage.java
来自「用Java开发的、实现类似Visio功能的应用程序源码」· Java 代码 · 共 458 行
JAVA
458 行
/**
* $Id: OutputPage.java $
*
* Copyright 2004 ~ 2005 JingFei International Cooperation LTD. All rights reserved.
*
*/
package com.jfimagine.jfgraph.transfer;
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;
/**
* OutputList is used to output a page to a graphics context, or get an image object on this page.
*
* @author CookieMaker
*
* @version $Revision: 1.3.0 $
*/
public class OutputPage {
/** Export/Print graph with grid.*/
private boolean m_exportWithGrid =false;
/** Export/Print graph with ruler.*/
private boolean m_exportWithRuler =false;
/** if is under metric or english measurement.*/
private boolean m_isMetric =true;
/** grid of this canvas.*/
private GridFormat m_gridFormat =new GridFormat();
/** A page for drawing.*/
private JFPage m_page =new JFPage();
/** export width.*/
private int m_width =0;
/** export height.*/
private int m_height =0;
/** if use page bounds or page format to decide the size of an exportation
* this is used when exporting a page.
* True to use page bounds, false use page format.
*/
private boolean m_usePageBounds =true;
/** if use page bounds or page format to decide the size of an exportation
* this is used when exporting a page.
* @return True to use page bounds, false use page format.
*/
public boolean isUsePageBounds(){
return m_usePageBounds;
}
public void setUsePageBounds(boolean usePageBounds){
m_usePageBounds =usePageBounds;
}
/** get export width */
public int getExportWidth(){
return m_width;
}
/** get export height */
public int getExportHeight(){
return m_height;
}
/** Set export with grid, or not.
* @param withGrid if export with grid.
*/
public void setExportWithGrid(boolean withGrid){
m_exportWithGrid =withGrid;
}
/** Set export with ruler, or not.
* @param withRuler if export with ruler.
*/
public void setExportWithRuler(boolean withRuler){
m_exportWithRuler =withRuler;
}
/** set current grid format
* @param gridFormat A new grid format.
*/
public void setGridFormat(GridFormat gridFormat){
m_gridFormat.setValue(gridFormat);
}
/**
* get current page.
*/
public JFPage getPage(){
return m_page;
}
/** set current drawing page.
* @param page A new page.
* @param zoomScale Zoom scale of current page.
*/
public void setPage(JFPage page,double zoomScale){
setPage(page,zoomScale,true);
}
/** set current drawing page.
* @param page A new page.
* @param zoomScale Zoom scale of current page.
* @param cloneNew If need to clone a new page to protect the original page's data.
*/
public void setPage(JFPage page,double zoomScale, boolean cloneNew){
try{
if (cloneNew){
m_page =(JFPage)page.clone();
}else{
m_page =page;
}
//hide show design.
m_page.setShowDesign(false);
//m_page.getCurrentLayer().setShowDesign(false);
//reset scale
if (m_page.getZoomScale()!=zoomScale)
m_page.setZoomScale(zoomScale);
}catch(Exception e){
}
}
public void setIsMetric(boolean isMetric) {
this.m_isMetric = isMetric;
}
/**
* Prepare a page to draw.
* @param usePageBounds if use page bounds or page format to decide exporting size.
*/
protected void prepareExport(boolean usePageBounds){
m_usePageBounds =usePageBounds;
if (m_page==null) return;
Dimension d,size;
//here we used the zoomScale below to zoom in or zoom out the shapes for special exporting usage,
//especially in print preview.
double zoomScale =m_page.getZoomScale();
//we will calculate the page size firstly by page format
JFPageFormat pageFormat =m_page.getPageFormat();
d =new Dimension((int)(pageFormat.getPageFormat().getWidth()* zoomScale),(int)(pageFormat.getPageFormat().getHeight() * zoomScale));
size =JFPageFormat.getScreenDimension(d);
m_width =(int)size.getWidth();
m_height =(int)size.getHeight();
//if use page bounds, we'll try to find out the maximum size of exporting.
if (m_usePageBounds){
Rect bounds =m_page.getBounds();
double extendSize =50; //we used an extended size here
int width1 =(int)((bounds.getX()+bounds.getWidth()+extendSize) * zoomScale);
int height1 =(int)((bounds.getY()+bounds.getHeight()+extendSize) * zoomScale);
if (width1>m_width)
m_width =width1;
if (height1>m_height)
m_height =height1;
}
if (m_exportWithRuler){
m_width +=JFRuler.SIZE;
m_height +=JFRuler.SIZE;
}
}
/**
* Draw current page to a specified graphics context.
* @param g A specified graphics context.
*/
public void draw(Graphics g){
draw(g,m_page);
}
/**
* Draw current page to a specified graphics context.
* @param g A specified graphics context.
* @param page A page to be drawn.
*/
public void draw(Graphics g, JFPage page){
prepareExport(m_usePageBounds);
Graphics2D g2 =(Graphics2D)g;
//if drawing rulers, there would be a xOffset and a yOffset.
double xOffset =0;
double yOffset =0;
com.jfimagine.jfgraph.shape.decorate.CanvasFormat canvasFormat =new com.jfimagine.jfgraph.shape.decorate.CanvasFormat();
int width =(int)canvasFormat.getScreenWidth();
int height =(int)canvasFormat.getScreenHeight();
//draw ruler
if (m_exportWithRuler){
drawRuler(page,g2,xOffset,yOffset,width,height,m_isMetric);
//drawRuler(page,g2,xOffset,yOffset, m_width, m_height,m_isMetric);
xOffset +=JFRuler.SIZE+1;
yOffset +=JFRuler.SIZE+1;
}
//draw grid
if (m_exportWithGrid && m_gridFormat!=null){
m_gridFormat.setSize(width,height);
drawGrid(g2,page.getZoomScale(),xOffset,yOffset,m_gridFormat);
}
//draw shapes.
drawGraph(page,g2,xOffset,yOffset);
}
/**
* Draw vertical and horizontal rulers on specified graphics context.
* @param page A page includes necessary information about rulers(e.g.scale information)
* @param g A specified graphics context.
* @param xOffset,yOffset x,y offsets of current ruler.
* @param width Full drawing width.
* @param height Full drawing height.
* @param isMetric If is a metric or english ruler.
*/
private static void drawRuler(JFPage page, Graphics g,double xOffset, double yOffset, int width, int height, boolean isMetric){
JFRuler ruler =new JFRuler();
Graphics2D g2 =(Graphics2D)g;
//draw vertical rulers
ruler.setOrientation(JFRuler.VERTICAL);
ruler.setIsMetric(isMetric);
ruler.setZoomScale(page.getZoomScale());
ruler.setHeight(height);
ruler.setScale(page.getScaleValue(),page.getScaleUnit(),page.getScreenUnit());
g2.translate(0,JFRuler.SIZE);
ruler.draw(g2,Color.white,Color.black,true);
g2.translate(0,-JFRuler.SIZE);
//draw horizontal rulers
ruler.setOrientation(JFRuler.HORIZONTAL);
ruler.setIsMetric(isMetric);
ruler.setZoomScale(page.getZoomScale());
ruler.setWidth(width);
ruler.setScale(page.getScaleValue(),page.getScaleUnit(),page.getScreenUnit());
g2.translate(JFRuler.SIZE,0);
ruler.draw(g2,Color.white,Color.black,true);
g2.translate(-JFRuler.SIZE,0);
}
/**
* Draw grids on specified graphics context.
* @param g A specified graphics context.
* @param zoomScale Zoom scale of current page.
* @param width Full drawing width.
* @param height Full drawing height.
*/
private static void drawGrid(Graphics g,double zoomScale, double xOffset, double yOffset,GridFormat gridFormat){
if ((int)xOffset==0 && (int)yOffset==0){
gridFormat.draw(g,zoomScale);
}else{
Graphics2D g2 =(Graphics2D)g;
g2.translate(xOffset,yOffset);
gridFormat.draw(g2,zoomScale);
g2.translate(-xOffset,-yOffset);
}
}
/**
* Draw graphs on specified graphics context.
* @param page A page to be drawn.
* @param g A specified graphics context.
*/
private static void drawGraph(JFPage page, Graphics g, double xOffset, double yOffset){
if ((int)xOffset==0 && (int)yOffset==0){
page.draw(g,false);
}else{
Graphics2D g2 =(Graphics2D)g;
g2.translate(xOffset,yOffset);
page.draw(g2,false);
g2.translate(-xOffset,-yOffset);
}
}
/**
* Draw current page to an internal buffered image.
* @return An image that generated.
*/
public BufferedImage getImage() {
prepareExport(m_usePageBounds);
// 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);
//clear 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;
}
/**
* calculate tiled pages, the rows and columns to divide the whole page.
*
* @return the rows and columns.
*/
private Dimension getTiledPages(){
//get exporting tiled page size.
Dimension exportSize =getTiledPageSize();
int exportWidth =(int)exportSize.getWidth();
int exportHeight =(int)exportSize.getHeight();
//actual page bounds
double zoomScale =m_page.getZoomScale();
Rect bounds =m_page.getBounds();
int pageWidth =(int)(bounds.getX() * zoomScale +bounds.getWidth() * zoomScale );
int pageHeight =(int)(bounds.getY() * zoomScale +bounds.getHeight() * zoomScale );
//ifi actual page bounds is smaller than the exporting size, return 1 page.
if (pageWidth<=exportWidth && pageHeight<=exportHeight){
return new Dimension(1,1);
}
//exporting grid, rows and columns. a residual decimal fraction that is greater than 0.05 will be treated as 1.
int cols =(int)(pageWidth * 1.0 / exportWidth + 0.95);
int rows =(int)(pageHeight * 1.0 / exportHeight + 0.95);
if (cols<=0) cols =1;
if (rows<=0) rows =1;
return new Dimension(cols,rows);
}
/**
* get tiled page size, in pixels.
* @return the size of tiled page size.
*/
private Dimension getTiledPageSize(){
JFPageFormat pageFormat =m_page.getPageFormat();
//exporting size
double zoomScale =m_page.getZoomScale();
int exportWidth =(int)(pageFormat.getPageFormat().getImageableWidth() * zoomScale);
int exportHeight=(int)(pageFormat.getPageFormat().getImageableHeight() * zoomScale);
if (exportWidth<=1) exportWidth=1;
if (exportHeight<=1) exportHeight=1;
return JFPageFormat.getScreenDimension(new Dimension(exportWidth,exportHeight));
}
/**
* get a specified tiled page's rectanglular area, in pixels.
*
* @param pageIndex A specified index of the tiled page.
* @return the rectanglular area of a tiled page.
*/
public Rect getTiledPageArea(int pageIndex){
//get tiled pages, the rows and columns
Dimension size =getTiledPages();
int rows =(int)size.getHeight();
int cols =(int)size.getWidth();
if (pageIndex<0)
pageIndex =0;
if (pageIndex>=rows*cols)
pageIndex =rows*cols-1;
//get a tiled page size
Dimension pageSize =getTiledPageSize();
int exportWidth =(int)pageSize.getWidth();
int exportHeight=(int)pageSize.getHeight();
//get the tiled page rectangular area.
int rowId =pageIndex / cols +1;
int colId =pageIndex % cols +1;
double x =(colId-1) * exportWidth;
double y =(rowId-1) * exportHeight;
return new Rect(x,y,exportWidth,exportHeight);
}
/**
* When drawing a page to a specified page format, we may need a tiled exporting method,
* to get multi pages exportation, otherwise we can not export the full page on a smaller
* page format.
*
* @return The tiled page amount.
*/
public int getTiledPageCount() {
Dimension size =getTiledPages();
return (int)size.getWidth() * (int)size.getHeight();
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?