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 4185019
27  * @summary Confirm that all of the drawString methods on Graphics2D
28  *          work for printer graphics objects.
29  * @run main/manual DrawStringMethods
30  */
31 
32 import java.awt.*;
33 import java.text.*;
34 import java.awt.font.*;
35 import java.awt.print.*;
36 
37 public class DrawStringMethods implements Printable {
38 
main(String args[])39     public static void main(String args[]) {
40         String[] instructions =
41         {
42             "Confirm that the methods are printed.",
43             " For Graphics: drawString, drawString, drawChars, drawBytes",
44             " For Graphics2D: drawString, drawString, drawGlyphVector"
45         };
46         Sysout.createDialogWithInstructions( instructions );
47 
48 
49         PrinterJob pjob = PrinterJob.getPrinterJob();
50         PageFormat pf = pjob.defaultPage();
51         Book book = new Book();
52 
53         book.append(new DrawStringMethods(), pf);
54         pjob.setPageable(book);
55 
56         try {
57             pjob.print();
58         } catch (PrinterException e) {
59             throw new RuntimeException(e.getMessage());
60         }
61     }
62 
getIterator(String s)63     public static AttributedCharacterIterator getIterator(String s) {
64         return new AttributedString(s).getIterator();
65     }
66 
print(Graphics g, PageFormat pf, int pageIndex)67     public int print(Graphics g, PageFormat pf, int pageIndex) {
68         int ix = (int) pf.getImageableX();
69         int iy = (int) pf.getImageableY();
70         String s;
71 
72         g.setColor(Color.black);
73 
74         iy += 50;
75         s = "--- Graphics methods: ---";
76         g.drawString(s, ix, iy);
77 
78         iy += 30;
79         s = "drawString(String str, int x, int y)";
80         g.drawLine(ix, iy, ix+10, iy);
81         g.drawString(s, ix+20, iy);
82 
83         iy += 30;
84         s = "drawString(AttributedCharacterIterator iterator, int x, int y)";
85         g.drawLine(ix, iy, ix+10, iy);
86         g.drawString(getIterator(s), ix+20, iy);
87 
88         iy += 30;
89         s = "drawChars(char data[], int offset, int length, int x, int y)";
90         g.drawLine(ix, iy, ix+10, iy);
91         g.drawChars(s.toCharArray(), 0, s.length(), ix+20, iy);
92 
93         iy += 30;
94         s = "drawBytes(byte data[], int offset, int length, int x, int y)";
95         byte data[] = new byte[s.length()];
96         for (int i = 0; i < data.length; i++) {
97             data[i] = (byte) s.charAt(i);
98         }
99         g.drawLine(ix, iy, ix+10, iy);
100         g.drawBytes(data, 0, data.length, ix+20, iy);
101 
102         iy += 50;
103         s = "--- Graphics2D methods: ---";
104         g.drawString(s, ix, iy);
105 
106         if (g instanceof Graphics2D) {
107             Graphics2D g2d = (Graphics2D) g;
108             Font f = g2d.getFont();
109             FontRenderContext frc = g2d.getFontRenderContext();
110 
111             iy += 30;
112             s = "drawString(String s, float x, float y)";
113             g.drawLine(ix, iy, ix+10, iy);
114             g2d.drawString(s, (float) ix+20, (float) iy);
115 
116             iy += 30;
117             s = "drawString(AttributedCharacterIterator iterator, "+
118                            "float x, float y)";
119             g.drawLine(ix, iy, ix+10, iy);
120             g2d.drawString(getIterator(s), (float) ix+20, (float) iy);
121 
122             iy += 30;
123             s = "drawGlyphVector(GlyphVector g, float x, float y)";
124             g.drawLine(ix, iy, ix+10, iy);
125             g2d.drawGlyphVector(f.createGlyphVector(frc, s), ix+20, iy);
126         } else {
127             iy += 30;
128             s = "Graphics object does not support Graphics2D methods";
129             g.drawString(s, ix+20, iy);
130         }
131 
132         return PAGE_EXISTS;
133     }
134 }
135 
136 class Sysout
137  {
138    private static TestDialog dialog;
139 
createDialogWithInstructions( String[] instructions )140    public static void createDialogWithInstructions( String[] instructions )
141     {
142       dialog = new TestDialog( new Frame(), "Instructions" );
143       dialog.printInstructions( instructions );
144       dialog.show();
145       println( "Any messages for the tester will display here." );
146     }
147 
createDialog( )148    public static void createDialog( )
149     {
150       dialog = new TestDialog( new Frame(), "Instructions" );
151       String[] defInstr = { "Instructions will appear here. ", "" } ;
152       dialog.printInstructions( defInstr );
153       dialog.show();
154       println( "Any messages for the tester will display here." );
155     }
156 
157 
printInstructions( String[] instructions )158    public static void printInstructions( String[] instructions )
159     {
160       dialog.printInstructions( instructions );
161     }
162 
163 
println( String messageIn )164    public static void println( String messageIn )
165     {
166       dialog.displayMessage( messageIn );
167     }
168 
169  }// Sysout  class
170 
171 /**
172   This is part of the standard test machinery.  It provides a place for the
173    test instructions to be displayed, and a place for interactive messages
174    to the user to be displayed.
175   To have the test instructions displayed, see Sysout.
176   To have a message to the user be displayed, see Sysout.
177   Do not call anything in this dialog directly.
178   */
179 class TestDialog extends Dialog
180  {
181 
182    TextArea instructionsText;
183    TextArea messageText;
184    int maxStringLength = 80;
185 
186    //DO NOT call this directly, go through Sysout
TestDialog( Frame frame, String name )187    public TestDialog( Frame frame, String name )
188     {
189       super( frame, name );
190       int scrollBoth = TextArea.SCROLLBARS_BOTH;
191       instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
192       add( "North", instructionsText );
193 
194       messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
195       add("South", messageText);
196 
197       pack();
198 
199       show();
200     }// TestDialog()
201 
202    //DO NOT call this directly, go through Sysout
printInstructions( String[] instructions )203    public void printInstructions( String[] instructions )
204     {
205       //Clear out any current instructions
206       instructionsText.setText( "" );
207 
208       //Go down array of instruction strings
209 
210       String printStr, remainingStr;
211       for( int i=0; i < instructions.length; i++ )
212        {
213      //chop up each into pieces maxSringLength long
214      remainingStr = instructions[ i ];
215      while( remainingStr.length() > 0 )
216       {
217         //if longer than max then chop off first max chars to print
218         if( remainingStr.length() >= maxStringLength )
219          {
220            //Try to chop on a word boundary
221            int posOfSpace = remainingStr.
222           lastIndexOf( ' ', maxStringLength - 1 );
223 
224            if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
225 
226            printStr = remainingStr.substring( 0, posOfSpace + 1 );
227            remainingStr = remainingStr.substring( posOfSpace + 1 );
228          }
229         //else just print
230         else
231          {
232            printStr = remainingStr;
233            remainingStr = "";
234          }
235 
236             instructionsText.append( printStr + "\n" );
237 
238       }// while
239 
240        }// for
241 
242     }//printInstructions()
243 
244    //DO NOT call this directly, go through Sysout
displayMessage( String messageIn )245    public void displayMessage( String messageIn )
246     {
247       messageText.append( messageIn + "\n" );
248     }
249 
250  }// TestDialog  class
251