1 /*
   2  * Copyright (c) 2007, 2017, 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  * @key headful printer
  27  * @bug 6498340
  28  * @summary No exception when printing text with a paint.
  29  * @run main PaintText
  30  */
  31 
  32 import java.awt.*;
  33 import java.awt.event.*;
  34 import java.text.*;
  35 import java.util.*;
  36 import java.awt.font.*;
  37 import java.awt.geom.*;
  38 import java.awt.print.*;
  39 import javax.swing.*;
  40 
  41 public class PaintText extends Component implements Printable {
  42 
  43     static int preferredSize;
  44     static int NUMTABS = 6;
  45     int tabNumber;
  46 
  47     public static void main(String args[]) {
  48 
  49         PrinterJob pjob = PrinterJob.getPrinterJob();
  50         if (pjob.getPrintService() == null) {
  51             System.out.println("No printers: cannot continue");
  52             return;
  53         }
  54 
  55         PageFormat pf = pjob.defaultPage();
  56         preferredSize = (int)pf.getImageableWidth();
  57 
  58         Book book = new Book();
  59 
  60         JTabbedPane p = new JTabbedPane();
  61 
  62         for (int id=1; id <= NUMTABS; id++) {
  63             String name = "Tab " + new Integer(id);
  64             PaintText ptt = new PaintText(id);
  65             p.add(name, ptt);
  66             book.append(ptt, pf);
  67         }
  68         pjob.setPageable(book);
  69 
  70         JFrame f = new JFrame();
  71         f.add(BorderLayout.CENTER, p);
  72         f.addWindowListener(new WindowAdapter() {
  73             public void windowClosing(WindowEvent e) {System.exit(0);}
  74         });
  75         f.pack();
  76         f.show();
  77 
  78         /* Non-jtreg execution will display the dialog */
  79         if (System.getProperty("test.jdk") == null) {
  80             if (!pjob.printDialog()) {
  81                 return;
  82             }
  83         }
  84         try {
  85             pjob.print();
  86         } catch (PrinterException e) {
  87             throw new RuntimeException(e.getMessage());
  88         } finally {
  89             f.dispose();
  90         }
  91     }
  92 
  93     public PaintText(int id) {
  94         tabNumber = id;
  95     }
  96 
  97     public int print(Graphics g, PageFormat pf, int pageIndex) {
  98         System.out.println(""+pageIndex);
  99         Graphics2D g2d = (Graphics2D)g;
 100         g2d.translate(pf.getImageableX(),  pf.getImageableY());
 101         g.drawString("ID="+tabNumber,100,20);
 102         g.translate(0, 25);
 103         paint(g);
 104         return PAGE_EXISTS;
 105     }
 106 
 107     public Dimension getMinimumSize() {
 108         return getPreferredSize();
 109     }
 110 
 111     public Dimension getPreferredSize() {
 112         return new Dimension(preferredSize, preferredSize);
 113     }
 114 
 115     public void paint(Graphics g) {
 116 
 117         /* fill with white before any transformation is applied */
 118         g.setColor(Color.white);
 119         g.fillRect(0, 0, getSize().width, getSize().height);
 120 
 121         Graphics2D g2d = (Graphics2D)g;
 122 
 123         Font f = new Font("Lucida Sans", Font.PLAIN, 40);
 124         Color c = new Color(0,0,255,96);
 125         Paint p = new GradientPaint(0f, 0f, Color.green,
 126                                     10f, 10f, Color.red,
 127                                     true);
 128         String s = "Sample Text To Paint";
 129         float x = 20, y= 50;
 130 
 131         switch (tabNumber) {
 132         case 1:
 133             g2d.setFont(f);
 134             g2d.setColor(c);
 135             g2d.drawString(s, x, y);
 136             break;
 137 
 138         case 2:
 139             g2d.setFont(f);
 140             g2d.setPaint(p);
 141             g2d.drawString(s, x, y);
 142             break;
 143 
 144         case 3:
 145             AttributedString as = new AttributedString(s);
 146             as.addAttribute(TextAttribute.FONT, f);
 147             as.addAttribute(TextAttribute.FOREGROUND, c);
 148             g2d.drawString(as.getIterator(), x, y);
 149             break;
 150 
 151         case 4:
 152             as = new AttributedString(s);
 153             as.addAttribute(TextAttribute.FONT, f);
 154             as.addAttribute(TextAttribute.FOREGROUND, p);
 155             g2d.drawString(as.getIterator(), x, y);
 156             break;
 157 
 158         case 5:
 159             as = new AttributedString(s);
 160             as.addAttribute(TextAttribute.FONT, f);
 161             as.addAttribute(TextAttribute.FOREGROUND, c);
 162             FontRenderContext frc = g2d.getFontRenderContext();
 163             TextLayout tl = new TextLayout(as.getIterator(), frc);
 164             tl.draw(g2d, x, y);
 165             break;
 166 
 167         case 6:
 168             as = new AttributedString(s);
 169             as.addAttribute(TextAttribute.FONT, f);
 170             as.addAttribute(TextAttribute.FOREGROUND, p);
 171             frc = g2d.getFontRenderContext();
 172             tl = new TextLayout(as.getIterator(), frc);
 173             tl.draw(g2d, x, y);
 174             break;
 175 
 176         default:
 177         }
 178     }
 179 
 180 }