1 /*
   2  * Copyright (c) 2009, 2017, 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  * @key headful
  27  * @bug 6683775 6794764 8186617
  28  * @summary Painting artifacts is seen when panel is made setOpaque(false) for a
  29  *          translucent window
  30  */
  31 
  32 import java.awt.Color;
  33 import java.awt.GraphicsConfiguration;
  34 import java.awt.GraphicsDevice;
  35 import java.awt.GraphicsEnvironment;
  36 import java.awt.Rectangle;
  37 import java.awt.Robot;
  38 import java.awt.Window;
  39 import java.awt.image.BufferedImage;
  40 
  41 import javax.swing.JFrame;
  42 import javax.swing.JPanel;
  43 import javax.swing.SwingUtilities;
  44 
  45 public class bug6683775 {
  46     static final int LOC = 100,
  47             SIZE = 200;
  48 
  49     public static void main(String[] args) throws Exception {
  50         GraphicsConfiguration gc = getGC();
  51         if (gc == null || !gc.getDevice().isWindowTranslucencySupported(
  52                 GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSLUCENT)) {
  53             return;
  54         }
  55         Robot robot = new Robot();
  56         final JFrame testFrame = new JFrame(gc);
  57 
  58         SwingUtilities.invokeAndWait(() -> {
  59             JFrame backgroundFrame = new JFrame("Background frame");
  60             backgroundFrame.setUndecorated(true);
  61             JPanel panel = new JPanel();
  62             panel.setBackground(Color.RED);
  63             backgroundFrame.add(panel);
  64             backgroundFrame.setBounds(LOC, LOC, SIZE, SIZE);
  65             backgroundFrame.setVisible(true);
  66 
  67             testFrame.setUndecorated(true);
  68             JPanel p = new JPanel();
  69             p.setOpaque(false);
  70             testFrame.add(p);
  71             setOpaque(testFrame, false);
  72             testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  73             testFrame.setBounds(LOC, LOC, SIZE, SIZE);
  74             testFrame.setVisible(true);
  75         });
  76 
  77         robot.waitForIdle();
  78         Thread.sleep(1500);
  79 
  80         //robot.getPixelColor() didn't work right for some reason
  81         BufferedImage capture =
  82                 robot.createScreenCapture(new Rectangle(LOC, LOC, SIZE, SIZE));
  83 
  84         SwingUtilities.invokeAndWait(testFrame::dispose);
  85 
  86         int redRGB = Color.RED.getRGB();
  87         if (redRGB != capture.getRGB(SIZE/2, SIZE/2)) {
  88             throw new RuntimeException("Transparent frame is not transparent!");
  89         }
  90     }
  91 
  92     public static void setOpaque(Window window, boolean opaque) {
  93         Color bg = window.getBackground();
  94         if (bg == null) {
  95             bg = new Color(0, 0, 0, 0);
  96         }
  97         window.setBackground(new Color(bg.getRed(), bg.getGreen(), bg.getBlue(),
  98                                        opaque ? 255 : 0));
  99     }
 100 
 101     private static GraphicsConfiguration getGC() {
 102         GraphicsConfiguration transparencyCapableGC =
 103                 GraphicsEnvironment.getLocalGraphicsEnvironment()
 104                         .getDefaultScreenDevice().getDefaultConfiguration();
 105         if (!transparencyCapableGC.isTranslucencyCapable()) {
 106             transparencyCapableGC = null;
 107 
 108             GraphicsEnvironment env =
 109                     GraphicsEnvironment.getLocalGraphicsEnvironment();
 110             GraphicsDevice[] devices = env.getScreenDevices();
 111 
 112             for (int i = 0; i < devices.length && transparencyCapableGC == null; i++) {
 113                 GraphicsConfiguration[] configs = devices[i].getConfigurations();
 114                 for (int j = 0; j < configs.length && transparencyCapableGC == null; j++) {
 115                     if (configs[j].isTranslucencyCapable()) {
 116                         transparencyCapableGC = configs[j];
 117                     }
 118                 }
 119             }
 120         }
 121         return transparencyCapableGC;
 122     }
 123 }