|
| 1 | +/* |
| 2 | + * Copyright (c) 2002, 2024, 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 4725045 |
| 27 | + * @key headful |
| 28 | + * @summary verifies that there are no artifacts due to using |
| 29 | + * GDI for copies to the back buffer (GDI should only be used |
| 30 | + * for copies to the screen) |
| 31 | + * @run main GdiBlitOffscreenTest |
| 32 | +*/ |
| 33 | + |
| 34 | +import java.awt.Color; |
| 35 | +import java.awt.Graphics; |
| 36 | +import java.awt.Image; |
| 37 | +import java.awt.Point; |
| 38 | +import java.awt.Robot; |
| 39 | +import java.awt.image.BufferedImage; |
| 40 | +import javax.swing.JComponent; |
| 41 | +import javax.swing.JFrame; |
| 42 | +import javax.swing.SwingUtilities; |
| 43 | + |
| 44 | +public class GdiBlitOffscreenTest { |
| 45 | + |
| 46 | + static volatile JFrame f; |
| 47 | + static final int imageW = 100, imageH = 100, FW = 500, FH = 500; |
| 48 | + static volatile BufferedImage greenImage; |
| 49 | + |
| 50 | + public static void main(String[] args) throws Exception { |
| 51 | + |
| 52 | + // First, create an image. |
| 53 | + greenImage = new BufferedImage(imageW, imageH, |
| 54 | + BufferedImage.TYPE_INT_RGB); |
| 55 | + Graphics redG = greenImage.getGraphics(); |
| 56 | + redG.setColor(Color.green); |
| 57 | + redG.fillRect(0, 0, imageW, imageH); |
| 58 | + redG.setColor(Color.white); |
| 59 | + redG.drawString("Passed!", 30, 80); |
| 60 | + |
| 61 | + Robot robot = new Robot(); |
| 62 | + try { |
| 63 | + SwingUtilities.invokeAndWait(GdiBlitOffscreenTest::createUI); |
| 64 | + robot.delay(1000); |
| 65 | + robot.waitForIdle(); |
| 66 | + Point p = f.getLocationOnScreen(); |
| 67 | + Color c = robot.getPixelColor(p.x+FW/2, p.y+FH/2); |
| 68 | + if (!c.equals(Color.green)) { |
| 69 | + throw new RuntimeException("Color is " + c); |
| 70 | + } |
| 71 | + } finally { |
| 72 | + if (f != null) { |
| 73 | + SwingUtilities.invokeAndWait(f::dispose); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private static void createUI() { |
| 79 | + f = new JFrame("GdiBlitOffscreenTest"); |
| 80 | + f.setSize(FW, FH); |
| 81 | + f.setVisible(true); |
| 82 | + |
| 83 | + // copy the image to the window. |
| 84 | + Graphics g = f.getGraphics(); |
| 85 | + g.drawImage(greenImage, 0, 0, null); |
| 86 | + |
| 87 | + // Now, get on with the rest of the test |
| 88 | + JComponent app = new GdiBlitOffscreenTestComponent(imageW, imageH, greenImage); |
| 89 | + app.setSize(500, 500); |
| 90 | + f.getContentPane().add(app); |
| 91 | + f.validate(); |
| 92 | + f.repaint(); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +class GdiBlitOffscreenTestComponent extends JComponent { |
| 97 | + |
| 98 | + int imageW, imageH; |
| 99 | + Image theImage; |
| 100 | + |
| 101 | + public GdiBlitOffscreenTestComponent(int imageW, int imageH, |
| 102 | + Image theImage) |
| 103 | + { |
| 104 | + this.theImage = theImage; |
| 105 | + this.imageW = imageW; |
| 106 | + this.imageH = imageH; |
| 107 | + } |
| 108 | + |
| 109 | + public void paintComponent(Graphics g) { |
| 110 | + int imageX = (getWidth() - imageW) / 2; |
| 111 | + int imageY = (getHeight() - imageH) / 2; |
| 112 | + g.setColor(Color.blue); |
| 113 | + g.fillRect(0, 0, getWidth(), getHeight()); |
| 114 | + g.setColor(Color.red); |
| 115 | + g.fillRect(imageX, imageY, imageW, imageH); |
| 116 | + g.setColor(Color.white); |
| 117 | + g.drawString("Failed!", imageX + 30, imageY + 80); |
| 118 | + g.drawImage(theImage, imageX, imageY, null); |
| 119 | + } |
| 120 | + |
| 121 | +} |
0 commit comments