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