Path: blob/master/test/jdk/javax/swing/JComponent/6683775/bug6683775.java
41155 views
/*1* Copyright (c) 2009, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @key headful26* @bug 6683775 6794764 818661727* @summary Painting artifacts is seen when panel is made setOpaque(false) for a28* translucent window29*/3031import java.awt.Color;32import java.awt.GraphicsConfiguration;33import java.awt.GraphicsDevice;34import java.awt.GraphicsEnvironment;35import java.awt.Rectangle;36import java.awt.Robot;37import java.awt.Window;38import java.awt.image.BufferedImage;3940import javax.swing.JFrame;41import javax.swing.JPanel;42import javax.swing.SwingUtilities;4344public class bug6683775 {45static final int LOC = 100,46SIZE = 200;4748public static void main(String[] args) throws Exception {49GraphicsConfiguration gc = getGC();50if (gc == null || !gc.getDevice().isWindowTranslucencySupported(51GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSLUCENT)) {52return;53}54Robot robot = new Robot();55final JFrame testFrame = new JFrame(gc);5657SwingUtilities.invokeAndWait(() -> {58JFrame backgroundFrame = new JFrame("Background frame");59backgroundFrame.setUndecorated(true);60JPanel panel = new JPanel();61panel.setBackground(Color.RED);62backgroundFrame.add(panel);63backgroundFrame.setBounds(LOC, LOC, SIZE, SIZE);64backgroundFrame.setVisible(true);6566testFrame.setUndecorated(true);67JPanel p = new JPanel();68p.setOpaque(false);69testFrame.add(p);70setOpaque(testFrame, false);71testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);72testFrame.setBounds(LOC, LOC, SIZE, SIZE);73testFrame.setVisible(true);74});7576robot.waitForIdle();77Thread.sleep(1500);7879//robot.getPixelColor() didn't work right for some reason80BufferedImage capture =81robot.createScreenCapture(new Rectangle(LOC, LOC, SIZE, SIZE));8283SwingUtilities.invokeAndWait(testFrame::dispose);8485int redRGB = Color.RED.getRGB();86if (redRGB != capture.getRGB(SIZE/2, SIZE/2)) {87throw new RuntimeException("Transparent frame is not transparent!");88}89}9091public static void setOpaque(Window window, boolean opaque) {92Color bg = window.getBackground();93if (bg == null) {94bg = new Color(0, 0, 0, 0);95}96window.setBackground(new Color(bg.getRed(), bg.getGreen(), bg.getBlue(),97opaque ? 255 : 0));98}99100private static GraphicsConfiguration getGC() {101GraphicsConfiguration transparencyCapableGC =102GraphicsEnvironment.getLocalGraphicsEnvironment()103.getDefaultScreenDevice().getDefaultConfiguration();104if (!transparencyCapableGC.isTranslucencyCapable()) {105transparencyCapableGC = null;106107GraphicsEnvironment env =108GraphicsEnvironment.getLocalGraphicsEnvironment();109GraphicsDevice[] devices = env.getScreenDevices();110111for (int i = 0; i < devices.length && transparencyCapableGC == null; i++) {112GraphicsConfiguration[] configs = devices[i].getConfigurations();113for (int j = 0; j < configs.length && transparencyCapableGC == null; j++) {114if (configs[j].isTranslucencyCapable()) {115transparencyCapableGC = configs[j];116}117}118}119}120return transparencyCapableGC;121}122}123124125