11/*
2- * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2013, 2024, 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
2222 */
2323
2424/*
25- test
26- @bug 8023392 8259232
27- @summary Swing text components printed with spaces between chars
28- @key printer
29- @run applet/manual=yesno bug8023392.html
30- */
25+ * @test
26+ * @bug 8023392 8259232
27+ * @key printer
28+ * @modules java.desktop/sun.swing
29+ * @library /java/awt/regtesthelpers
30+ * @build PassFailJFrame
31+ * @summary Swing text components printed with spaces between chars
32+ * @run main/manual bug8023392
33+ */
3134
32- import javax .swing .*;
33- import javax .swing .border .LineBorder ;
34- import java .applet .Applet ;
35- import java .awt .*;
35+ import java .awt .BorderLayout ;
36+ import java .awt .Color ;
37+ import java .awt .Component ;
38+ import java .awt .Container ;
39+ import java .awt .Font ;
40+ import java .awt .Graphics ;
41+ import java .awt .Graphics2D ;
3642import java .awt .event .ActionEvent ;
3743import java .awt .event .ActionListener ;
3844import java .awt .font .TextAttribute ;
4349import java .text .AttributedCharacterIterator ;
4450import java .text .AttributedString ;
4551
52+ import javax .swing .Box ;
53+ import javax .swing .BoxLayout ;
54+ import javax .swing .JButton ;
55+ import javax .swing .JFrame ;
56+ import javax .swing .JLabel ;
57+ import javax .swing .JList ;
58+ import javax .swing .JPanel ;
59+ import javax .swing .JScrollPane ;
60+ import javax .swing .border .LineBorder ;
4661
47- public class bug8023392 extends Applet {
48- static final String [] instructions = {
49- "Please select the RadioButton for applet size labeled \" variable\" radiobutton in test harness window." ,
50- "A Frame containing several pairs of labels ((a) and (b)) is displayed." ,
51- "Labels of each pair look the same and are left-aligned (with spaces " ,
52- "between chars)." ,
53- "1. Hit the print button." ,
54- "2. Select any available printer (printing to file is also fine)." ,
55- "3. Look at the printing result (paper, PDF, PS, etc.):" ,
56- " The (a) and (b) labels should look almost the same and the (a) labels" ,
57- " shouldn't appear as if they are stretched along X axis." };
58-
59- public void init () {
60- this .setLayout (new BorderLayout ());
61- add (new SimplePrint2 (), BorderLayout .CENTER );
62-
63- Sysout .createDialogWithInstructions (instructions );
62+ import sun .swing .SwingUtilities2 ;
63+
64+ public class bug8023392 {
65+ private static final String INSTRUCTIONS =
66+ """
67+ A Frame containing several pairs of labels (a) and (b) is displayed.
68+ Labels of each pair look the same and are left-aligned (with spaces
69+ between chars).
70+ 1. Hit the print button.
71+ 2. Select any available printer (printing to file is also fine).
72+ 3. Look at the printing result (paper, PDF, PS, etc.):
73+ The (a) and (b) labels should look almost the same and the (a)
74+ labels shouldn't appear as if they are stretched along X axis.
75+ """ ;
76+
77+ public static void main (String [] args ) throws Exception {
78+ PassFailJFrame
79+ .builder ()
80+ .title ("bug8023392 Test Instructions" )
81+ .instructions (INSTRUCTIONS )
82+ .rows ((int ) INSTRUCTIONS .lines ().count () + 2 )
83+ .columns (40 )
84+ .testUI (bug8023392 ::init )
85+ .build ()
86+ .awaitAndCheck ();
87+ }
6488
89+ public static JFrame init () {
90+ JFrame frame = new JFrame ("Test Window" );
91+ frame .setLayout (new BorderLayout ());
92+ frame .add (new SimplePrint2 (), BorderLayout .CENTER );
93+ frame .pack ();
94+ return frame ;
6595 }
6696
6797 public static class SimplePrint2 extends JPanel
@@ -70,7 +100,6 @@ public static class SimplePrint2 extends JPanel
70100 JLabel label2 ;
71101 JButton printButton ;
72102
73-
74103 public SimplePrint2 () {
75104 setLayout (new BorderLayout ());
76105 label1 = new JLabel ("2a) a b c d e" +
@@ -84,15 +113,15 @@ public SimplePrint2() {
84113 String s = "3a) a b c d e " ;
85114 @ Override
86115 protected void paintComponent (Graphics g ) {
87- sun . swing . SwingUtilities2 .drawChars (this , g , s .toCharArray (),
116+ SwingUtilities2 .drawChars (this , g , s .toCharArray (),
88117 0 , s .length (), 0 , 15 );
89118 }
90119 });
91120 p1 .add (new JLabel ("wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww" ) {
92121 String s = "3b) a b c d e" ;
93122 @ Override
94123 protected void paintComponent (Graphics g ) {
95- sun . swing . SwingUtilities2 .drawChars (this , g , s .toCharArray (),
124+ SwingUtilities2 .drawChars (this , g , s .toCharArray (),
96125 0 , s .length (), 0 , 15 );
97126 }
98127 });
@@ -107,7 +136,7 @@ protected void paintComponent(Graphics g) {
107136 }
108137 @ Override
109138 protected void paintComponent (Graphics g ) {
110- sun . swing . SwingUtilities2 .drawString (this , g , it , 0 , 15 );
139+ SwingUtilities2 .drawString (this , g , it , 0 , 15 );
111140 }
112141 });
113142
@@ -122,7 +151,7 @@ protected void paintComponent(Graphics g) {
122151 }
123152 @ Override
124153 protected void paintComponent (Graphics g ) {
125- sun . swing . SwingUtilities2 .drawString (this , g , it , 0 , 15 );
154+ SwingUtilities2 .drawString (this , g , it , 0 , 15 );
126155 }
127156 });
128157
@@ -159,6 +188,8 @@ public void actionPerformed(ActionEvent e) {
159188 job .print ();
160189 } catch (PrinterException ex ) {
161190 ex .printStackTrace ();
191+ String msg = "PrinterException: " + ex .getMessage ();
192+ PassFailJFrame .forceFail (msg );
162193 }
163194 }
164195 }
@@ -178,110 +209,3 @@ public int print(Graphics graphics,
178209 }
179210 }
180211}
181-
182-
183- /**
184- * *************************************************
185- * Standard Test Machinery
186- * DO NOT modify anything below -- it's a standard
187- * chunk of code whose purpose is to make user
188- * interaction uniform, and thereby make it simpler
189- * to read and understand someone else's test.
190- * **************************************************
191- */
192- class Sysout {
193- private static TestDialog dialog ;
194-
195- public static void createDialogWithInstructions (String [] instructions ) {
196- dialog = new TestDialog (new Frame (), "Instructions" );
197- dialog .printInstructions (instructions );
198- dialog .show ();
199- println ("Any messages for the tester will display here." );
200- }
201-
202- public static void createDialog () {
203- dialog = new TestDialog (new Frame (), "Instructions" );
204- String [] defInstr = {"Instructions will appear here. " , "" };
205- dialog .printInstructions (defInstr );
206- dialog .show ();
207- println ("Any messages for the tester will display here." );
208- }
209-
210-
211- public static void printInstructions (String [] instructions ) {
212- dialog .printInstructions (instructions );
213- }
214-
215-
216- public static void println (String messageIn ) {
217- dialog .displayMessage (messageIn );
218- }
219-
220- }// Sysout class
221-
222-
223- class TestDialog extends Dialog {
224-
225- TextArea instructionsText ;
226- TextArea messageText ;
227- int maxStringLength = 80 ;
228-
229- //DO NOT call this directly, go through Sysout
230- public TestDialog (Frame frame , String name ) {
231- super (frame , name );
232- int scrollBoth = TextArea .SCROLLBARS_BOTH ;
233- instructionsText = new TextArea ("" , 15 , maxStringLength , scrollBoth );
234- add ("North" , instructionsText );
235-
236- messageText = new TextArea ("" , 5 , maxStringLength , scrollBoth );
237- add ("South" , messageText );
238-
239- pack ();
240-
241- show ();
242- }// TestDialog()
243-
244- //DO NOT call this directly, go through Sysout
245- public void printInstructions (String [] instructions ) {
246- //Clear out any current instructions
247- instructionsText .setText ("" );
248-
249- //Go down array of instruction strings
250-
251- String printStr , remainingStr ;
252- for (int i = 0 ; i < instructions .length ; i ++) {
253- //chop up each into pieces maxSringLength long
254- remainingStr = instructions [i ];
255- while (remainingStr .length () > 0 ) {
256- //if longer than max then chop off first max chars to print
257- if (remainingStr .length () >= maxStringLength ) {
258- //Try to chop on a word boundary
259- int posOfSpace = remainingStr .
260- lastIndexOf (' ' , maxStringLength - 1 );
261-
262- if (posOfSpace <= 0 ) posOfSpace = maxStringLength - 1 ;
263-
264- printStr = remainingStr .substring (0 , posOfSpace + 1 );
265- remainingStr = remainingStr .substring (posOfSpace + 1 );
266- }
267- //else just print
268- else {
269- printStr = remainingStr ;
270- remainingStr = "" ;
271- }
272-
273- instructionsText .append (printStr + "\n " );
274-
275- }// while
276-
277- }// for
278-
279- }//printInstructions()
280-
281- //DO NOT call this directly, go through Sysout
282- public void displayMessage (String messageIn ) {
283- messageText .append (messageIn + "\n " );
284- }
285-
286- }// TestDialog class
287-
0 commit comments