下面列出了java.awt.print.PageFormat#getImageableY ( ) 实例代码,或者点击链接到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;
}
/**
* 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 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;
}
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException
{
if (pageIndex>0)
{
return NO_SUCH_PAGE;
}
StringBuffer s=new StringBuffer();
for (int i=0;i<10;i++)
{
s.append("1234567890ABCDEFGHIJ");
}
int x=(int) pageFormat.getImageableX();
int y=(int) (pageFormat.getImageableY()+50);
graphics.drawString(s.toString(), x, y);
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.
*/
@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;
}
/**
* 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;
}
/**
* 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;
}
/**
* Handles the page format.
*
* @param atts
* the attributes.
* @throws SAXException
* if a parser error occurs or the validation failed.
* @noinspection SuspiciousNameCombination
*/
private void handlePageFormat( final Attributes atts ) throws SAXException {
final MasterReport report =
(MasterReport) getRootHandler().getHelperObject( ReportParserUtil.HELPER_OBJ_REPORT_NAME );
// grab the default page definition ...
PageFormat format = report.getPageDefinition().getPageFormat( 0 );
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() );
format = createPageFormat( format, atts );
defTopMargin = ParserUtil.parseFloat( atts.getValue( getUri(), PageReadHandler.TOPMARGIN_ATT ), defTopMargin );
defBottomMargin =
ParserUtil.parseFloat( atts.getValue( getUri(), PageReadHandler.BOTTOMMARGIN_ATT ), defBottomMargin );
defLeftMargin = ParserUtil.parseFloat( atts.getValue( getUri(), PageReadHandler.LEFTMARGIN_ATT ), defLeftMargin );
defRightMargin = ParserUtil.parseFloat( atts.getValue( getUri(), PageReadHandler.RIGHTMARGIN_ATT ), defRightMargin );
final Paper p = format.getPaper();
switch ( format.getOrientation() ) {
case PageFormat.PORTRAIT:
PageFormatFactory.getInstance().setBorders( p, defTopMargin, defLeftMargin, defBottomMargin, defRightMargin );
break;
case PageFormat.LANDSCAPE:
// right, top, left, bottom
PageFormatFactory.getInstance().setBorders( p, defRightMargin, defTopMargin, defLeftMargin, defBottomMargin );
break;
case PageFormat.REVERSE_LANDSCAPE:
PageFormatFactory.getInstance().setBorders( p, defLeftMargin, defBottomMargin, defRightMargin, defTopMargin );
break;
default:
// will not happen..
throw new IllegalArgumentException( "Unexpected paper orientation." );
}
format.setPaper( p );
pageFormat = format;
}
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 pf, int pi)
throws PrinterException {
if (pi != 0) {
return NO_SUCH_PAGE;
}
Graphics2D g2 = (Graphics2D) g;
g2.setFont(new Font("Serif", Font.PLAIN, 36));
g2.setPaint(Color.black);
g2.drawString("Java Source and Support", 100, 100);
Rectangle2D outline = new Rectangle2D.Double(pf.getImageableX(), pf
.getImageableY(), pf.getImageableWidth(), pf
.getImageableHeight());
g2.draw(outline);
return PAGE_EXISTS;
}
private String extractLink( final RenderNode node, final ReportMouseEvent event ) {
if ( node instanceof RenderableReplacedContentBox ) {
// process image map
final ImageMap imageMap = RenderUtility.extractImageMap( (RenderableReplacedContentBox) node );
if ( imageMap != null ) {
final PageDrawable physicalPageDrawable = drawablePanel.getPageDrawable();
final PageFormat pf = physicalPageDrawable.getPageFormat();
final float x1 = (float) ( event.getSourceEvent().getX() / zoom );
final float y1 = (float) ( event.getSourceEvent().getY() / zoom );
final float imageMapX = (float) ( x1 - pf.getImageableX() - StrictGeomUtility.toExternalValue( node.getX() ) );
final float imageMapY = (float) ( y1 - pf.getImageableY() - StrictGeomUtility.toExternalValue( node.getY() ) );
final ImageMapEntry[] imageMapEntries = imageMap.getEntriesForPoint( imageMapX, imageMapY );
for ( int i = 0; i < imageMapEntries.length; i++ ) {
final ImageMapEntry entry = imageMapEntries[i];
final Object imageMapTarget = entry.getAttribute( LibXmlInfo.XHTML_NAMESPACE, "href" );
if ( imageMapTarget != null ) {
return String.valueOf( imageMapTarget );
}
}
}
}
final String target = (String) node.getStyleSheet().getStyleProperty( ElementStyleKeys.HREF_TARGET );
if ( target == null ) {
return null;
}
return target;
}
/**
* 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;
}
/**
* print the window
*
* @param gc0
* @param format
* @param pagenumber
* @return
* @throws PrinterException
*/
public int print(Graphics gc0, PageFormat format, int pagenumber) throws PrinterException {
if (pagenumber == 0) {
Graphics2D gc = ((Graphics2D) gc0);
gc.setFont(getFont());
Dimension dim = swingPanel4FX.getPanel().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);
swingPanel4FX.getPanel().paint(gc);
return Printable.PAGE_EXISTS;
} else
return Printable.NO_SUCH_PAGE;
}
public int print(Graphics g, PageFormat pf, int page)
throws PrinterException {
if (page > 0) {
return NO_SUCH_PAGE;
}
int ix = (int)pf.getImageableX();
int iy = (int)pf.getImageableY();
int iw = (int)pf.getImageableWidth();
int ih = (int)pf.getImageableHeight();
System.out.println("ix="+ix+" iy="+iy+" iw="+iw+" ih="+ih);
if ((ix < 0) || (iy < 0)) {
throw new RuntimeException("Imageable x or y is a negative value.");
}
Paper paper = pf.getPaper();
int wid = (int)paper.getWidth();
int hgt = (int)paper.getHeight();
System.out.println("wid="+wid+" hgt="+hgt);
/*
* If imageable width/height is -ve, then print was done with 1" margin
* e.g. ix=72 iy=72 iw=451 ih=697 and paper wid=595
* but with fix, we get print with hardware margin e.g.
* ix=12, iy=12, iw=571, ih=817
*/
if ((wid - iw > 72) || (hgt - ih > 72)) {
throw new RuntimeException("Imageable width or height is negative value");
}
if ((ix+iw > wid) || (iy+ih > hgt)) {
throw new RuntimeException("Printable width or height "
+ "exceeds paper width or height.");
}
// runtime checking to see if the margins/printable area
// correspond to the entire size of the paper, for now, make it pass
// as for linux, the hwmargin is not taken into account - bug6574279
if (ix == 0 && iy == 0 && (ix+iw == wid) && (iy+ih == hgt)) {
return PAGE_EXISTS;
}
Graphics2D g2d = (Graphics2D)g;
g2d.translate(ix, iy);
g2d.setColor(Color.black);
g2d.drawRect(1, 1, iw-2, ih-2);
return PAGE_EXISTS;
}
@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);
}
/**
* Returns the string to be used as the tooltip for <i>event</i>. By default this returns any string set using
* <code>setToolTipText</code>. If a component provides more extensive API to support differing tooltips at different
* locations, this method should be overridden.
*/
public String getToolTipText( final MouseEvent event ) {
final PageBackgroundDrawable backgroundDrawable = getBackgroundDrawable();
if ( backgroundDrawable == null ) {
return null;
}
final PageDrawable physicalPageDrawable = getPageDrawable();
if ( physicalPageDrawable == null ) {
return null;
}
final float zoom = (float) backgroundDrawable.getZoom();
final float x1 = event.getX() / zoom;
final float y1 = event.getY() / zoom;
final RenderNode[] nodes = physicalPageDrawable.getNodesAt( x1, y1, null, null );
if ( nodes.length == 0 ) {
return null;
}
for ( int i = nodes.length - 1; i >= 0; i -= 1 ) {
final RenderNode node = nodes[i];
final ReportAttributeMap attributes = node.getAttributes();
final Object swingTooltip =
attributes.getAttribute( AttributeNames.Swing.NAMESPACE, AttributeNames.Swing.TOOLTIP );
if ( swingTooltip != null ) {
return String.valueOf( swingTooltip );
}
final Object htmlTooltip = attributes.getAttribute( AttributeNames.Html.NAMESPACE, AttributeNames.Html.TITLE );
if ( htmlTooltip != null ) {
return String.valueOf( htmlTooltip );
}
final Object styleTooltip = node.getStyleSheet().getStyleProperty( ElementStyleKeys.HREF_TITLE );
if ( styleTooltip != null ) {
return String.valueOf( styleTooltip );
}
final Object hrefTarget = node.getStyleSheet().getStyleProperty( ElementStyleKeys.HREF_TARGET );
if ( hrefTarget != null ) {
return String.valueOf( hrefTarget );
}
if ( node instanceof RenderableReplacedContentBox == false ) {
continue;
}
final ImageMap imageMap = RenderUtility.extractImageMap( (RenderableReplacedContentBox) node );
if ( imageMap == null ) {
continue;
}
final PageFormat pf = physicalPageDrawable.getPageFormat();
final float imageMapX = (float) ( x1 - pf.getImageableX() - StrictGeomUtility.toExternalValue( node.getX() ) );
final float imageMapY = (float) ( y1 - pf.getImageableY() - StrictGeomUtility.toExternalValue( node.getY() ) );
final ImageMapEntry[] imageMapEntries = imageMap.getEntriesForPoint( imageMapX, imageMapY );
for ( int j = 0; j < imageMapEntries.length; j++ ) {
final ImageMapEntry imageMapEntry = imageMapEntries[j];
final Object imageMapTooltip = imageMapEntry.getAttribute( LibXmlInfo.XHTML_NAMESPACE, "title" );
if ( imageMapTooltip != null ) {
return String.valueOf( imageMapTooltip );
}
final Object imageMapTarget = imageMapEntry.getAttribute( LibXmlInfo.XHTML_NAMESPACE, "href" );
if ( imageMapTarget != null ) {
return String.valueOf( imageMapTarget );
}
}
}
return null;
}