1/*
2 * Copyright (c) 1999, 2003, 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 * @test
25 * @bug 4257262 6708509
26 * @summary Image should be sent to printer.
27* @run main/manual PrintAWTImage
28 */
29
30import java.awt.*;
31import java.awt.event.*;
32import java.awt.print.*;
33
34
35public class PrintAWTImage extends Frame
36                           implements ActionListener, Printable {
37
38 public Image imgJava;
39
40
41 public static void main(String args[]) {
42    PrintAWTImage f = new PrintAWTImage();
43    f.show();
44 }
45
46 public PrintAWTImage() {
47
48    Button printButton = new Button("Print");
49    setLayout(new FlowLayout());
50    add(printButton);
51    printButton.addActionListener(this);
52
53    addWindowListener(new WindowAdapter() {
54       public void windowClosing(WindowEvent e) {
55             System.exit(0);
56            }
57    });
58
59    pack();
60 }
61
62 public void actionPerformed(ActionEvent e) {
63
64   PrinterJob pj = PrinterJob.getPrinterJob();
65
66   if (pj != null && pj.printDialog()) {
67       pj.setPrintable(this);
68       try {
69            pj.print();
70      } catch (PrinterException pe) {
71      } finally {
72         System.err.println("PRINT RETURNED");
73      }
74   }
75 }
76
77
78    public int print(Graphics g, PageFormat pgFmt, int pgIndex) {
79      if (pgIndex > 0)
80         return Printable.NO_SUCH_PAGE;
81
82      Graphics2D g2d = (Graphics2D)g;
83      g2d.translate(pgFmt.getImageableX(), pgFmt.getImageableY());
84      Image imgJava = Toolkit.getDefaultToolkit().getImage("duke.gif");
85      g2d.drawImage(imgJava, 0, 0, this);
86
87      return Printable.PAGE_EXISTS;
88    }
89
90}
91