1 /*
2  * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  *
26  * @bug 4276227
27  * @summary Checks that the PrinterGraphics is for a Printer GraphicsDevice.
28  * Test doesn't run unless there's a printer on the system.
29  * @author prr
30  * @run main/othervm PrinterDevice
31  */
32 
33 import java.awt.*;
34 import java.awt.geom.*;
35 import java.awt.print.*;
36 import java.io.*;
37 import javax.print.attribute.*;
38 import javax.print.attribute.standard.*;
39 
40 public class PrinterDevice implements Printable {
41 
main(String args[])42     public static void main(String args[]) throws PrinterException {
43         System.setProperty("java.awt.headless", "true");
44 
45         PrinterJob pj = PrinterJob.getPrinterJob();
46         if (pj.getPrintService() == null) {
47             return; /* Need a printer to run this test */
48         }
49 
50         PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
51         File f = new File("./out.prn");
52         f.deleteOnExit();
53         aset.add(new Destination(f.toURI()));
54         aset.add(OrientationRequested.LANDSCAPE);
55         pj.setPrintable(new PrinterDevice());
56         pj.print(aset);
57     }
58 
print(Graphics g, PageFormat pf, int pageIndex)59     public int print(Graphics g, PageFormat pf, int pageIndex) {
60          if (pageIndex > 0 ) {
61              return Printable.NO_SUCH_PAGE;
62          }
63 
64          /* Make sure calls to get DeviceConfig, its transforms,
65           * etc all work without exceptions and as expected */
66          Graphics2D g2 = (Graphics2D)g;
67          GraphicsConfiguration gConfig = g2.getDeviceConfiguration();
68          AffineTransform dt = gConfig.getDefaultTransform();
69          AffineTransform nt = gConfig.getNormalizingTransform();
70          AffineTransform gt = g2.getTransform();
71 
72          System.out.println("Graphics2D transform = " + gt);
73          System.out.println("Default transform = " + dt);
74          System.out.println("Normalizing transform = " + nt);
75 
76          Rectangle bounds = gConfig.getBounds();
77          System.out.println("Bounds = " + bounds);
78          if (!nt.isIdentity()) {
79              throw new RuntimeException("Expected Identity transdform");
80          }
81 
82          /* Make sure that device really is TYPE_PRINTER */
83          GraphicsDevice gd = gConfig.getDevice();
84          System.out.println("Printer Device ID = " + gd.getIDstring());
85          if (!(gd.getType() == GraphicsDevice.TYPE_PRINTER)) {
86              throw new RuntimeException("Expected printer device");
87          }
88          System.out.println(" *** ");
89          System.out.println("");
90          return Printable.PAGE_EXISTS;
91     }
92 }
93