jdk/test/java/awt/print/PrinterJob/PrintGlyphVectorTest.java
changeset 21929 48575f27a3cb
child 23010 6dadb192ad81
equal deleted inserted replaced
21781:12385ca90f4d 21929:48575f27a3cb
       
     1 /*
       
     2  * Copyright (c) 1999, 2003, 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 8029204
       
    27  * @summary Tests GlyphVector is printed in the correct location
       
    28  * @run main/manual=yesno PrintGlyphVectorTest
       
    29  */
       
    30 
       
    31 import java.io.*;
       
    32 import java.awt.*;
       
    33 import java.awt.event.*;
       
    34 import java.awt.font.*;
       
    35 import java.awt.geom.*;
       
    36 import java.awt.print.*;
       
    37 
       
    38 public class PrintGlyphVectorTest extends Component implements Printable {
       
    39 
       
    40     public void drawGVs(Graphics g) {
       
    41 
       
    42         String testString = "0123456789abcdefghijklm";
       
    43         Graphics2D g2d = (Graphics2D)g;
       
    44         g2d.setColor(Color.black);
       
    45         Font font = new Font("SansSerif", Font.PLAIN, 30);
       
    46         FontRenderContext frc = g2d.getFontRenderContext();
       
    47         GlyphVector v = font.createGlyphVector(frc, testString);
       
    48 
       
    49 
       
    50         float x = 50f,
       
    51               y = 50f;
       
    52 
       
    53         g2d.drawGlyphVector(v, x, y);
       
    54         Rectangle2D r = v.getVisualBounds();
       
    55         r.setRect(r.getX()+x, r.getY()+y, r.getWidth(), r.getHeight());
       
    56         g2d.draw(r);
       
    57 
       
    58         Point2D p; // .Float p = new Point2D.Float();
       
    59         for (int i = 0; i < v.getNumGlyphs(); i++) {
       
    60             p = v.getGlyphPosition(i);
       
    61             p.setLocation(p.getX()+50, p.getY());
       
    62             v.setGlyphPosition(i, p);
       
    63         }
       
    64 
       
    65         x = 0;
       
    66         y+= 50;
       
    67 
       
    68         g2d.drawGlyphVector(v, x, y);
       
    69         r = v.getVisualBounds();
       
    70         r.setRect(r.getX()+x, r.getY()+y, r.getWidth(), r.getHeight());
       
    71         g2d.draw(r);
       
    72 
       
    73 
       
    74 
       
    75     }
       
    76 
       
    77      public void paint(Graphics g) {
       
    78        g.setColor(Color.white);
       
    79        g.fillRect(0,0,getSize().width, getSize().height);
       
    80        drawGVs(g);
       
    81      }
       
    82 
       
    83     public Dimension getPreferredSize() {
       
    84         return new Dimension(600,200);
       
    85     }
       
    86 
       
    87     public int print(Graphics g, PageFormat pf, int pageIndex) {
       
    88 
       
    89         if (pageIndex > 0) {
       
    90             return Printable.NO_SUCH_PAGE;
       
    91         }
       
    92 
       
    93         Graphics2D g2d = (Graphics2D)g;
       
    94         g2d.translate(pf.getImageableX(), pf.getImageableY());
       
    95         drawGVs(g2d);
       
    96 
       
    97         return Printable.PAGE_EXISTS;
       
    98     }
       
    99 
       
   100 
       
   101     public static void main(String arg[]) throws Exception {
       
   102 
       
   103        Frame f = new Frame();
       
   104        PrintGlyphVectorTest pvt = new PrintGlyphVectorTest();
       
   105        f.add("Center", pvt);
       
   106        f.add("South", new PrintInstructions());
       
   107        f.pack();
       
   108        f.show();
       
   109 
       
   110 
       
   111     }
       
   112 }
       
   113 
       
   114 class PrintInstructions extends Panel implements ActionListener {
       
   115 
       
   116    static final String INSTRUCTIONS =
       
   117        "You must have a printer installed for this test.\n" +
       
   118        "Press the PRINT button below and OK the print dialog\n" +
       
   119        "Retrieve the output and compare the printed and on-screen text\n" +
       
   120        " to confirm that in both cases the text is aligned and the boxes\n" +
       
   121        "are around the text, not offset from the text.";
       
   122 
       
   123   PrintInstructions() {
       
   124 
       
   125      setLayout(new GridLayout(2,1));
       
   126      TextArea t = new TextArea(INSTRUCTIONS, 8, 80);
       
   127      add(t);
       
   128      Button b = new Button("PRINT");
       
   129      b.setFont(new Font("Dialog", Font.BOLD, 30));
       
   130      b.addActionListener(this);
       
   131      add(b);
       
   132   }
       
   133 
       
   134   public void actionPerformed(ActionEvent e) {
       
   135        PrinterJob pj = PrinterJob.getPrinterJob();
       
   136        if (pj == null ||
       
   137            pj.getPrintService() == null ||
       
   138            !pj.printDialog()) {
       
   139            return;
       
   140        }
       
   141 
       
   142        pj.setPrintable(new PrintGlyphVectorTest());
       
   143        try {
       
   144            pj.print();
       
   145        } catch (PrinterException ex) {
       
   146            System.err.println(ex);
       
   147        }
       
   148   }
       
   149 
       
   150 }