下面列出了java.awt.print.PageFormat#getImageableX ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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;
}
public int print( Graphics graphics, PageFormat format, int index ) {
Graphics2D g2d = (Graphics2D)graphics;
double scalex = g2d.getTransform().getScaleX();
double scaley = g2d.getTransform().getScaleY();
double centerx = ( format.getImageableX() +
( format.getImageableWidth() / 2 ) ) * scalex;
double centery = ( format.getImageableY() +
( format.getImageableHeight() / 2 ) ) * scaley;
// The following 2 lines cause an error when printing in landscape.
g2d.scale( 1 / scalex, 1 / scaley );
g2d.translate( centerx, centery );
Path2D.Double path = new Path2D.Double();
path.moveTo( -scalex * 72, -scaley * 72 );
path.lineTo( -scalex * 72, scaley * 72 );
path.lineTo( scalex * 72, scaley * 72 );
path.lineTo( scalex * 72, -scaley * 72 );
path.closePath();
g2d.draw( path );
return index == 0 ? PAGE_EXISTS : NO_SUCH_PAGE;
}
/**
* 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;
}
/**
* 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.
*/
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;
}
/**
* 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;
}
/**
* 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;
}
/**
* 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.
*/
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;
}
public Insets getPageMargins( final PageFormat format ) {
final int marginLeft = (int) format.getImageableX();
final int marginRight = (int) ( format.getWidth() - format.getImageableWidth() - format.getImageableX() );
final int marginTop = (int) ( format.getImageableY() );
final int marginBottom = (int) ( format.getHeight() - format.getImageableHeight() - format.getImageableY() );
return new Insets( marginTop, marginLeft, marginBottom, marginRight );
}
public static void main(String args[]) throws Exception {
String[] instructions
= {
"Page Dialog will be shown.",
"Change top(in) margin value from 1.0 to 2.0",
"Then select OK."
};
SwingUtilities.invokeAndWait(() -> {
JOptionPane.showMessageDialog((Component) null,
instructions, "Instructions",
JOptionPane.INFORMATION_MESSAGE);
});
PrinterJob pj = PrinterJob.getPrinterJob();
try {
HashPrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
PageFormat pf;
pf = pj.pageDialog(aset);
double left = pf.getImageableX();
double top = pf.getImageableY();
System.out.println("pageDialog - left/top from pageFormat: " + left / 72
+ " " + top / 72);
System.out.println("pageDialog - left/top from attribute set: "
+ getPrintableXFromASet(aset) + " "
+ getPrintableYFromASet(aset));
if (top / 72 != 2.0f || getPrintableYFromASet(aset) != 2.0f) {
throw new RuntimeException("Top margin value not updated");
}
} catch (Exception e) {
e.printStackTrace();
}
}
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);
Color bg = getBackground();
setBackground(Color.white);
paint(g2d);
setBackground(bg);
return(PAGE_EXISTS);
}
}
public void draw( final Graphics2D graphics, final Rectangle2D bounds ) {
final PageFormat gpf = getPageFormat();
final Rectangle2D.Double imageableArea =
new Rectangle2D.Double( gpf.getImageableX(), gpf.getImageableY(), gpf.getImageableWidth(), gpf
.getImageableHeight() );
graphics.setPaint( new Color( 225, 225, 225 ) );
graphics.fill( imageableArea );
graphics.setPaint( Color.gray );
graphics.draw( imageableArea );
final int pcH = pageDefinition.getPageCountHorizontal();
final int pcW = pageDefinition.getPageCountVertical();
final Line2D line = new Line2D.Double();
for ( int splitH = 1; splitH < pcH; splitH += 1 ) {
final double xPos = gpf.getImageableX() + ( splitH * gpf.getImageableWidth() );
line.setLine( xPos, gpf.getImageableY(), xPos, gpf.getImageableY() + gpf.getImageableHeight() );
graphics.draw( line );
}
for ( int splitW = 1; splitW < pcW; splitW += 1 ) {
final double yPos = gpf.getImageableY() + ( splitW * gpf.getImageableHeight() );
line.setLine( gpf.getImageableX(), yPos, gpf.getImageableX() + gpf.getImageableWidth(), yPos );
graphics.draw( line );
}
}
/**
* 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 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");
}
/**
* 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;
}
/**
* 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 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);
}
}
private void setSize() {
int pageCount = printable.getPageCount();
rowCount = (pageCount - 1) / columnCount + 1;
pageWidth = 0;
pageHeight = 0;
pages = new Page[pageCount];
PageFormat pageFormat = printable.getPageFormat();
for (int i = 0; i < pageCount; i++) {
pageFormat = printable.getPageFormat(pageFormat, i);
double w = pageFormat.getWidth() + 1;
double h = pageFormat.getHeight() + 1;
double iW = pageFormat.getImageableWidth();
double iH = pageFormat.getImageableHeight();
double x = pageFormat.getImageableX();
double y = pageFormat.getImageableY();
reverce = (pageFormat.getOrientation() == PageFormat.REVERSE_LANDSCAPE);
/*
* if (pageFormat.getOrientation() == PageFormat.LANDSCAPE) { double
* t;
*
* t = w; w = h; h = t;
*
* t = iW; iW = iH; iH = t;
*
* t = x; x = y; y = t; }
*/
Page page = new Page(w, h, x, y, iW, iH);
if (pageWidth < w)
pageWidth = w;
if (pageHeight < h)
pageHeight = h;
pages[i] = page;
}
width = (columnCount - 1) * (pageWidth + W_SPACE / zoom) + pageWidth;
height = rowCount * (pageHeight + W_SPACE / zoom);
Dimension size = new Dimension((int) (width * getZoom()),
(int) (height * getZoom()));
this.setSize(size);
this.setPreferredSize(size);
}
@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);
}
/**
* Draws the object.
*
* @param graphics
* the graphics device.
* @param area
* the area inside which the object should be drawn.
*/
public strictfp void draw( final Graphics2D graphics, final Rectangle2D area ) {
if ( backend == null ) {
return;
}
final PageFormat pageFormat = backend.getPageFormat();
final float outerW = (float) pageFormat.getWidth();
final float outerH = (float) pageFormat.getHeight();
final float innerX = (float) pageFormat.getImageableX();
final float innerY = (float) pageFormat.getImageableY();
final float innerW = (float) pageFormat.getImageableWidth();
final float innerH = (float) pageFormat.getImageableHeight();
final Graphics2D g2 = (Graphics2D) graphics.create();
// double paperBorder = paperBorderPixel * zoomFactor;
/** Prepare background **/
g2.transform( AffineTransform.getScaleInstance( getZoom(), getZoom() ) );
g2.setRenderingHint( RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON );
/** Prepare background **/
final Rectangle2D pageArea = new Rectangle2D.Float( 0, 0, outerW, outerH );
/**
* The border around the printable area is painted when the corresponding property is set to true.
*/
final Rectangle2D printingArea = new Rectangle2D.Float( innerX, innerY, innerW, innerH );
/** Paint Page Shadow */
final Rectangle2D southborder = new Rectangle2D.Float( getShadowSize(), outerH, outerW, getShadowSize() );
final Rectangle2D eastborder = new Rectangle2D.Float( outerW, getShadowSize(), getShadowSize(), outerH );
g2.setPaint( UIManager.getColor( "controlShadow" ) ); //$NON-NLS-1$
g2.fill( southborder );
g2.fill( eastborder );
if ( isBorderPainted() ) {
g2.setPaint( Color.gray );
g2.draw( printingArea );
}
g2.setPaint( Color.white );
g2.fill( pageArea );
final Graphics2D g22 = (Graphics2D) g2.create();
backend.draw( g22, new Rectangle2D.Double( 0, 0, pageFormat.getWidth(), pageFormat.getHeight() ) );
g22.dispose();
final Rectangle2D transPageArea = new Rectangle2D.Float( 0, 0, outerW, outerH );
g2.setPaint( Color.black );
g2.draw( transPageArea );
g2.dispose();
}
/**
* 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.
*/
public int print(Graphics g, PageFormat pf, int pageIndex) {
if (pageIndex != 0) {
return NO_SUCH_PAGE;
}
/** this works but the curve is made of little pieces */
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);
if (printToPrinter) return PAGE_EXISTS;
// The rest should be moved up to the export eps action listener so it's not done per page'
// Show export dialog
JFileChooser fileChooser = new JFileChooser();
ExtensionFileFilter filter = new ExtensionFileFilter(
localizationResources.getString("PostScript_Files"), ".eps");
fileChooser.addChoosableFileFilter(filter);
String filename="";
int option = fileChooser.showSaveDialog(this);
if (option == JFileChooser.APPROVE_OPTION) {
filename = fileChooser.getSelectedFile().getPath();
if (isEnforceFileExtensions()) {
if (!filename.endsWith(".eps") || !filename.endsWith(".ps")) {
filename = filename + ".eps";
}
} else
return NO_SUCH_PAGE;
}
try {
OutputStream out = new java.io.FileOutputStream(new File(filename));
out = new java.io.BufferedOutputStream(out);
//Instantiate the EPSDocumentGraphics2D instance
EPSDocumentGraphics2D g2dps = new EPSDocumentGraphics2D(false);
g2dps.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext());
//Set up the document size
g2dps.setupDocument(out, (int)w, (int)h+200);
//Paint a bounding box
g2dps.drawRect((int)x, (int)y, (int)w, (int)h);
this.chart.draw(g2dps, new Rectangle2D.Double(x, y, w, h), this.anchor,
null);
//A few rectangles rotated and with different color
//Cleanup
g2dps.finish();
out.flush();
out.close();
}
catch(java.io.IOException e){
return NO_SUCH_PAGE;
}
return PAGE_EXISTS;
}