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 @test 26 @bug 5024549 27 @summary Pass if dialogs are modal. 28 @run applet/manual PrintApplet.html 29*/ 30import java.awt.*; 31import java.awt.event.*; 32import java.applet.*; 33import java.awt.print.*; 34import javax.swing.*; 35 36public class PrintApplet extends JApplet implements Printable { 37 private JButton jButton1 = new JButton(); 38 39 40 public PrintApplet() { 41 } 42 43 public void init() { 44 try { 45 jbInit(); 46 } 47 catch(Exception e) { 48 e.printStackTrace(); 49 } 50 } 51 52 private void jbInit() throws Exception { 53 jButton1.setText("PRINT"); 54 jButton1.addActionListener(new java.awt.event.ActionListener() { 55 public void actionPerformed(ActionEvent e) { 56 jButton1_actionPerformed(e); 57 } 58 }); 59 jButton1.setBounds(new Rectangle(165, 248, 80, 30)); 60 this.setSize(new Dimension(400,300)); 61 this.getContentPane().setLayout(null); 62 this.getContentPane().setBackground(Color.pink); 63 this.getContentPane().add(jButton1, BorderLayout.SOUTH); 64 } 65 66 public void start() { 67 } 68 69 public void stop() { 70 } 71 72 public void destroy() { 73 } 74 75 public String getAppletInfo() { 76 return "Applet inf"; 77 } 78 79 public String[][] getParameterInfo() { 80 return null; 81 } 82 83 84 public int print(Graphics g, PageFormat pf, int page) throws PrinterException { 85 System.out.println("Calling print"); 86 if (page == 0) { 87 Graphics2D g2 = (Graphics2D)g; 88 g2.translate(pf.getImageableX(), pf.getImageableY()); 89 g2.setColor(Color.black); 90 g2.drawString("Hello World", 20, 100); 91 92 return Printable.PAGE_EXISTS; 93 } 94 return Printable.NO_SUCH_PAGE; 95 } 96 97 98 99 void jButton1_actionPerformed(ActionEvent e) { 100 PrinterJob printJob = null; 101 PageFormat pageFormat = null; 102 Paper prtPaper = null; 103 boolean bPrintFlg = true; 104 105 106 try{ 107 printJob = PrinterJob.getPrinterJob(); 108 109 } 110 catch(SecurityException se){ 111 112 bPrintFlg = false; 113 } 114 115 if (bPrintFlg) { 116 117 pageFormat = printJob.pageDialog(printJob.defaultPage()); 118 System.out.println("PrintApplet: pageFormat = "+pageFormat.getWidth()/72.0+" x "+pageFormat.getHeight()/72.0); 119 if (pageFormat != null) { 120 121 prtPaper = pageFormat.getPaper(); 122 pageFormat.setPaper(prtPaper); 123 124 125 printJob.setPrintable(this, pageFormat); 126 } 127 128 if (printJob.printDialog()) { 129 130 try { 131 printJob.print(); 132 } 133 catch (java.awt.print.PrinterException ex) { 134 ex.printStackTrace(); 135 } 136 137 } 138 139 } 140 } 141} 142