Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JWindow/ShapedAndTranslucentWindows/PerPixelTranslucentCanvas.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 java.awt.*;24import javax.swing.*;25import java.awt.image.BufferedImage;2627/*28* @test29* @summary Check if a per-pixel translucent window shows up with correct translucency30* @author mrkam31* @library ../../../../lib/testlibrary32* @build Common ExtendedRobot33* @run main PerPixelTranslucentCanvas34*/3536public class PerPixelTranslucentCanvas extends Common {3738JPanel center;39Color OVAL_COLOR = Color.BLUE;4041public static void main(String[] ignored) throws Exception {42FG_COLOR = new Color(200, 0, 0, 100);43BG_COLOR = Color.GREEN;44for (Class<Window> windowClass: WINDOWS_TO_TEST)45new PerPixelTranslucentCanvas(windowClass).doTest();46}4748public PerPixelTranslucentCanvas(Class windowClass) throws Exception {49super(windowClass);50}5152@Override53public void createSwingComponents() {54Container contentPane = RootPaneContainer.class.cast(window).getContentPane();55BorderLayout bl = new BorderLayout(10, 10);56contentPane.setLayout(bl);5758JLabel label = new JLabel("North", new ImageIcon(59new BufferedImage(30, 30, BufferedImage.TYPE_INT_RGB)), SwingConstants.CENTER);60contentPane.add(label, BorderLayout.NORTH);6162JButton button = new JButton("West");63contentPane.add(button, BorderLayout.WEST);6465center = new JPanel() {66@Override67public void paint(Graphics g) {68g.setColor(OVAL_COLOR);69g.fillOval(0, 0, getWidth(), getHeight());70}71};72contentPane.add(center, BorderLayout.CENTER);7374JTextField jTextField = new JTextField("South");75contentPane.add(jTextField, BorderLayout.SOUTH);76}7778@Override79public void doTest() throws Exception {80robot.waitForIdle(delay);8182Rectangle bounds = center.getBounds();83Point loc = center.getLocationOnScreen();8485final int x = loc.x + bounds.width / 2;86final int y = loc.y + bounds.height / 2;8788Color color = robot.getPixelColor(x, y);89if (OVAL_COLOR.getRGB() != color.getRGB())90throw new RuntimeException("bounds = " + bounds + "\n" +91"loc = " + loc + "\n" +92"background loc = " + background.getX() + ", " + background.getY() + "\n" +93"so middle point over background is " + (x - background.getX()) + ", " + (y - background.getY()) + "\n" +94"Oval is not opaque in the middle point (" + x + ", " + y + ", " + color + ")");9596color = robot.getPixelColor(loc.x - 5, loc.y - 5);97if (FG_COLOR.getRGB() == color.getRGB())98throw new RuntimeException("Background is not translucent (" + color + ")");99100EventQueue.invokeAndWait(this::dispose);101robot.waitForIdle();102}103}104105106