1 /*
   2  * Copyright (c) 2007, 2018, 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 6576507 8202841
  27  * @summary Both lines of text should be readable
  28  * @run main/manual LCDTextAndGraphicsState
  29  */
  30 import java.awt.Component;
  31 import java.awt.Graphics;
  32 import java.awt.Graphics2D;
  33 import java.awt.Color;
  34 import java.awt.RenderingHints;
  35 import java.awt.AlphaComposite;
  36 import java.awt.GradientPaint;
  37 import java.awt.Shape;
  38 import java.awt.Dimension;
  39 import java.awt.Frame;
  40 import java.awt.BorderLayout;
  41 import java.awt.Button;
  42 import java.awt.TextArea;
  43 import java.awt.FlowLayout;
  44 import java.awt.event.ActionEvent;
  45 import java.awt.event.ActionListener;
  46 import java.awt.geom.RoundRectangle2D;
  47 
  48 public class LCDTextAndGraphicsState extends Component {
  49 
  50     String text = "This test passes only if this text appears SIX TIMES";
  51 
  52     private static Button passButton;
  53     private static Button failButton;
  54     private static TextArea instructions;
  55     private static Frame frame;
  56     private static Frame instructionFrame;
  57     private static Thread mainThread = null;
  58     private static int sleepTime = 300000;
  59     private static int sleepLoopTime = 1000;
  60     private static volatile boolean continueTest = true;
  61 
  62     public void createAndShowInstructionFrame() {
  63         passButton = new Button("Pass");
  64         passButton.setEnabled(true);
  65 
  66         failButton = new Button("Fail");
  67         failButton.setEnabled(true);
  68 
  69         instructions = new TextArea(5, 60);
  70         instructions.setText(" This is a manual test\n\n" +
  71                 " 1) Press PASS if both lines of text are readable and appear 6 times\n" +
  72                 " 2) Press FAIL otherwise");
  73 
  74         instructionFrame = new Frame("Test Instructions");
  75         instructionFrame.add(passButton);
  76         instructionFrame.add(failButton);
  77         instructionFrame.add(instructions);
  78         instructionFrame.setSize(200,200);
  79         instructionFrame.setLayout(new FlowLayout());
  80         instructionFrame.pack();
  81         instructionFrame.setVisible(true);
  82 
  83         passButton.addActionListener(new ActionListener() {
  84             @Override
  85             public void actionPerformed(ActionEvent ae) {
  86                 dispose();
  87                 continueTest = false;
  88             }
  89         });
  90 
  91         failButton.addActionListener(new ActionListener() {
  92             @Override
  93             public void actionPerformed(ActionEvent ae) {
  94                 dispose();
  95                 continueTest = false;
  96                 throw new RuntimeException("Both lines of text are not readable or do not appear SIX TIMES !!!");
  97             }
  98         });
  99     }
 100 
 101     private static void dispose() {
 102         frame.dispose();
 103         instructionFrame.dispose();
 104     }
 105 
 106     public void paint(Graphics g) {
 107 
 108         Graphics2D g2d = (Graphics2D)g.create();
 109         g2d.setColor(Color.white);
 110         g2d.fillRect(0,0,getSize().width, getSize().height);
 111 
 112         test1(g.create(0,   0, 500, 200));
 113         test2(g.create(0, 200, 500, 200));
 114         test3(g.create(0, 400, 500, 200));
 115     }
 116 
 117     public void test1(Graphics g) {
 118         Graphics2D g2d = (Graphics2D)g;
 119         g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
 120                              RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
 121         g2d.setColor(Color.black);
 122         g2d.drawString(text, 10, 50);
 123         g2d.setComposite(AlphaComposite.getInstance(
 124                          AlphaComposite.SRC_OVER, 0.9f));
 125         g2d.drawString(text, 10, 80);
 126     }
 127 
 128     public void test2(Graphics g) {
 129         Graphics2D g2d = (Graphics2D)g;
 130         g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
 131                              RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
 132         g2d.setColor(Color.black);
 133         g2d.drawString(text, 10, 50);
 134         g2d.setPaint(new GradientPaint(
 135                      0f, 0f, Color.BLACK, 100f, 100f, Color.GRAY));
 136         g2d.drawString(text, 10, 80);
 137     }
 138 
 139     public void test3(Graphics g) {
 140         Graphics2D g2d = (Graphics2D)g;
 141         g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
 142                              RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
 143         g2d.setColor(Color.black);
 144         g2d.drawString(text, 10, 50);
 145         Shape s = new RoundRectangle2D.Double(0, 60, 400, 50, 5, 5);
 146         g2d.clip(s);
 147         g2d.drawString(text, 10, 80);
 148     }
 149 
 150     public Dimension getPreferredSize() {
 151         return new Dimension(500,600);
 152     }
 153 
 154     public void createAndShowFrame() {
 155         frame = new Frame("Composite and Text Test");
 156         frame.add(new LCDTextAndGraphicsState(), BorderLayout.CENTER);
 157         frame.setLocationRelativeTo(instructionFrame);
 158         frame.pack();
 159         frame.setVisible(true);
 160     }
 161 
 162     public static void main(String[] args) throws Exception {
 163 
 164         mainThread = Thread.currentThread();
 165         LCDTextAndGraphicsState lcdTest = new LCDTextAndGraphicsState();
 166         lcdTest.createAndShowInstructionFrame();
 167         lcdTest.createAndShowFrame();
 168 
 169         int remainingSleepTime = sleepTime;
 170         while(remainingSleepTime > 0 && continueTest) {
 171             mainThread.sleep(sleepLoopTime);
 172             remainingSleepTime -= sleepLoopTime;
 173         }
 174 
 175         if (continueTest) {
 176             lcdTest.dispose();
 177             throw new RuntimeException("Timed out after " + (sleepTime - remainingSleepTime) / 1000
 178                     + " seconds");
 179         }
 180     }
 181 }
 182