Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JWindow/ShapedAndTranslucentWindows/PerPixelTranslucentSwing.java
38853 views
/*1* Copyright (c) 2010, 2014, 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*/2223import javax.swing.*;24import java.awt.*;2526/*27* @test28* @summary Check if a per-pixel translucent window shows only the area having29* opaque pixels30* Test Description: Check if PERPIXEL_TRANSLUCENT Translucency type is supported31* on the current platform. Proceed if it is supported. Create a swing window32* with some swing components in it and a transparent background (alpha 0.0).33* Bring this window on top of a known background. Do this test for JFrame,34* JWindow and JDialog35* Expected Result: Only the components present in the window must be shown. Other36* areas of the window must be transparent so that the background shows37* @author mrkam38* @library ../../../../lib/testlibrary39* @build Common ExtendedRobot40* @run main PerPixelTranslucentSwing41*/4243public class PerPixelTranslucentSwing extends Common {4445JButton north;4647public static void main(String[] ignored) throws Exception {48FG_COLOR = new Color(200, 0, 0, 0);49if (checkTranslucencyMode(GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSLUCENT))50for (Class<Window> windowClass: WINDOWS_TO_TEST)51new PerPixelTranslucentSwing(windowClass).doTest();52}5354public PerPixelTranslucentSwing(Class windowClass) throws Exception {55super(windowClass);56}5758@Override59public void createSwingComponents() {60Container contentPane = RootPaneContainer.class.cast(window).getContentPane();61BorderLayout bl = new BorderLayout(10, 5);62contentPane.setLayout(bl);6364north = new JButton("North");65contentPane.add(north, BorderLayout.NORTH);6667JList center = new JList(new String[] {"Center"});68contentPane.add(center, BorderLayout.CENTER);6970JTextField south = new JTextField("South");71contentPane.add(south, BorderLayout.SOUTH);7273window.pack();74window.setVisible(true);7576north.requestFocus();77}7879@Override80public void doTest() throws Exception {81robot.waitForIdle(delay);8283// Check for background translucency84Rectangle bounds = north.getBounds();85Point loc = north.getLocationOnScreen();8687Color color = robot.getPixelColor(loc.x + bounds.width / 2, loc.y + bounds.height + 3);88System.out.println(color);89if (FG_COLOR.getRGB() == color.getRGB())90throw new RuntimeException("Background is not translucent (" + color + ")");9192EventQueue.invokeAndWait(this::dispose);93}94}959697