下面列出了java.awt.print.PageFormat#getImageableWidth ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public int print(Graphics g, PageFormat format, int page)
throws PrinterException
{
if (page >= 1)
{
return Printable.NO_SUCH_PAGE;
}
double width = getSize().width;
double height = getSize().height;
double pageWidth = format.getImageableWidth();
double pageHeight = format.getImageableHeight();
double scale = 1;
if (width >= pageWidth)
scale = pageWidth / width;
double xSpace = (pageWidth - width * scale) / 2;
double ySpace = (pageHeight - height * scale) / 2;
Graphics2D g2d = (Graphics2D)g;
g2d.translate(format.getImageableX() + xSpace,
format.getImageableY() + ySpace);
g2d.scale(scale, scale);
print(g2d);
return Printable.PAGE_EXISTS;
}
/**
* This describes a <code>PageFormat</code> as a String. This is provided as
* a debugging tool, because <code>PageFormat.toString()</code> doesn't
* support this itself.
*/
public static String toString(PageFormat f) {
if (f == null)
return "null";
String orientation;
if (f.getOrientation() == PageFormat.LANDSCAPE) {
orientation = "LANDSCAPE";
} else if (f.getOrientation() == PageFormat.PORTRAIT) {
orientation = "PORTRAIT";
} else if (f.getOrientation() == PageFormat.REVERSE_LANDSCAPE) {
orientation = "REVERSE_LANDSCAPE";
} else {
orientation = "UNKNOWN";
}
return ("PageFormat[ " + f.getWidth() + "x" + f.getHeight()
+ " imageable=(" + f.getImageableX() + ", " + f.getImageableY()
+ ", " + f.getImageableWidth() + ", " + f.getImageableHeight()
+ ") orientation=" + orientation + "]");
}
/**
* Prints the chart on a single page.
*
* @param g the graphics context.
* @param pf the page format to use.
* @param pageIndex the index of the page. If not <code>0</code>, nothing
* gets print.
*
* @return The result of printing.
*/
@Override
public int print(Graphics g, PageFormat pf, int pageIndex) {
if (pageIndex != 0) {
return NO_SUCH_PAGE;
}
Graphics2D g2 = (Graphics2D) g;
double x = pf.getImageableX();
double y = pf.getImageableY();
double w = pf.getImageableWidth();
double h = pf.getImageableHeight();
this.chart.draw(g2, new Rectangle2D.Double(x, y, w, h), this.anchor,
null);
return PAGE_EXISTS;
}
/**
* Handles the page format.
*
* @param atts
* the attributes.
* @throws SAXException
* if a parser error occurs or the validation failed.
* @noinspection SuspiciousNameCombination
*/
private PageFormat configurePageSizeAndMargins( final Attributes atts, PageFormat format ) throws SAXException {
// (1) Grab the existing default ...
float defTopMargin = (float) format.getImageableY();
float defBottomMargin = (float) ( format.getHeight() - format.getImageableHeight() - format.getImageableY() );
float defLeftMargin = (float) format.getImageableX();
float defRightMargin = (float) ( format.getWidth() - format.getImageableWidth() - format.getImageableX() );
// (2) Now configure the new paper-size
format = configurePageSize( format, atts );
// (3) Reconfigure margins as requested
defTopMargin = ParserUtil.parseFloat( atts.getValue( getUri(), "margin-top" ), defTopMargin );
defBottomMargin = ParserUtil.parseFloat( atts.getValue( getUri(), "margin-bottom" ), defBottomMargin );
defLeftMargin = ParserUtil.parseFloat( atts.getValue( getUri(), "margin-left" ), defLeftMargin );
defRightMargin = ParserUtil.parseFloat( atts.getValue( getUri(), "margin-right" ), defRightMargin );
final Paper p = format.getPaper();
switch ( format.getOrientation() ) {
case PageFormat.PORTRAIT:
PageFormatFactory.getInstance().setBorders( p, defTopMargin, defLeftMargin, defBottomMargin, defRightMargin );
break;
case PageFormat.REVERSE_LANDSCAPE:
PageFormatFactory.getInstance().setBorders( p, defLeftMargin, defBottomMargin, defRightMargin, defTopMargin );
break;
case PageFormat.LANDSCAPE:
PageFormatFactory.getInstance().setBorders( p, defRightMargin, defTopMargin, defLeftMargin, defBottomMargin );
break;
default:
// will not happen..
throw new IllegalArgumentException( "Unexpected paper orientation." );
}
format.setPaper( p );
return format;
}
public int print(Graphics g, PageFormat pf, int index) {
if (index > 0 || image == null) {
return Printable.NO_SUCH_PAGE;
}
((Graphics2D)g).translate(pf.getImageableX(), pf.getImageableY());
int w = image.getWidth(null);
int h = image.getHeight(null);
int iw = (int)pf.getImageableWidth();
int ih = (int)pf.getImageableHeight();
// ensure image will fit
int dw = w;
int dh = h;
if (dw > iw) {
dh = (int)(dh * ( (float) iw / (float) dw)) ;
dw = iw;
}
if (dh > ih) {
dw = (int)(dw * ( (float) ih / (float) dh)) ;
dh = ih;
}
// centre on page
int dx = (iw - dw) / 2;
int dy = (ih - dh) / 2;
g.drawImage(image, dx, dy, dx+dw, dy+dh, 0, 0, w, h, null);
return Printable.PAGE_EXISTS;
}
public int print(Graphics g, PageFormat pf, int index) {
if (index > 0 || image == null) {
return Printable.NO_SUCH_PAGE;
}
((Graphics2D)g).translate(pf.getImageableX(), pf.getImageableY());
int w = image.getWidth(null);
int h = image.getHeight(null);
int iw = (int)pf.getImageableWidth();
int ih = (int)pf.getImageableHeight();
// ensure image will fit
int dw = w;
int dh = h;
if (dw > iw) {
dh = (int)(dh * ( (float) iw / (float) dw)) ;
dw = iw;
}
if (dh > ih) {
dw = (int)(dw * ( (float) ih / (float) dh)) ;
dh = ih;
}
// centre on page
int dx = (iw - dw) / 2;
int dy = (ih - dh) / 2;
g.drawImage(image, dx, dy, dx+dw, dy+dh, 0, 0, w, h, null);
return Printable.PAGE_EXISTS;
}
public static void updateRuleForPage( final CSSPageRule rule,
final PageFormat format ) {
if ( format == null ) {
rule.removeProperty( BoxStyleKeys.MARGIN_TOP );
rule.removeProperty( BoxStyleKeys.MARGIN_LEFT );
rule.removeProperty( BoxStyleKeys.MARGIN_BOTTOM );
rule.removeProperty( BoxStyleKeys.MARGIN_RIGHT );
rule.removeProperty( PageStyleKeys.SIZE );
// rule.removeProperty(PageStyleKeys.HORIZONTAL_PAGE_SPAN);
// rule.removeProperty(PageStyleKeys.VERTICAL_PAGE_SPAN);
return;
}
final double width = format.getWidth();
final double height = format.getHeight();
rule.setPropertyValueAsString( PageStyleKeys.SIZE,
width + "pt " + height + "pt" );
rule.setPropertyValueAsString( BoxStyleKeys.MARGIN_TOP, format.getImageableY() + "pt" );
rule.setPropertyValueAsString( BoxStyleKeys.MARGIN_LEFT, format.getImageableX() + "pt" );
final double marginRight = width - format.getImageableX() - format.getImageableWidth();
final double marginBottom = height - format.getImageableY() - format.getImageableHeight();
rule.setPropertyValueAsString( BoxStyleKeys.MARGIN_BOTTOM, marginBottom + "pt" );
rule.setPropertyValueAsString( BoxStyleKeys.MARGIN_RIGHT, marginRight + "pt" );
// rule.setPropertyValueAsString(PageStyleKeys.HORIZONTAL_PAGE_SPAN, "1");
// rule.setPropertyValueAsString(PageStyleKeys.VERTICAL_PAGE_SPAN, "1");
}
public static int printComponent(Graphics g, PageFormat pf, int page, Component component) {
// We have only one page, and 'page'
// is zero-based
if (page > 0) {
return Printable.NO_SUCH_PAGE;
}
Graphics2D g2d = (Graphics2D)g;
// User (0,0) is typically outside the
// imageable area, so we must translate
// by the X and Y values in the PageFormat
// to avoid clipping.
g2d.translate(pf.getImageableX(), pf.getImageableY());
//Scale component image to one page size
double scaleX = pf.getImageableWidth() / component.getWidth();
double scaleY = pf.getImageableHeight() / component.getHeight();
double scale = Math.min(scaleX, scaleY);
g2d.scale(scale, scale);
component.paintAll(g);
// tell the caller that this page is part
// of the printed document
return Printable.PAGE_EXISTS;
}
/**
* Adds a new page format to the page definition.
*
* @param format
* the page format
* @param x
* the x-position to where the imageable-x of the pageformat is mapped.
* @param y
* the y-position to where the imageable-y of the pageformat is mapped.
*/
public void addPageFormat( final PageFormat format, final float x, final float y ) {
if ( format == null ) {
throw new NullPointerException( "The given pageformat must not be null." );
}
width = Math.max( width, (float) ( format.getImageableWidth() + x ) );
height = Math.max( height, (float) ( format.getImageableHeight() + y ) );
final Rectangle2D bounds = new Rectangle2D.Double( x, y, format.getImageableWidth(), format.getImageableHeight() );
pageBoundsList.add( bounds );
pageFormatList.add( format.clone() );
}
/**
* Creates a new SimplePageDefinition object.
*
* @param format
* the pageformat used as base.
* @param x
* the number of physical pages in a row.
* @param y
* the number of physical pages in a column.
*/
public SimplePageDefinition( final PageFormat format, final int x, final int y ) {
if ( format == null ) {
throw new NullPointerException( "Format must not be null" );
}
if ( x < 1 ) {
throw new IllegalArgumentException( "PageCount must be greater or equal to 1" );
}
if ( y < 1 ) {
throw new IllegalArgumentException( "PageCount must be greater or equal to 1" );
}
this.format = (PageFormat) format.clone();
this.pageCountHorizontal = x;
this.pageCountVertical = y;
this.pagePositions = new Rectangle2D[pageCountHorizontal * pageCountVertical];
final float width = (float) format.getImageableWidth();
final float height = (float) format.getImageableHeight();
float pageStartY = 0;
for ( int vert = 0; vert < pageCountVertical; vert++ ) {
float pageStartX = 0;
for ( int hor = 0; hor < pageCountHorizontal; hor++ ) {
final Rectangle2D rect = new Rectangle2D.Float( pageStartX, pageStartY, width, height );
pagePositions[vert * pageCountHorizontal + hor] = rect;
pageStartX += width;
}
pageStartY += height;
}
}
/**
* Adjusts the scaling factors in both the horizontal and vertical directions
* to garuntee that the Scene prints onto a single page.
* @param useSymmetricScaling if true, the horizontal and vertical scaling
* factors will be the same whereby preserving the current aspect ration. The
* smallest of the two (horizontal and vertical) scaling factors is used for
* both.
*/
private void scaleToFit(boolean useSymmetricScaling) {
PageFormat format = getPageFormat();
Rectangle componentBounds = scene.getView().getBounds();
if (componentBounds.width * componentBounds.height == 0) {
return;
}
double scaleX = format.getImageableWidth() / componentBounds.width;
double scaleY = format.getImageableHeight() / componentBounds.height;
if (scaleX < 1 || scaleY < 1) {
if (useSymmetricScaling) {
if (scaleX < scaleY) {
scaleY = scaleX;
} else {
scaleX = scaleY;
}
}
setSize((float) (componentBounds.width * scaleX), (float) (componentBounds.height * scaleY));
setScaledSize(scaleX, scaleY);
}
}
/**
* Print the graph associated with this viewer.
*
* @param gc0 the graphics context.
* @param format page format
* @param pagenumber page index
*/
public int print(Graphics gc0, PageFormat format, int pagenumber) throws PrinterException {
JPanel panel = getPanel();
if (panel != null && pagenumber == 0) {
if (panel instanceof GraphView) {
return ((GraphView) panel).print(gc0, format, pagenumber);
} else {
Graphics2D gc = ((Graphics2D) gc0);
Dimension dim = panel.getSize();
int image_w = dim.width;
int image_h = dim.height;
double paper_x = format.getImageableX() + 1;
double paper_y = format.getImageableY() + 1;
double paper_w = format.getImageableWidth() - 2;
double paper_h = format.getImageableHeight() - 2;
double scale_x = paper_w / image_w;
double scale_y = paper_h / image_h;
double scale = Math.min(scale_x, scale_y);
double shift_x = paper_x + (paper_w - scale * image_w) / 2.0;
double shift_y = paper_y + (paper_h - scale * image_h) / 2.0;
gc.translate(shift_x, shift_y);
gc.scale(scale, scale);
panel.print(gc0);
return Printable.PAGE_EXISTS;
}
}
return Printable.NO_SUCH_PAGE;
}
public int print(Graphics g, PageFormat pf, int index) {
if (index > 0 || image == null) {
return Printable.NO_SUCH_PAGE;
}
((Graphics2D)g).translate(pf.getImageableX(), pf.getImageableY());
int w = image.getWidth(null);
int h = image.getHeight(null);
int iw = (int)pf.getImageableWidth();
int ih = (int)pf.getImageableHeight();
// ensure image will fit
int dw = w;
int dh = h;
if (dw > iw) {
dh = (int)(dh * ( (float) iw / (float) dw)) ;
dw = iw;
}
if (dh > ih) {
dw = (int)(dw * ( (float) ih / (float) dh)) ;
dh = ih;
}
// centre on page
int dx = (iw - dw) / 2;
int dy = (ih - dh) / 2;
g.drawImage(image, dx, dy, dx+dw, dy+dh, 0, 0, w, h, null);
return Printable.PAGE_EXISTS;
}
/**
* Print the frame associated with this viewer.
*
* @param gc0 the graphics context.
* @param format page format
* @param pagenumber page index
*/
public int print(Graphics gc0, PageFormat format, int pagenumber) throws PrinterException {
if (pagenumber == 0) {
Graphics2D gc = ((Graphics2D) gc0);
gc.setFont(getFont());
Dimension dim = getContentPane().getSize();
int image_w = dim.width;
int image_h = dim.height;
double paper_x = format.getImageableX() + 1;
double paper_y = format.getImageableY() + 1;
double paper_w = format.getImageableWidth() - 2;
double paper_h = format.getImageableHeight() - 2;
double scale_x = paper_w / image_w;
double scale_y = paper_h / image_h;
double scale = Math.min(scale_x, scale_y);
double shift_x = paper_x + (paper_w - scale * image_w) / 2.0;
double shift_y = paper_y + (paper_h - scale * image_h) / 2.0;
gc.translate(shift_x, shift_y);
gc.scale(scale, scale);
gc.setStroke(new BasicStroke(1.0f));
gc.setColor(Color.BLACK);
getContentPane().paint(gc);
return Printable.PAGE_EXISTS;
} else
return Printable.NO_SUCH_PAGE;
}
public int print(Graphics g, PageFormat pf, int index) {
if (index > 0 || image == null) {
return Printable.NO_SUCH_PAGE;
}
((Graphics2D)g).translate(pf.getImageableX(), pf.getImageableY());
int w = image.getWidth(null);
int h = image.getHeight(null);
int iw = (int)pf.getImageableWidth();
int ih = (int)pf.getImageableHeight();
// ensure image will fit
int dw = w;
int dh = h;
if (dw > iw) {
dh = (int)(dh * ( (float) iw / (float) dw)) ;
dw = iw;
}
if (dh > ih) {
dw = (int)(dw * ( (float) ih / (float) dh)) ;
dh = ih;
}
// centre on page
int dx = (iw - dw) / 2;
int dy = (ih - dh) / 2;
g.drawImage(image, dx, dy, dx+dw, dy+dh, 0, 0, w, h, null);
return Printable.PAGE_EXISTS;
}
public int print(Graphics g, PageFormat pf, int index) {
if (index > 0 || image == null) {
return Printable.NO_SUCH_PAGE;
}
((Graphics2D)g).translate(pf.getImageableX(), pf.getImageableY());
int w = image.getWidth(null);
int h = image.getHeight(null);
int iw = (int)pf.getImageableWidth();
int ih = (int)pf.getImageableHeight();
// ensure image will fit
int dw = w;
int dh = h;
if (dw > iw) {
dh = (int)(dh * ( (float) iw / (float) dw)) ;
dw = iw;
}
if (dh > ih) {
dw = (int)(dw * ( (float) ih / (float) dh)) ;
dh = ih;
}
// centre on page
int dx = (iw - dw) / 2;
int dy = (ih - dh) / 2;
g.drawImage(image, dx, dy, dx+dw, dy+dh, 0, 0, w, h, null);
return Printable.PAGE_EXISTS;
}
public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
if (pageIndex > 0) {
return(NO_SUCH_PAGE);
} else {
Graphics2D g2d = (Graphics2D)g;
double x0 = pageFormat.getImageableX();
double y0 = pageFormat.getImageableY();
double w0 = pageFormat.getImageableWidth();
double h0 = pageFormat.getImageableHeight();
double w1 = getWidth();
double h1 = getHeight();
double scale;
if (w0 / w1 < h0 / h1) {
scale = w0 / w1;
} else {
scale = h0 /h1;
}
g2d.translate(x0, y0);
g2d.scale(scale, scale);
// Turn off double buffering
paint(g2d);
// Turn double buffering back on
return(PAGE_EXISTS);
}
}
/**
* Method: print
* <p>
*
* @param g
* a value of type Graphics
* @param pageFormat
* a value of type PageFormat
* @param page
* a value of type int
* @return a value of type int
*/
public int print(Graphics g, PageFormat pageFormat, int page) {
//--- Create the Graphics2D object
Graphics2D g2d = (Graphics2D) g;
//--- Translate the origin to 0,0 for the top left corner
g2d.translate(pageFormat.getImageableX(), pageFormat
.getImageableY());
//--- Set the default drawing color to black
g2d.setPaint(Color.black);
//--- Draw a border arround the page
Rectangle2D.Double border = new Rectangle2D.Double(0, 0, pageFormat
.getImageableWidth(), pageFormat.getImageableHeight());
g2d.draw(border);
//--- Print the title
String titleText = "Printing in Java Part 2";
Font titleFont = new Font("helvetica", Font.BOLD, 36);
g2d.setFont(titleFont);
//--- Compute the horizontal center of the page
FontMetrics fontMetrics = g2d.getFontMetrics();
double titleX = (pageFormat.getImageableWidth() / 2)
- (fontMetrics.stringWidth(titleText) / 2);
double titleY = 3 * POINTS_PER_INCH;
g2d.drawString(titleText, (int) titleX, (int) titleY);
return (PAGE_EXISTS);
}
/**
* Prints the specified page on the specified graphics using
* <code>pageFormat</code> for the page format.
*
* @param g
* The graphics to paint the graph on.
* @param printFormat
* The page format to use for printing.
* @param page
* The page to print
* @return Returns {@link Printable#PAGE_EXISTS} or
* {@link Printable#NO_SUCH_PAGE}.
*/
public int print(Graphics g, PageFormat printFormat, int page)
{
int result = NO_SUCH_PAGE;
// Disables double-buffering before printing
RepaintManager currentManager = RepaintManager
.currentManager(mxGraphComponent.this);
currentManager.setDoubleBufferingEnabled(false);
// Gets the current state of the view
mxGraphView view = graph.getView();
// Stores the old state of the view
boolean eventsEnabled = view.isEventsEnabled();
mxPoint translate = view.getTranslate();
// Disables firing of scale events so that there is no
// repaint or update of the original graph while pages
// are being printed
view.setEventsEnabled(false);
// Uses the view to create temporary cell states for each cell
mxTemporaryCellStates tempStates = new mxTemporaryCellStates(view,
1 / pageScale);
try
{
view.setTranslate(new mxPoint(0, 0));
mxGraphics2DCanvas canvas = createCanvas();
canvas.setGraphics((Graphics2D) g);
canvas.setScale(1 / pageScale);
view.revalidate();
mxRectangle graphBounds = graph.getGraphBounds();
Dimension pSize = new Dimension((int) Math.ceil(graphBounds.getX()
+ graphBounds.getWidth()) + 1, (int) Math.ceil(graphBounds
.getY() + graphBounds.getHeight()) + 1);
int w = (int) (printFormat.getImageableWidth());
int h = (int) (printFormat.getImageableHeight());
int cols = (int) Math.max(
Math.ceil((double) (pSize.width - 5) / (double) w), 1);
int rows = (int) Math.max(
Math.ceil((double) (pSize.height - 5) / (double) h), 1);
if (page < cols * rows)
{
int dx = (int) ((page % cols) * printFormat.getImageableWidth());
int dy = (int) (Math.floor(page / cols) * printFormat
.getImageableHeight());
g.translate(-dx + (int) printFormat.getImageableX(), -dy
+ (int) printFormat.getImageableY());
g.setClip(dx, dy, (int) (dx + printFormat.getWidth()),
(int) (dy + printFormat.getHeight()));
graph.drawGraph(canvas);
result = PAGE_EXISTS;
}
}
finally
{
view.setTranslate(translate);
tempStates.destroy();
view.setEventsEnabled(eventsEnabled);
// Enables double-buffering after printing
currentManager.setDoubleBufferingEnabled(true);
}
return result;
}
@Override
@SuppressWarnings("unchecked")
public void paint(@Nonnull final Graphics g) {
final Graphics2D gfx = (Graphics2D) g;
gfx.setColor(parent.isDarkTheme() ? Color.DARK_GRAY : Color.LIGHT_GRAY);
final Dimension size = getSize();
gfx.fillRect(0, 0, size.width, size.height);
final double scale = this.parent.getScale();
final PageFormat thePageFormat = this.parent.getPageFormat();
final PrintPage[][] allPages = this.parent.getPages();
final double PAGE_WIDTH = thePageFormat.getWidth();
final double PAGE_HEIGHT = thePageFormat.getHeight();
final double AREA_WIDTH = thePageFormat.getImageableWidth();
final double AREA_HEIGHT = thePageFormat.getImageableHeight();
final Rectangle2D pageBack = new Rectangle2D.Double(0.0d, 0.0d, PAGE_WIDTH, PAGE_HEIGHT);
final Rectangle2D pageArea = new Rectangle2D.Double(0.0d, 0.0d, AREA_WIDTH, AREA_HEIGHT);
final Color SHADOW = new Color(0, 0, 0, 0x50);
int y = INTERVAL_Y;
final double AREA_X = thePageFormat.getImageableX();
final double AREA_Y = thePageFormat.getImageableY();
final boolean drawBorder = this.parent.isDrawBorder();
gfx.scale(scale, scale);
for (final PrintPage[] pages : allPages) {
int x = INTERVAL_X;
for (final PrintPage p : pages) {
gfx.translate(x, y);
gfx.setColor(SHADOW);
pageBack.setRect(SHADOW_X, SHADOW_Y, pageBack.getWidth(), pageBack.getHeight());
gfx.fill(pageBack);
gfx.setColor(Color.WHITE);
pageBack.setRect(0.0d, 0.0d, pageBack.getWidth(), pageBack.getHeight());
gfx.fill(pageBack);
gfx.translate(AREA_X, AREA_Y);
final Graphics2D gfxCopy = (Graphics2D) gfx.create();
gfxCopy.clip(pageArea);
p.print(gfxCopy);
gfxCopy.dispose();
if (drawBorder) {
final Stroke oldStroke = gfx.getStroke();
gfx.setColor(MMDPrintPanel.BORDER_COLOR);
gfx.setStroke(MMDPrintPanel.BORDER_STYLE);
gfx.draw(pageArea);
gfx.setStroke(oldStroke);
}
gfx.translate(-AREA_X, -AREA_Y);
gfx.translate(-x, -y);
x += INTERVAL_X + PAGE_WIDTH;
}
y += INTERVAL_Y + PAGE_HEIGHT;
}
gfx.scale(1.0d, 1.0d);
paintBorder(g);
}