📄 g2outputtarget.java
字号:
/**
* ========================================
* JFreeReport : a free Java report library
* ========================================
*
* Project Info: http://www.jfree.org/jfreereport/index.html
* Project Lead: Thomas Morgner;
*
* (C) Copyright 2000-2003, by Simba Management Limited and Contributors.
*
* This library is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* -------------------
* G2OutputTarget.java
* -------------------
* (C)opyright 2000-2003, by Simba Management Limited and Contributors.
*
* Original Author: David Gilbert (for Simba Management Limited);
* Contributor(s): Thomas Morgner;
*
* $Id: G2OutputTarget.java,v 1.8.2.1 2003/12/21 23:28:45 taqua Exp $
*
* Changes
* -------
* 21-Feb-2002 : Version 1 (DG);
* 18-Apr-2002 : MultilineText is working again, ImageElement support
* 16-May-2002 : Interface of drawShape changed so we can draw different line width (JS)
* 08-Jun-2002 : Documentation
* 17-Jul-2002 : Fixed a nullpointer when an ImageReference did not contain a graphics
* 26-Aug-2002 : Fixed drawString: Text was placed too deep, Fontheight is defined MaxAscent,
* not font.getFontheight()!
* 10-Dec-2002 : Updated Javadocs (DG);
*
*/
package org.jfree.report.modules.output.pageable.graphics;
import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.AffineTransform;
import java.awt.geom.Dimension2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.awt.print.PageFormat;
import org.jfree.report.DrawableContainer;
import org.jfree.report.ImageReference;
import org.jfree.report.layout.DefaultSizeCalculator;
import org.jfree.report.layout.SizeCalculator;
import org.jfree.report.modules.output.pageable.base.LogicalPage;
import org.jfree.report.modules.output.pageable.base.OutputTarget;
import org.jfree.report.modules.output.pageable.base.OutputTargetException;
import org.jfree.report.modules.output.pageable.base.output.AbstractOutputTarget;
import org.jfree.report.modules.output.pageable.base.output.DummyOutputTarget;
import org.jfree.report.modules.output.pageable.base.physicals.PhysicalPage;
import org.jfree.report.style.FontDefinition;
import org.jfree.report.util.Log;
import org.jfree.report.util.ReportConfiguration;
import org.jfree.report.util.WaitingImageObserver;
/**
* A report output target that uses a Graphics2D object to draw the report. This allows reports
* to be printed on the screen and on the printer.
*
* @author David Gilbert
* @author Thomas Morgner
*/
public strictfp class G2OutputTarget extends AbstractOutputTarget
{
/** The graphics device. */
private Graphics2D g2;
/** The saved state of the Graphics2D device. */
private G2State savedState;
/** The current age. */
private PhysicalPage currentPage;
/** Temporary storage for the Graphics2D clip region. */
private Shape originalClip;
/** A dummy graphics2D. */
private static Graphics2D dummyGraphics;
/** The open flag. */
private boolean isOpen;
/** The current font definition for this outputtarget. */
private FontDefinition fontDefinition;
/**
* Creates an empty graphics by using a 1x1 pixel buffered image.
*
* @return a Graphics2D instance.
*/
public static Graphics2D createEmptyGraphics()
{
if (dummyGraphics == null)
{
final BufferedImage image = new BufferedImage(BufferedImage.TYPE_INT_ARGB, 1, 1);
dummyGraphics = image.createGraphics();
applyStandardRenderingHints(dummyGraphics);
}
return dummyGraphics;
}
/**
* Configures a Graphics2D object to use a predefined set of RenderingHints.
* This enables KEY_FRACTIONALMETRICS to value VALUE_FRACTIONALMETRICS_ON,
* KEY_ANTIALIASING to value VALUE_ANTIALIAS_OFF and sets the TextAliasing
* as defined by the DefaultFontRenderContext.
*
* @param g2 the graphics2D object that should be configured
*/
private static void applyStandardRenderingHints(final Graphics2D g2)
{
g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
RenderingHints.VALUE_FRACTIONALMETRICS_ON);
if (DefaultSizeCalculator.getFrcDetector().isAliased())
{
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
}
else
{
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
}
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_OFF);
}
/**
* A state of a Graphics2D object. This does not store clipping regions or advanced
* properties.
*/
private static final class G2State
{
/** The paint. */
private Paint mypaint;
/** The font. */
private FontDefinition myfont;
/** The stroke. */
private Stroke mystroke;
/** The transform. */
private AffineTransform mytransform;
/** The background. */
private Color mybackground;
/** The clip area. */
private Shape myclip;
/**
* Create a new state.
*
* @param s the graphics device.
*/
private G2State(final G2OutputTarget s)
{
save(s);
}
/**
* Saves the state of the Graphics2D.
*
* @param source the Graphics2D.
*/
protected void save(final G2OutputTarget source)
{
mypaint = source.getPaint();
myfont = source.getFont();
mystroke = source.getStroke();
final Graphics2D g2 = source.getGraphics2D();
mytransform = g2.getTransform();
mybackground = g2.getBackground();
myclip = g2.getClip();
}
/**
* Copies the state back to the specified Graphics2D.
*
* @param target the Graphics2D.
* @throws OutputTargetException if restoring the state failes.
*/
protected void restore(final G2OutputTarget target)
throws OutputTargetException
{
target.setStroke(mystroke);
target.setFont(myfont);
target.setPaint(mypaint);
final Graphics2D g2 = target.getGraphics2D();
g2.setTransform(mytransform);
g2.setBackground(mybackground);
g2.setClip(myclip);
}
}
/**
* Creates a new output target.
*
* @param page the logical page.
* @param graphics the graphics device.
*/
public G2OutputTarget(final LogicalPage page, final Graphics2D graphics)
{
super(page);
setGraphics2D(graphics);
}
/**
* Constructs an output target for drawing to a Java Graphics2D object.
*
* @param g2 the graphics device.
* @param physPageFormat the page format for the physical page used to print the report.
* @param logPageFormat the page format for the logical page used to layout the report.
*/
public G2OutputTarget(final Graphics2D g2, final PageFormat physPageFormat,
final PageFormat logPageFormat)
{
super(physPageFormat, logPageFormat);
setGraphics2D(g2);
}
/**
* Constructs an output target for drawing to a Java Graphics2D object.
*
* @param g2 the graphics device.
* @param pageFormat the page format.
*/
public G2OutputTarget(final Graphics2D g2, final PageFormat pageFormat)
{
this(g2, pageFormat, pageFormat);
}
/**
* Sets the graphics device for this output target.
*
* @param g2 the graphics device (null not permitted).
*/
private void setGraphics2D(final Graphics2D g2)
{
if (g2 == null)
{
throw new NullPointerException("Graphics must not be null");
}
this.g2 = (Graphics2D) g2.create();
applyStandardRenderingHints(this.g2);
}
/**
* Opens the target.
*
* @throws OutputTargetException if there is a problem with the output target.
*/
public void open() throws OutputTargetException
{
originalClip = g2.getClip();
setFont(new FontDefinition(g2.getFont().getName(), g2.getFont().getSize()));
isOpen = true;
}
/**
* Closes the target.
*/
public void close()
{
originalClip = null;
savedState = null;
isOpen = false;
}
/**
* Returns <code>true</code> if the output target is open, and <code>false</code> otherwise.
*
* @return the open state of this target.
*/
public boolean isOpen()
{
return isOpen;
}
/**
* A page is starting. This target saves the current state of the Graphics2D device.
*
* @param page the physical page.
*/
public void beginPage(final PhysicalPage page)
{
this.currentPage = page;
final Rectangle2D pageBounds = currentPage.getBounds();
final PageFormat currentPageFormat = page.getPageFormat();
final Rectangle2D bounds = new Rectangle2D.Float((float) currentPageFormat.getImageableX(),
(float) currentPageFormat.getImageableY(),
(float) currentPageFormat.getImageableWidth() + 1,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -