Path: blob/master/test/jdk/javax/swing/JDialog/Transparency/TransparencyTest.java
41152 views
/*1* Copyright (c) 2016, 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 8062946 815990627@summary Verify Transparency upon iconify/deiconify sequence28@run main TransparencyTest29*/30import java.awt.GraphicsEnvironment;31import java.awt.GraphicsDevice;32import java.awt.Color;33import java.awt.Point;34import java.awt.Robot;35import javax.swing.JDialog;36import javax.swing.JFrame;37import javax.swing.SwingUtilities;3839public class TransparencyTest {4041private static JFrame frame;42private static JDialog dialog;43private static JDialog backgroundDialog;44private static final int WIDTH = 250;45private static final int HEIGHT = 250;46private static final float OPACITY = 0.60f;47private static volatile Point dlgPos;4849public static void createAndShowGUI() {50frame = new JFrame("JFrame");51frame.setSize(WIDTH, HEIGHT);52frame.setLocation(100, 300);5354dialog = new JDialog(frame, false);55dialog.setSize(250, 250);56dialog.setUndecorated(true);57dialog.setLocation(400, 300);58dlgPos = dialog.getLocation();59backgroundDialog = new JDialog(frame, false);60backgroundDialog.setSize(250, 250);61backgroundDialog.setUndecorated(true);62backgroundDialog.getContentPane().setBackground(Color.red);63backgroundDialog.setLocation(dlgPos.x, dlgPos.y);6465frame.setVisible(true);66backgroundDialog.setVisible(true);67dialog.setVisible(true);68}6970public static void main(String[] args) throws Exception {7172GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();73GraphicsDevice gd = ge.getDefaultScreenDevice();74GraphicsDevice.WindowTranslucency mode = GraphicsDevice.WindowTranslucency.TRANSLUCENT;75boolean translucencyCheck = gd.isWindowTranslucencySupported(mode);76if(!translucencyCheck) {77return;78}7980Robot robot = new Robot();81// create a GUI82SwingUtilities.invokeAndWait(new Runnable() {8384@Override85public void run() {86createAndShowGUI();87}88});89robot.waitForIdle();90robot.delay(200);9192int x = dlgPos.x + 100;93int y = dlgPos.y + 100;94Color opaque = robot.getPixelColor(x, y);9596// set Dialog Opacity97SwingUtilities.invokeAndWait(new Runnable() {9899@Override100public void run() {101dialog.setOpacity(OPACITY);102}103});104robot.waitForIdle();105robot.delay(200);106107// iconify frame108SwingUtilities.invokeAndWait(new Runnable() {109110@Override111public void run() {112frame.setExtendedState(JFrame.ICONIFIED);113}114});115robot.waitForIdle();116robot.delay(500);117118// deiconify frame119SwingUtilities.invokeAndWait(new Runnable() {120121@Override122public void run() {123frame.setExtendedState(JFrame.NORMAL);124}125});126robot.waitForIdle();127robot.delay(500);128129Color transparent = robot.getPixelColor(x, y);130if (transparent.equals(opaque)) {131frame.dispose();132throw new RuntimeException("JDialog transparency lost "133+ "upon iconify/deiconify sequence");134}135frame.dispose();136}137}138139140