Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JWindow/ShapedAndTranslucentWindows/SetShapeAndClickSwing.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.*;25import java.awt.event.ComponentAdapter;26import java.awt.event.ComponentEvent;27import java.awt.event.MouseAdapter;28import java.awt.event.MouseEvent;29import java.awt.geom.Area;30import java.awt.geom.GeneralPath;31import java.awt.geom.Rectangle2D;3233/*34* @test35* @summary Check if a window set with shape clips the contents36* Test Description: Check if PERPIXEL_TRANSPARENT translucency type is supported37* by the current platform. Proceed if it is supported. Apply different types38* of shapes on a Window which contains some awt components. Shape should be39* applied in such a way that some components are partially clipped off. Check40* if the components appear only partially and events work correctly for those41* components - i.e. events occur only on the areas which appear and do not42* occur on the clipped off areas. Events should be checked by clicking on the43* visible and clipped regions. Repeat this for Window, Dialog and Frame.44* Expected Result: If PERPIXEL_TRANSPARENT translucency type is supported, clicking45* on clipped region should deliver the event to the background (it should be46* another Window behind the test window)47* @author mrkam48* @library ../../../../lib/testlibrary49* @build Common ExtendedRobot50* @run main SetShapeAndClickSwing51*/5253public class SetShapeAndClickSwing extends Common {5455Component south, center, north;5657public static void main(String[] args) throws Exception {58if (checkTranslucencyMode(GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSPARENT))59for (Class<Window> windowClass: WINDOWS_TO_TEST)60new SetShapeAndClickSwing(windowClass).doTest();61}6263public SetShapeAndClickSwing(Class windowClass) throws Exception {64super(windowClass);65}6667@Override68public void initBackgroundFrame() {69super.initBackgroundFrame();70background.addMouseListener(new MouseAdapter() {71@Override72public void mouseClicked(MouseEvent e) {73clicked |= 1 << 0;74}75});76}7778@Override79public void createSwingComponents() {80window.setSize(200,200);81window.setLayout(new BorderLayout());8283south = new JLabel("South");84south.addMouseListener(new MouseAdapter() {85@Override86public void mouseClicked(MouseEvent e) {87clicked |= 1 << 3;88}89});90window.add(south, BorderLayout.SOUTH);9192center = new JList();93center.addMouseListener(new MouseAdapter() {94@Override95public void mouseClicked(MouseEvent e) {96clicked |= 1 << 2;97}98});99window.add(center, BorderLayout.CENTER);100101north = new JTextField("North");102north.addMouseListener(new MouseAdapter() {103@Override104public void mouseClicked(MouseEvent e) {105clicked |= 1 << 1;106}107});108window.add(north, BorderLayout.NORTH);109}110111@Override112public void doTest() throws Exception {113114robot.waitForIdle();115116Point wls = window.getLocationOnScreen();117Point ls;118int y;119ls = north.getLocationOnScreen();120checkClick(ls.x + north.getWidth() / 3, ls.y + north.getHeight() / 2, 1);121122ls = center.getLocationOnScreen();123checkClick(ls.x + center.getWidth() * 3 / 4, ls.y + center.getHeight() * 3 / 4, 2);124125ls = south.getLocationOnScreen();126checkClick(ls.x + south.getWidth() * 2 / 3, ls.y + south.getHeight() / 2, 3);127128ls = center.getLocationOnScreen();129checkClick(ls.x + center.getWidth() / 4, ls.y + center.getHeight() / 4, 2);130131ls = north.getLocationOnScreen();132y = ls.y + north.getHeight() / 2;133checkClick(wls.x + 200 - (y - wls.y), y, 0);134135EventQueue.invokeAndWait(window::toFront);136robot.waitForIdle();137138ls = center.getLocationOnScreen();139y = ls.y + center.getHeight() / 2;140checkClick(wls.x + 200 - (y - wls.y), y, 0);141142EventQueue.invokeAndWait(window::toFront);143robot.waitForIdle();144145ls = south.getLocationOnScreen();146y = ls.y + south.getHeight() / 2;147checkClick(wls.x + 200 - (y - wls.y), y, 0);148149EventQueue.invokeAndWait(window::dispose);150EventQueue.invokeAndWait(background::dispose);151152robot.waitForIdle();153}154155@Override156public void applyShape() {157Area shape = new Area(new Rectangle2D.Float(0, 0, 200, 200));158GeneralPath gp;159gp = new GeneralPath();160gp.moveTo(190, 0);161gp.lineTo(200, 0);162gp.lineTo(200, 10);163gp.lineTo(10, 200);164gp.lineTo(0, 200);165gp.lineTo(0, 190);166gp.closePath();167shape.subtract(new Area(gp));168169window.setShape(shape);170}171}172173174