001: /*
002: * $RCSfile: ImagePrinter.java,v $
003: *
004: * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * - Redistribution of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * - Redistribution in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * Neither the name of Sun Microsystems, Inc. or the names of
019: * contributors may be used to endorse or promote products derived
020: * from this software without specific prior written permission.
021: *
022: * This software is provided "AS IS," without a warranty of any
023: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
024: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
025: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
026: * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
027: * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
028: * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
029: * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
030: * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
031: * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
032: * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
033: * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
034: * POSSIBILITY OF SUCH DAMAGES.
035: *
036: * You acknowledge that this software is not designed, licensed or
037: * intended for use in the design, construction, operation or
038: * maintenance of any nuclear facility.
039: *
040: * $Revision: 1.2 $
041: * $Date: 2007/02/09 17:21:50 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.print_canvas3d;
046:
047: import java.awt.print.*;
048: import java.awt.Graphics;
049: import java.awt.Graphics2D;
050: import java.awt.Image;
051: import java.awt.image.BufferedImage;
052: import java.awt.image.ImageObserver;
053: import java.awt.geom.AffineTransform;
054:
055: class ImagePrinter implements Printable, ImageObserver {
056: BufferedImage bImage;
057:
058: public int print(Graphics g, PageFormat pf, int pi)
059: throws PrinterException {
060:
061: if (pi >= 1) {
062: return Printable.NO_SUCH_PAGE;
063: }
064:
065: Graphics2D g2d = (Graphics2D) g;
066: //g2d.translate(pf.getImageableX(), pf.getImageableY());
067: AffineTransform t2d = new AffineTransform();
068: t2d.translate(pf.getImageableX(), pf.getImageableY());
069: double xscale = pf.getImageableWidth()
070: / (double) bImage.getWidth();
071: double yscale = pf.getImageableHeight()
072: / (double) bImage.getHeight();
073: double scale = Math.min(xscale, yscale);
074: t2d.scale(scale, scale);
075: try {
076: g2d.drawImage(bImage, t2d, this );
077: } catch (Exception ex) {
078: ex.printStackTrace();
079: return Printable.NO_SUCH_PAGE;
080: }
081: return Printable.PAGE_EXISTS;
082: }
083:
084: void print() {
085: PrinterJob printJob = PrinterJob.getPrinterJob();
086: PageFormat pageFormat = printJob.defaultPage();
087: pageFormat.setOrientation(PageFormat.LANDSCAPE);
088: pageFormat = printJob.validatePage(pageFormat);
089: printJob.setPrintable(this , pageFormat);
090: if (printJob.printDialog()) {
091: try {
092: printJob.print();
093: } catch (PrinterException ex) {
094: ex.printStackTrace();
095: }
096: }
097: }
098:
099: public boolean imageUpdate(Image img, int infoflags, int x, int y,
100: int width, int height) {
101: return false;
102: }
103:
104: ImagePrinter(BufferedImage bImage) {
105: this.bImage = bImage;
106: }
107: }
|