|
| 1 | +/* |
| 2 | + * Copyright (c) 1999, 2025, 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 4216180 |
| 27 | + * @summary This test verifies that Graphics2D.setBackground and clearRect |
| 28 | + * performs correctly regardless of antialiasing hint. |
| 29 | + * @key headful |
| 30 | + * @run main NativeWin32Clear |
| 31 | + */ |
| 32 | + |
| 33 | +import java.awt.AWTException; |
| 34 | +import java.awt.Color; |
| 35 | +import java.awt.Dimension; |
| 36 | +import java.awt.EventQueue; |
| 37 | +import java.awt.Frame; |
| 38 | +import java.awt.Graphics; |
| 39 | +import java.awt.Graphics2D; |
| 40 | +import java.awt.Insets; |
| 41 | +import java.awt.Point; |
| 42 | +import java.awt.Rectangle; |
| 43 | +import java.awt.RenderingHints; |
| 44 | +import java.awt.Robot; |
| 45 | +import java.awt.image.BufferedImage; |
| 46 | +import java.lang.reflect.InvocationTargetException; |
| 47 | + |
| 48 | +public class NativeWin32Clear extends Frame { |
| 49 | + |
| 50 | + public void initialize() { |
| 51 | + setLocationRelativeTo(null); |
| 52 | + setSize(300, 200); |
| 53 | + setBackground(Color.red); |
| 54 | + setVisible(true); |
| 55 | + } |
| 56 | + |
| 57 | + public void paint(Graphics g) { |
| 58 | + Graphics2D g2 = (Graphics2D) g; |
| 59 | + Dimension d = getSize(); |
| 60 | + g2.setBackground(Color.green); |
| 61 | + g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, |
| 62 | + RenderingHints.VALUE_ANTIALIAS_ON); |
| 63 | + g2.clearRect(0, 0, d.width / 2, d.height); |
| 64 | + g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, |
| 65 | + RenderingHints.VALUE_ANTIALIAS_OFF); |
| 66 | + g2.clearRect(d.width / 2, 0, d.width / 2, d.height); |
| 67 | + g2.setColor(Color.black); |
| 68 | + } |
| 69 | + |
| 70 | + public void cleanup() { |
| 71 | + setVisible(false); |
| 72 | + dispose(); |
| 73 | + } |
| 74 | + |
| 75 | + public void requestCoordinates(Rectangle r) { |
| 76 | + Insets insets = getInsets(); |
| 77 | + Point location = getLocationOnScreen(); |
| 78 | + Dimension size = getSize(); |
| 79 | + r.x = location.x + insets.left + 5; |
| 80 | + r.y = location.y + insets.top + 5; |
| 81 | + r.width = size.width - (insets.left + insets.right + 10); |
| 82 | + r.height = size.height - (insets.top + insets.bottom + 10); |
| 83 | + } |
| 84 | + |
| 85 | + /* |
| 86 | + * Check color match within allowed deviation. |
| 87 | + * Prints first non-matching pixel coordinates and actual and expected values. |
| 88 | + * Returns true if image is filled with the provided color, false otherwise. |
| 89 | + */ |
| 90 | + private boolean checkColor(BufferedImage img, Color c, int delta) { |
| 91 | + int cRed = c.getRed(); |
| 92 | + int cGreen = c.getGreen(); |
| 93 | + int cBlue = c.getBlue(); |
| 94 | + for (int y = 0; y < img.getHeight(); y++) { |
| 95 | + for (int x = 0; x < img.getWidth(); x++) { |
| 96 | + int rgb = img.getRGB(x, y); |
| 97 | + int red = (rgb & 0x00ff0000) >> 16; |
| 98 | + int green = (rgb & 0x0000ff00) >> 8; |
| 99 | + int blue = rgb & 0x000000ff; |
| 100 | + if (cRed > (red + delta) || cRed < (red - delta) |
| 101 | + || cGreen > (green + delta) || cGreen < (green - delta) |
| 102 | + || cBlue > (blue + delta) || cBlue < (blue - delta)) { |
| 103 | + System.err.println("Color at coordinates (" + x + ", " + y + ") does not match"); |
| 104 | + System.err.println("Expected color: " + c.getRGB()); |
| 105 | + System.err.println("Actual color: " + rgb); |
| 106 | + System.err.println("Allowed deviation: " + delta); |
| 107 | + return false; |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + return true; |
| 112 | + } |
| 113 | + |
| 114 | + public static void main(String[] args) throws InterruptedException, |
| 115 | + InvocationTargetException, AWTException { |
| 116 | + NativeWin32Clear test = new NativeWin32Clear(); |
| 117 | + try { |
| 118 | + EventQueue.invokeAndWait(test::initialize); |
| 119 | + Robot robot = new Robot(); |
| 120 | + Rectangle coords = new Rectangle(); |
| 121 | + EventQueue.invokeAndWait(() -> { |
| 122 | + test.requestCoordinates(coords); |
| 123 | + }); |
| 124 | + robot.delay(2000); |
| 125 | + robot.mouseMove(coords.x - 50, coords.y - 50); |
| 126 | + robot.waitForIdle(); |
| 127 | + BufferedImage capture = robot.createScreenCapture(coords); |
| 128 | + robot.delay(2000); |
| 129 | + if (!test.checkColor(capture, Color.green, 5)) { |
| 130 | + throw new RuntimeException("Incorrect color encountered, check error log for details"); |
| 131 | + } |
| 132 | + } finally { |
| 133 | + EventQueue.invokeAndWait(test::cleanup); |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments