Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JWindow/ShapedAndTranslucentWindows/Common.java
38853 views
/*1* Copyright (c) 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 javax.swing.border.EmptyBorder;25import java.awt.*;26import java.awt.event.*;27import java.awt.geom.Area;28import java.awt.geom.Rectangle2D;29import java.awt.image.BufferedImage;30import java.security.SecureRandom;3132public abstract class Common {3334ExtendedRobot robot;35Class<? extends JFrame> windowClass;36JFrame background;37BufferedImage foreground;38Window window;39volatile boolean gradientBackgroundEnabled = false;40volatile int gradientWidth = 255;41volatile int gradientHeight = 255;4243float opacity = 1.0f;44float perPixelTranslucency = 1.0f;45static Color BG_COLOR = Color.BLUE;46static Color FG_COLOR = Color.RED;47static final int delay = 1000;48static final SecureRandom random = new SecureRandom();49static final int dl = 100;50static final Class[] WINDOWS_TO_TEST = { JWindow.class, JFrame.class, JDialog.class };5152volatile int clicked;5354public Common(Class windowClass, float opacity, float perPixelTranslucency, boolean gradient) throws Exception {55this.gradientBackgroundEnabled = gradient;56this.perPixelTranslucency = perPixelTranslucency;57this.opacity = opacity;58robot = new ExtendedRobot();59this.windowClass = windowClass;60EventQueue.invokeAndWait(this::initBackgroundFrame);61EventQueue.invokeAndWait(this::initGUI);62}6364public Common(Class windowClass) throws Exception {65this(windowClass, 1.0f, 1.0f, false);66}6768public Common(Class windowClass, boolean gradient) throws Exception {69this(windowClass, 1.0f, 1.0f, gradient);70}7172public abstract void doTest() throws Exception;7374public void dispose() {75window.dispose();76background.dispose();77}7879public void applyShape() {};8081public void applyDynamicShape() {82final Area a = new Area();83Dimension size = window.getSize();84for (int x = 0; x < 3; x++) {85for (int y = 0; y < 3; y++) {86a.add(new Area(new Rectangle2D.Double(87x * size.getWidth() / 17*6, y * size.getHeight() / 17*6,88size.getWidth() / 17*5, size.getHeight() / 17*5)));89}90}91window.setShape(a);92}9394public BufferedImage getForegroundWindow() throws Exception {95final BufferedImage f[] = new BufferedImage[1];96EventQueue.invokeAndWait( () -> {97f[0] = new BufferedImage(window.getWidth(),98window.getHeight(), BufferedImage.TYPE_INT_RGB);99window.printAll(f[0].createGraphics());100});101robot.waitForIdle(delay);102return f[0];103}104105public static boolean checkTranslucencyMode(GraphicsDevice.WindowTranslucency mode) {106107if (!GraphicsEnvironment108.getLocalGraphicsEnvironment()109.getDefaultScreenDevice()110.isWindowTranslucencySupported(mode)){111System.out.println(mode+" translucency mode isn't supported");112return false;113} else {114return true;115}116117}118119public void applyAppDragNResizeSupport() {120MouseAdapter m = new MouseAdapter() {121122private Point dragOrigin = null;123private Dimension origSize = null;124private Point origLoc = null;125private boolean left = false;126private boolean top = false;127private boolean bottom = false;128private boolean right = false;129130public void mousePressed(MouseEvent e) {131dragOrigin = e.getLocationOnScreen();132origSize = window.getSize();133origLoc = window.getLocationOnScreen();134right = (origLoc.x + window.getWidth() - dragOrigin.x) < 5;135left = !right && dragOrigin.x - origLoc.x < 5;136bottom = (origLoc.y + window.getHeight() - dragOrigin.y) < 5;137top = !bottom && dragOrigin.y - origLoc.y < 5;138}139140public void mouseReleased(MouseEvent e) { resize(e); }141public void mouseDragged(MouseEvent e) { resize(e); }142143void resize(MouseEvent e) {144Point dragDelta = e.getLocationOnScreen();145dragDelta.translate(-dragOrigin.x, -dragOrigin.y);146Point newLoc = new Point(origLoc);147newLoc.translate(dragDelta.x, dragDelta.y);148Dimension newSize = new Dimension(origSize);149if (left || right) {150newSize.width += right ? dragDelta.x : -dragDelta.x;151}152if (top || bottom) {153newSize.height += bottom ? dragDelta.y : -dragDelta.y;154}155if (right || (top || bottom) && !left) {156newLoc.x = origLoc.x;157}158if (bottom || (left || right) && !top) {159newLoc.y = origLoc.y;160}161window.setBounds(newLoc.x, newLoc.y, newSize.width, newSize.height);162}163};164for (Component comp : window.getComponents()) {165comp.addMouseListener(m);166comp.addMouseMotionListener(m);167}168169window.addMouseListener(m);170window.addMouseMotionListener(m);171}172173public void checkTranslucentShape() throws Exception {174foreground = getForegroundWindow();175Point[] points = new Point[4];176177Dimension size = window.getSize();178Point location = window.getLocationOnScreen();179180points[0] = new Point(20, 20);181points[1] = new Point(20, size.height-20);182points[2] = new Point(size.width-20, 20);183points[3] = new Point(size.width-20, size.height-20);184185for (Point p : points){186p.translate(location.x, location.y);187Color actual = robot.getPixelColor(p.x, p.y);188if (actual.equals(BG_COLOR)|| actual.equals(FG_COLOR))189throw new RuntimeException("Error in point "+p+": "+actual+" equals to foreground or background color");190else191System.out.println("OK with foreground point "+p);192}193}194195public void checkDynamicShape() throws Exception {196Point[] points = new Point[4];197198Dimension size = window.getSize();199200int blockSizeX = (int) (size.getWidth() / 17);201int blockSizeY = (int) (size.getHeight() / 17);202203// background204points[0] = new Point((int) (blockSizeX * 5.5), (int) (blockSizeY * 5.5));205points[1] = new Point((int) (size.getWidth() - blockSizeX * 5.5), (int) (size.getHeight() - blockSizeY * 5.5));206points[2] = new Point((int) (blockSizeX * 5.5), (int) (size.getHeight() - blockSizeY * 5.5));207points[3] = new Point((int) (size.getWidth() - blockSizeX * 5.5), (int) (blockSizeY * 5.5));208checkShape(points, true);209210// foreground211if (opacity < 1.0f){212checkTranslucentShape();213} else {214points[0] = new Point(3 * blockSizeX, 3 * blockSizeY);215points[1] = new Point(14 * blockSizeX, 14 * blockSizeY);216points[2] = new Point(3 * blockSizeX, 14 * blockSizeY);217points[3] = new Point(14 * blockSizeX, 3 * blockSizeY);218checkShape(points, false);219}220}221222public void checkShape(Point[] points, boolean areBackgroundPoints) throws Exception {223224Point location = window.getLocationOnScreen();225226for (Point p : points) {227p.translate(location.x, location.y);228Color pixel = robot.getPixelColor(p.x, p.y);229if (areBackgroundPoints) {230if (pixel.getRed() != 0231|| pixel.getGreen() != 0 )232throw new RuntimeException("Background point " + p +233" color " + pixel +234" does not equal to background color " + BG_COLOR);235else236System.out.println("OK with background point " + p);237} else {238if (pixel.equals(BG_COLOR))239throw new RuntimeException("Foreground point " + p +240" color " + pixel +241" equals to background color " + BG_COLOR);242else243System.out.println("OK with foreground point " + p);244}245}246}247248public void initBackgroundFrame() {249background = new JFrame();250background.setUndecorated(true);251background.getContentPane().setBackground(BG_COLOR);252background.setSize(500, 500);253background.setLocation(dl, dl);254background.setVisible(true);255}256257public void initGUI() {258Container contentPane;259if (windowClass.equals(Frame.class)) {260window = new JFrame();261((JFrame) window).setUndecorated(true);262contentPane = ((JFrame) window).getContentPane();263} else if (windowClass.equals(Dialog.class)) {264window = new JDialog(background);265((JDialog) window).setUndecorated(true);266contentPane = ((JDialog) window).getContentPane();267} else {268window = new JWindow(background);269contentPane = ((JWindow) window).getContentPane();270}271272if (perPixelTranslucency < 1.0f) {273contentPane.setBackground(colorWithOpacity(FG_COLOR, perPixelTranslucency));274window.setBackground(colorWithOpacity(FG_COLOR, perPixelTranslucency));275} else {276contentPane.setBackground(FG_COLOR);277window.setBackground(FG_COLOR);278}279280window.setLocation(2 * dl, 2 * dl);281window.setSize(255, 255);282window.setPreferredSize(new Dimension(255, 255));283createSwingComponents();284if (opacity < 1.0f)285window.setOpacity(opacity);286287window.addComponentListener(new ComponentAdapter() {288@Override289public void componentResized(ComponentEvent e) {290applyShape();291}292});293applyShape();294window.setVisible(true);295applyAppDragNResizeSupport();296window.toFront();297}298299public void createSwingComponents() {300Container contentPane;301if (gradientBackgroundEnabled) {302JPanel jPanel = new JPanel() {303@Override304protected void paintComponent(Graphics g) {305if (g instanceof Graphics2D) {306Color background = Color.RED;307Paint p = new GradientPaint(0.0f, 0.0f, colorWithOpacity(background, 0),3080.0f, gradientHeight - 3, colorWithOpacity(background, 1), true);309Graphics2D g2d = (Graphics2D) g;310g2d.setPaint(p);311g2d.fillRect(0, 3, gradientWidth, gradientHeight - 3);312} else {313super.paintComponent(g);314}315}316};317jPanel.setBorder(new EmptyBorder(15, 5, 5, 5));318jPanel.setOpaque(false);319320contentPane = jPanel;321322RootPaneContainer.class.cast(window).setContentPane(contentPane);323} else {324contentPane = RootPaneContainer.class.cast(window).getContentPane();325}326contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));327328JButton button = new JButton("JButton");329window.add(button);330331JTextArea textArea = new JTextArea("JTextArea");332window.add(textArea);333334JCheckBox checkbox = new JCheckBox("JCheckBox");335checkbox.setOpaque(false);336window.add(checkbox);337338JComboBox comboBox = new JComboBox(new String[]{"JComboBox", "Some item"});339window.add(comboBox);340341JLabel label = new JLabel("JLabel");342window.add(label);343344JTextField textField = new JTextField("JTextField");345window.add(textField);346347JPanel panel = new JPanel();348panel.setOpaque(false);349window.add(panel);350351JComboBox comboBox2 = new JComboBox(new String[]{"JComboBox2", "Another item"});352window.add(comboBox2);353354JRadioButton radioButton = new JRadioButton("JRadioButton");355radioButton.setOpaque(false);356window.add(radioButton);357}358359Color colorWithOpacity(Color color, float opacity) {360return new Color(color.getColorSpace(), color.getColorComponents(null), opacity);361}362363public void checkTranslucent() throws Exception {364checkTranslucentShape();365366// Drag367Point location = window.getLocationOnScreen();368robot.dragAndDrop(location.x + 30, location.y + 5, location.x + dl + random.nextInt(dl), location.y + random.nextInt(dl));369robot.waitForIdle(delay);370checkTranslucentShape();371372// Resize373location = window.getLocationOnScreen();374robot.dragAndDrop(location.x + 4, location.y + 4, location.x + random.nextInt(2*dl)-dl, location.y + random.nextInt(2*dl)-dl);375robot.waitForIdle(delay);376checkTranslucentShape();377378EventQueue.invokeAndWait(this::dispose);379}380381public void checkDynamic() throws Exception {382checkDynamicShape();383384// Drag385Point location = window.getLocationOnScreen();386robot.dragAndDrop(location.x + 30, location.y + 5, location.x + dl + random.nextInt(dl), location.y + random.nextInt(dl));387robot.waitForIdle(delay);388checkDynamicShape();389390// Resize391location = window.getLocationOnScreen();392robot.dragAndDrop(location.x + 4, location.y + 4, location.x + random.nextInt(2*dl)-dl, location.y + random.nextInt(2*dl)-dl);393robot.waitForIdle(delay);394checkDynamicShape();395396EventQueue.invokeAndWait(this::dispose);397}398399void checkClick(int x, int y, int flag) throws Exception {400401System.out.println("Trying to click point " + x + ", " + y + ", looking for " + flag + " flag to trigger.");402403clicked = 0;404robot.mouseMove(x, y);405robot.click();406407for (int i = 0; i < 100; i++)408if ((clicked & (1 << flag)) == 0)409robot.delay(50);410else411break;412413if ((clicked & (1 << flag)) == 0)414throw new RuntimeException("FAIL: Flag " + flag + " is not triggered for point " + x + ", " + y + "!");415}416}417418419