1
1
/*
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.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
21
21
* questions.
22
22
*/
23
23
24
- /**
25
- *
24
+ /*
26
25
* @test
27
26
* @bug 4521945 7006865
28
27
* @summary Test printing images of different types.
29
28
* @author prr
30
- * @run main/manual=yesno/timeout=900 ImageTypes
29
+ * @run main/manual ImageTypes
31
30
*/
32
31
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
+
34
59
import 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 .*;
40
60
import static java .awt .image .BufferedImage .*;
41
- import javax .print .*;
42
- import javax .print .attribute .*;
43
- import javax .print .attribute .standard .*;
44
61
45
- public class ImageTypes extends Frame implements ActionListener {
62
+ public class ImageTypes {
46
63
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 );
48
68
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
+ }
53
77
}
54
78
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 ;
69
93
}
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 );
73
137
}
74
138
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 );
77
148
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." );
84
149
return ta ;
85
150
}
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
- }
103
151
}
104
152
105
153
class ImageCanvas extends Component implements Printable {
@@ -111,7 +159,6 @@ class ImageCanvas extends Component implements Printable {
111
159
int sw =99 , sh =99 ;
112
160
113
161
void paintImage (BufferedImage bi , Color c1 , Color c2 ) {
114
-
115
162
GradientPaint tp = new GradientPaint (0.0f , 0.0f , c1 , 10f , 8f , c2 , true );
116
163
Graphics2D g2d = (Graphics2D )bi .getGraphics ();
117
164
g2d .setPaint (tp );
@@ -132,7 +179,6 @@ void paintImage(BufferedImage bi, Color c1, Color c2) {
132
179
}
133
180
134
181
ImageCanvas () {
135
-
136
182
opaqueImg = new BufferedImage (sw , sh , TYPE_INT_RGB );
137
183
Color o1 = new Color (0 , 0 , 0 );
138
184
Color o2 = new Color (255 , 255 , 255 );
@@ -171,9 +217,7 @@ void paintImage(BufferedImage bi, Color c1, Color c2) {
171
217
172
218
}
173
219
174
-
175
220
public int print (Graphics g , PageFormat pgFmt , int pgIndex ) {
176
-
177
221
if (pgIndex > 0 ) {
178
222
return Printable .NO_SUCH_PAGE ;
179
223
}
@@ -184,7 +228,6 @@ public int print(Graphics g, PageFormat pgFmt, int pgIndex) {
184
228
}
185
229
186
230
private void drawImage (Graphics g , int biType , IndexColorModel icm ) {
187
-
188
231
BufferedImage bi ;
189
232
if (icm != null ) {
190
233
bi = new BufferedImage (sw , sh , biType , icm );
@@ -202,7 +245,6 @@ private void drawImage(Graphics g, int biType, IndexColorModel icm) {
202
245
}
203
246
204
247
public void paint (Graphics g ) {
205
-
206
248
int incX = sw +10 , incY = sh +10 ;
207
249
208
250
g .translate (10 , 10 );
@@ -259,14 +301,11 @@ public void paint(Graphics g) {
259
301
g .translate (incX , 0 );
260
302
}
261
303
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 () {
269
309
return new Dimension (468 , 600 );
270
310
}
271
-
272
311
}
0 commit comments