11/*
2- * Copyright (c) 2006, 2011 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2006, 2021 , Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
2121 * questions.
2222 */
2323
24- /**
25- *
24+ /*
2625 * @test
2726 * @bug 4521945 7006865
2827 * @summary Test printing images of different types.
2928 * @author prr
30- * @run main/manual=yesno/timeout=900 ImageTypes
29+ * @run main/manual ImageTypes
3130 */
3231
33- import java .io .*;
32+ import java .awt .Button ;
33+ import java .awt .Color ;
34+ import java .awt .Component ;
35+ import java .awt .Dimension ;
36+ import java .awt .Font ;
37+ import java .awt .FontMetrics ;
38+ import java .awt .Frame ;
39+ import java .awt .GradientPaint ;
40+ import java .awt .Graphics ;
41+ import java .awt .Graphics2D ;
42+ import java .awt .GridLayout ;
43+ import java .awt .Panel ;
44+ import java .awt .TextArea ;
45+ import java .awt .event .ActionEvent ;
46+ import java .awt .event .ActionListener ;
47+ import java .awt .image .BufferedImage ;
48+ import java .awt .image .DataBuffer ;
49+ import java .awt .image .IndexColorModel ;
50+ import java .awt .print .PageFormat ;
51+ import java .awt .print .Printable ;
52+ import java .awt .print .PrinterException ;
53+ import java .awt .print .PrinterJob ;
54+ import java .util .concurrent .CountDownLatch ;
55+ import java .util .concurrent .TimeUnit ;
56+ import javax .print .attribute .HashPrintRequestAttributeSet ;
57+ import javax .print .attribute .PrintRequestAttributeSet ;
58+
3459import static java .awt .Color .*;
35- import java .awt .*;
36- import java .awt .geom .*;
37- import java .awt .event .*;
38- import java .awt .print .*;
39- import java .awt .image .*;
4060import static java .awt .image .BufferedImage .*;
41- import javax .print .*;
42- import javax .print .attribute .*;
43- import javax .print .attribute .standard .*;
4461
45- public class ImageTypes extends Frame implements ActionListener {
62+ public class ImageTypes {
4663
47- private ImageCanvas c ;
64+ private static Frame testFrame ;
65+ private static ImageCanvas imageCanvas ;
66+ private static volatile boolean testResult ;
67+ private static final CountDownLatch countDownLatch = new CountDownLatch (1 );
4868
49- public static void main (String args []) {
50-
51- ImageTypes f = new ImageTypes ();
52- f .show ();
69+ public static void main (String [] args ) throws InterruptedException {
70+ createTestUI ();
71+ if (!countDownLatch .await (10 , TimeUnit .MINUTES )) {
72+ throw new RuntimeException ("Timeout : No action was performed on the test UI." );
73+ }
74+ if (!testResult ) {
75+ throw new RuntimeException ("Test failed!" );
76+ }
5377 }
5478
55- public ImageTypes () {
56- super ("Image Types Printing Test" );
57- c = new ImageCanvas ();
58- add ("Center" , c );
59-
60- Button printThisButton = new Button ("Print" );
61- printThisButton .addActionListener (this );
62- Panel p = new Panel ();
63- p . add ( printThisButton );
64- add ( "South" , p );
65- add ( "North" , getInstructions ());
66- addWindowListener ( new WindowAdapter () {
67- public void windowClosing ( WindowEvent e ) {
68- System . exit ( 0 ) ;
79+ public static void createTestUI () {
80+ testFrame = new Frame ("Image Types Printing Test" );
81+ imageCanvas = new ImageCanvas ();
82+ testFrame . add ("Center" , imageCanvas );
83+
84+ Button printButton = new Button ("Print" );
85+ printButton .addActionListener (new ActionListener () {
86+ @ Override
87+ public void actionPerformed ( ActionEvent e ) {
88+ PrinterJob pj = PrinterJob . getPrinterJob ( );
89+ if ( pj . getPrintService () == null ) {
90+ System . out . println ( "No printers. Test cannot continue. Install " +
91+ "printer and restart the test." );
92+ return ;
6993 }
70- });
71-
72- pack ();
94+ PrintRequestAttributeSet attrs = new HashPrintRequestAttributeSet ();
95+ if (pj != null && pj .printDialog (attrs )) {
96+ pj .setPrintable (imageCanvas );
97+ try {
98+ pj .print (attrs );
99+ } catch (PrinterException pe ) {
100+ pe .printStackTrace ();
101+ throw new RuntimeException ("Exception whilst printing." );
102+ } finally {
103+ System .out .println ("PRINT RETURNED OK." );
104+ }
105+ }
106+ }
107+ });
108+
109+ Button passButton = new Button ("Pass" );
110+ passButton .addActionListener (new ActionListener () {
111+ @ Override
112+ public void actionPerformed (ActionEvent e ) {
113+ testResult = true ;
114+ countDownLatch .countDown ();
115+ testFrame .dispose ();
116+ }
117+ });
118+
119+ Button failButton = new Button ("Fail" );
120+ failButton .addActionListener (new ActionListener () {
121+ @ Override
122+ public void actionPerformed (ActionEvent e ) {
123+ testResult = false ;
124+ countDownLatch .countDown ();
125+ testFrame .dispose ();
126+ }
127+ });
128+
129+ Panel buttonPanel = new Panel (new GridLayout (1 ,3 ));
130+ buttonPanel .add (printButton );
131+ buttonPanel .add (passButton );
132+ buttonPanel .add (failButton );
133+ testFrame .add ("South" , buttonPanel );
134+ testFrame .add ("North" , getInstructions ());
135+ testFrame .pack ();
136+ testFrame .setVisible (true );
73137 }
74138
75- private TextArea getInstructions () {
76- TextArea ta = new TextArea (10 , 60 );
139+ private static TextArea getInstructions () {
140+ String testInstruction = "This is a manual test as it requires that you compare " +
141+ "the on-screen rendering with the printed output.\n " +
142+ "Select the 'Print' button to print out the test.\n " +
143+ "For each image compare the printed one to the on-screen one.\n " +
144+ "Press Pass button if the onscreen and printed rendering " +
145+ "match else Press fail button" ;
146+ TextArea ta = new TextArea (testInstruction ,7 , 60 ,
147+ TextArea .SCROLLBARS_NONE );
77148 ta .setFont (new Font ("Dialog" , Font .PLAIN , 11 ));
78- ta .setText
79- ("This is a manual test as it requires that you compare " +
80- "the on-screen rendering with the printed output.\n " +
81- "Select the 'Print' button to print out the test.\n " +
82- "For each image compare the printed one to the on-screen one.\n " +
83- "The test PASSES if the onscreen and printed rendering match." );
84149 return ta ;
85150 }
86-
87- public void actionPerformed (ActionEvent e ) {
88- PrinterJob pj = PrinterJob .getPrinterJob ();
89-
90- PrintRequestAttributeSet attrs = new HashPrintRequestAttributeSet ();
91- if (pj != null && pj .printDialog (attrs )) {
92- pj .setPrintable (c );
93- try {
94- pj .print (attrs );
95- } catch (PrinterException pe ) {
96- pe .printStackTrace ();
97- throw new RuntimeException ("Exception whilst printing." );
98- } finally {
99- System .out .println ("PRINT RETURNED OK." );
100- }
101- }
102- }
103151}
104152
105153class ImageCanvas extends Component implements Printable {
@@ -111,7 +159,6 @@ class ImageCanvas extends Component implements Printable {
111159 int sw =99 , sh =99 ;
112160
113161 void paintImage (BufferedImage bi , Color c1 , Color c2 ) {
114-
115162 GradientPaint tp = new GradientPaint (0.0f , 0.0f , c1 , 10f , 8f , c2 , true );
116163 Graphics2D g2d = (Graphics2D )bi .getGraphics ();
117164 g2d .setPaint (tp );
@@ -132,7 +179,6 @@ void paintImage(BufferedImage bi, Color c1, Color c2) {
132179 }
133180
134181 ImageCanvas () {
135-
136182 opaqueImg = new BufferedImage (sw , sh , TYPE_INT_RGB );
137183 Color o1 = new Color (0 , 0 , 0 );
138184 Color o2 = new Color (255 , 255 , 255 );
@@ -171,9 +217,7 @@ void paintImage(BufferedImage bi, Color c1, Color c2) {
171217
172218 }
173219
174-
175220 public int print (Graphics g , PageFormat pgFmt , int pgIndex ) {
176-
177221 if (pgIndex > 0 ) {
178222 return Printable .NO_SUCH_PAGE ;
179223 }
@@ -184,7 +228,6 @@ public int print(Graphics g, PageFormat pgFmt, int pgIndex) {
184228 }
185229
186230 private void drawImage (Graphics g , int biType , IndexColorModel icm ) {
187-
188231 BufferedImage bi ;
189232 if (icm != null ) {
190233 bi = new BufferedImage (sw , sh , biType , icm );
@@ -202,7 +245,6 @@ private void drawImage(Graphics g, int biType, IndexColorModel icm) {
202245 }
203246
204247 public void paint (Graphics g ) {
205-
206248 int incX = sw +10 , incY = sh +10 ;
207249
208250 g .translate (10 , 10 );
@@ -259,14 +301,11 @@ public void paint(Graphics g) {
259301 g .translate (incX , 0 );
260302 }
261303
262-
263-
264- /* Size is chosen to match default imageable width of a NA letter
265- * page. This means there will be clipping, what is clipped will
266- * depend on PageFormat orientation.
267- */
268- public Dimension getPreferredSize () {
304+ /* Size is chosen to match default imageable width of a NA letter
305+ * page. This means there will be clipping, what is clipped will
306+ * depend on PageFormat orientation.
307+ */
308+ public Dimension getPreferredSize () {
269309 return new Dimension (468 , 600 );
270310 }
271-
272311}
0 commit comments