1 /*
2  * Copyright (c) 2014, 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  * @bug 8041902
26  * @summary Test printing of wide poly lines.
27  * @run main/manual=yesno PolylinePrintingTest
28  */
29 
30 import java.awt.Dialog;
31 import java.awt.Frame;
32 import java.awt.TextArea;
33 import java.awt.BasicStroke;
34 import java.awt.Graphics;
35 import java.awt.Graphics2D;
36 import java.awt.geom.Path2D;
37 import java.awt.print.PageFormat;
38 import java.awt.print.Paper;
39 import java.awt.print.Printable;
40 import java.awt.print.PrinterException;
41 import java.awt.print.PrinterJob;
42 
43 public class PolylinePrintingTest implements Printable {
44 
print(Graphics graphics, PageFormat pageFormat, int pageIndex)45     public int print(Graphics graphics, PageFormat pageFormat,
46                      int pageIndex) throws PrinterException {
47 
48         if (pageIndex > 0) {
49             return NO_SUCH_PAGE;
50         }
51 
52         Graphics2D g2d = (Graphics2D) graphics;
53         g2d.setStroke(new BasicStroke(25,
54                                       BasicStroke.CAP_ROUND,
55                                       BasicStroke.JOIN_MITER,
56                                       10.0F, null, 1.0F));
57 
58         int[] x2Points = {100, 250, 400};
59         int[] y2Points = {100, 400, 100};
60         drawPolylineGOOD(g2d, x2Points, y2Points);
61         drawPolylineBAD(g2d, x2Points, y2Points);
62 
63         return PAGE_EXISTS;
64     }
65 
drawPolylineGOOD(Graphics2D g2d, int[] x2Points, int[] y2Points)66     private void drawPolylineGOOD(Graphics2D g2d,
67                                   int[] x2Points, int[] y2Points) {
68 
69         Path2D polyline =
70             new Path2D.Float(Path2D.WIND_EVEN_ODD, x2Points.length);
71 
72         polyline.moveTo(x2Points[0], y2Points[0]);
73 
74         for (int index = 1; index < x2Points.length; index++) {
75                 polyline.lineTo(x2Points[index], y2Points[index]);
76         }
77         g2d.draw(polyline);
78     }
79 
drawPolylineBAD(Graphics2D g, int[] xp, int[] yp)80     private void drawPolylineBAD(Graphics2D g, int[] xp, int[] yp) {
81         int offset = 200;
82         g.translate(0, offset);
83         g.drawPolyline(xp, yp, xp.length);
84     }
85 
PolylinePrintingTest()86     public PolylinePrintingTest() throws PrinterException {
87         PrinterJob job = PrinterJob.getPrinterJob();
88         PageFormat pf = job.defaultPage();
89         Paper p = pf.getPaper();
90         p.setImageableArea(0,0,p.getWidth(), p.getHeight());
91         pf.setPaper(p);
92         job.setPrintable(this, pf);
93         if (job.printDialog()) {
94             job.print();
95         }
96     }
97 
main(String[] args)98     public static void main(String[] args) throws PrinterException {
99         String[] instructions = {
100              "You must have a printer available to perform this test.",
101              "OK the print dialog, and collect the printed page.",
102              "Passing test : Output should show two identical chevrons.",
103              "Failing test : The line joins will appear different."
104            };
105         Sysout.createDialog();
106         Sysout.printInstructions(instructions);
107         new PolylinePrintingTest();
108     }
109 }
110 
111 class Sysout {
112    private static TestDialog dialog;
113 
createDialogWithInstructions( String[] instructions )114    public static void createDialogWithInstructions( String[] instructions )
115     {
116       dialog = new TestDialog( new Frame(), "Instructions" );
117       dialog.printInstructions( instructions );
118       dialog.show();
119       println( "Any messages for the tester will display here." );
120     }
121 
createDialog( )122    public static void createDialog( )
123     {
124       dialog = new TestDialog( new Frame(), "Instructions" );
125       String[] defInstr = { "Instructions will appear here. ", "" } ;
126       dialog.printInstructions( defInstr );
127       dialog.show();
128       println( "Any messages for the tester will display here." );
129     }
130 
131 
printInstructions( String[] instructions )132    public static void printInstructions( String[] instructions )
133     {
134       dialog.printInstructions( instructions );
135     }
136 
137 
println( String messageIn )138    public static void println( String messageIn )
139     {
140       dialog.displayMessage( messageIn );
141     }
142 
143 }// Sysout  class
144 
145 /**
146   This is part of the standard test machinery.  It provides a place for the
147    test instructions to be displayed, and a place for interactive messages
148    to the user to be displayed.
149   To have the test instructions displayed, see Sysout.
150   To have a message to the user be displayed, see Sysout.
151   Do not call anything in this dialog directly.
152   */
153 class TestDialog extends Dialog {
154   TextArea instructionsText;
155    TextArea messageText;
156    int maxStringLength = 80;
157 
158    //DO NOT call this directly, go through Sysout
TestDialog( Frame frame, String name )159    public TestDialog( Frame frame, String name )
160     {
161       super( frame, name );
162       int scrollBoth = TextArea.SCROLLBARS_BOTH;
163       instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
164       add( "North", instructionsText );
165 
166       messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
167       add("Center", messageText);
168 
169       pack();
170 
171       show();
172     }// TestDialog()
173 
174    //DO NOT call this directly, go through Sysout
printInstructions( String[] instructions )175    public void printInstructions( String[] instructions )
176     {
177       //Clear out any current instructions
178       instructionsText.setText( "" );
179 
180       //Go down array of instruction strings
181 
182       String printStr, remainingStr;
183       for( int i=0; i < instructions.length; i++ )
184        {
185          //chop up each into pieces maxSringLength long
186          remainingStr = instructions[ i ];
187          while( remainingStr.length() > 0 )
188           {
189             //if longer than max then chop off first max chars to print
190             if( remainingStr.length() >= maxStringLength )
191              {
192                //Try to chop on a word boundary
193                int posOfSpace = remainingStr.
194                   lastIndexOf( ' ', maxStringLength - 1 );
195                if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
196 
197                printStr = remainingStr.substring( 0, posOfSpace + 1 );
198                remainingStr = remainingStr.substring( posOfSpace + 1 );
199              }
200             //else just print
201             else
202              {
203                printStr = remainingStr;
204                remainingStr = "";
205              }
206 
207             instructionsText.append( printStr + "\n" );
208 
209           }// while
210 
211        }// for
212 
213     }//printInstructions()
214 
215    //DO NOT call this directly, go through Sysout
displayMessage( String messageIn )216    public void displayMessage( String messageIn )
217     {
218       messageText.append( messageIn + "\n" );
219     }
220 
221 }// TestDialog  class
222 
223