|
1 | 1 | /* |
2 | | - * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2007, 2021, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
|
21 | 21 | * questions. |
22 | 22 | */ |
23 | 23 |
|
24 | | -/** |
| 24 | +/* |
25 | 25 | * @test |
26 | 26 | * @bug 6576507 |
27 | 27 | * @summary Both lines of text should be readable |
28 | | - * @run main/manual=yesno LCDTextAndGraphicsState |
| 28 | + * @run main/manual LCDTextAndGraphicsState |
29 | 29 | */ |
30 | | -import java.awt.*; |
31 | | -import java.awt.geom.*; |
| 30 | + |
| 31 | +import java.awt.AlphaComposite; |
| 32 | +import java.awt.BorderLayout; |
| 33 | +import java.awt.Button; |
| 34 | +import java.awt.Color; |
| 35 | +import java.awt.Component; |
| 36 | +import java.awt.Dimension; |
| 37 | +import java.awt.Frame; |
| 38 | +import java.awt.GradientPaint; |
| 39 | +import java.awt.Graphics; |
| 40 | +import java.awt.Graphics2D; |
| 41 | +import java.awt.GridLayout; |
| 42 | +import java.awt.Panel; |
| 43 | +import java.awt.RenderingHints; |
| 44 | +import java.awt.Shape; |
| 45 | +import java.awt.TextArea; |
| 46 | +import java.awt.event.ActionEvent; |
| 47 | +import java.awt.geom.RoundRectangle2D; |
| 48 | +import java.util.concurrent.CountDownLatch; |
| 49 | +import java.util.concurrent.TimeUnit; |
32 | 50 |
|
33 | 51 | public class LCDTextAndGraphicsState extends Component { |
34 | 52 |
|
35 | | - String text = "This test passes only if this text appears SIX TIMES"; |
| 53 | + private static final Frame testFrame = new Frame("Composite and Text Test"); |
| 54 | + private static final String text = "This test passes only if this text appears SIX TIMES else fail"; |
| 55 | + private static volatile boolean testResult; |
| 56 | + private static volatile CountDownLatch countDownLatch; |
36 | 57 |
|
37 | 58 | public void paint(Graphics g) { |
38 | | - |
39 | 59 | Graphics2D g2d = (Graphics2D)g.create(); |
40 | 60 | g2d.setColor(Color.white); |
41 | 61 | g2d.fillRect(0,0,getSize().width, getSize().height); |
42 | | - |
43 | | - test1(g.create(0, 0, 500, 200)); |
44 | | - test2(g.create(0, 200, 500, 200)); |
45 | | - test3(g.create(0, 400, 500, 200)); |
| 62 | + test1(g.create(0, 0, 500, 100)); |
| 63 | + test2(g.create(0, 100, 500, 100)); |
| 64 | + test3(g.create(0, 200, 500, 100)); |
46 | 65 | } |
47 | 66 |
|
48 | 67 | public void test1(Graphics g) { |
49 | 68 | Graphics2D g2d = (Graphics2D)g; |
50 | 69 | g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, |
51 | 70 | RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB); |
52 | 71 | g2d.setColor(Color.black); |
53 | | - g2d.drawString(text, 10, 50); |
| 72 | + g2d.drawString(text, 10, 20); |
54 | 73 | g2d.setComposite(AlphaComposite.getInstance( |
55 | 74 | AlphaComposite.SRC_OVER, 0.9f)); |
56 | | - g2d.drawString(text, 10, 80); |
| 75 | + g2d.drawString(text, 10, 50); |
57 | 76 | } |
58 | 77 |
|
59 | 78 | public void test2(Graphics g) { |
60 | 79 | Graphics2D g2d = (Graphics2D)g; |
61 | 80 | g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, |
62 | 81 | RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB); |
63 | 82 | g2d.setColor(Color.black); |
64 | | - g2d.drawString(text, 10, 50); |
| 83 | + g2d.drawString(text, 10, 20); |
65 | 84 | g2d.setPaint(new GradientPaint( |
66 | 85 | 0f, 0f, Color.BLACK, 100f, 100f, Color.GRAY)); |
67 | | - g2d.drawString(text, 10, 80); |
| 86 | + g2d.drawString(text, 10, 50); |
68 | 87 | } |
69 | 88 |
|
70 | 89 | public void test3(Graphics g) { |
71 | 90 | Graphics2D g2d = (Graphics2D)g; |
72 | 91 | g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, |
73 | 92 | RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB); |
74 | 93 | g2d.setColor(Color.black); |
75 | | - g2d.drawString(text, 10, 50); |
76 | | - Shape s = new RoundRectangle2D.Double(0, 60, 400, 50, 5, 5); |
| 94 | + g2d.drawString(text, 10, 20); |
| 95 | + Shape s = new RoundRectangle2D.Double(0, 30, 400, 50, 5, 5); |
77 | 96 | g2d.clip(s); |
78 | | - g2d.drawString(text, 10, 80); |
| 97 | + g2d.drawString(text, 10, 50); |
79 | 98 | } |
80 | 99 |
|
81 | 100 | public Dimension getPreferredSize() { |
82 | | - return new Dimension(500,600); |
| 101 | + return new Dimension(500,300); |
| 102 | + } |
| 103 | + |
| 104 | + public static void disposeUI() { |
| 105 | + countDownLatch.countDown(); |
| 106 | + testFrame.dispose(); |
83 | 107 | } |
84 | 108 |
|
85 | | - public static void main(String[] args) throws Exception { |
| 109 | + public static void createTestUI() { |
| 110 | + testFrame.add(new LCDTextAndGraphicsState(), BorderLayout.NORTH); |
| 111 | + Panel resultButtonPanel = new Panel(new GridLayout(1, 2)); |
| 112 | + Button passButton = new Button("Pass"); |
| 113 | + passButton.addActionListener((ActionEvent e) -> { |
| 114 | + testResult = true; |
| 115 | + disposeUI(); |
| 116 | + }); |
| 117 | + Button failButton = new Button("Fail"); |
| 118 | + failButton.addActionListener(e -> { |
| 119 | + testResult = false; |
| 120 | + disposeUI(); |
| 121 | + }); |
| 122 | + resultButtonPanel.add(passButton); |
| 123 | + resultButtonPanel.add(failButton); |
| 124 | + |
| 125 | + Panel controlUI = new Panel(new BorderLayout()); |
| 126 | + TextArea instructions = new TextArea( |
| 127 | + "Instructions:\n" + |
| 128 | + "If you see the text six times above, press Pass.\n" + |
| 129 | + "If not, press Fail.", |
| 130 | + 3, |
| 131 | + 50, |
| 132 | + TextArea.SCROLLBARS_NONE |
| 133 | + ); |
| 134 | + instructions.setEditable(false); |
| 135 | + controlUI.add(instructions, BorderLayout.CENTER); |
| 136 | + controlUI.add(resultButtonPanel, BorderLayout.SOUTH); |
| 137 | + |
| 138 | + testFrame.add(controlUI, BorderLayout.SOUTH); |
| 139 | + testFrame.pack(); |
| 140 | + testFrame.setLocationRelativeTo(null); |
| 141 | + testFrame.setVisible(true); |
| 142 | + } |
86 | 143 |
|
87 | | - Frame f = new Frame("Composite and Text Test"); |
88 | | - f.add(new LCDTextAndGraphicsState(), BorderLayout.CENTER); |
89 | | - f.pack(); |
90 | | - f.setVisible(true); |
| 144 | + public static void main(String[] args) throws InterruptedException { |
| 145 | + countDownLatch = new CountDownLatch(1); |
| 146 | + createTestUI(); |
| 147 | + if (!countDownLatch.await(10, TimeUnit.MINUTES)) { |
| 148 | + throw new RuntimeException("Timeout : No action was performed on the test UI."); |
| 149 | + } |
| 150 | + if (!testResult) { |
| 151 | + throw new RuntimeException("Test failed!"); |
| 152 | + } |
91 | 153 | } |
92 | 154 | } |
0 commit comments