1 /*
   2  * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.awt.*;
  25 import javax.swing.*;
  26 import java.awt.image.BufferedImage;
  27 
  28 /*
  29  * @test
  30  * @key headful
  31  * @summary Check if a per-pixel translucent window shows up with correct translucency
  32  * @author mrkam
  33  * @library ../../../../lib/testlibrary
  34  * @build Common ExtendedRobot
  35  * @run main PerPixelTranslucentCanvas
  36  */
  37 
  38 public class PerPixelTranslucentCanvas extends Common {
  39 
  40     JPanel center;
  41     Color OVAL_COLOR = Color.BLUE;
  42 
  43     public static void main(String[] ignored) throws Exception {
  44         FG_COLOR = new Color(200, 0, 0, 100);
  45         BG_COLOR = Color.GREEN;
  46         for (Class<Window> windowClass: WINDOWS_TO_TEST)
  47             new PerPixelTranslucentCanvas(windowClass).doTest();
  48     }
  49 
  50     public PerPixelTranslucentCanvas(Class windowClass) throws Exception {
  51         super(windowClass);
  52     }
  53 
  54     @Override
  55     public void createSwingComponents() {
  56         Container contentPane = RootPaneContainer.class.cast(window).getContentPane();
  57         BorderLayout bl = new BorderLayout(10, 10);
  58         contentPane.setLayout(bl);
  59 
  60         JLabel label = new JLabel("North", new ImageIcon(
  61                 new BufferedImage(30, 30, BufferedImage.TYPE_INT_RGB)), SwingConstants.CENTER);
  62         contentPane.add(label, BorderLayout.NORTH);
  63 
  64         JButton button = new JButton("West");
  65         contentPane.add(button, BorderLayout.WEST);
  66 
  67         center = new JPanel() {
  68             @Override
  69             public void paint(Graphics g) {
  70                 g.setColor(OVAL_COLOR);
  71                 g.fillOval(0, 0, getWidth(), getHeight());
  72             }
  73         };
  74         contentPane.add(center, BorderLayout.CENTER);
  75 
  76         JTextField jTextField = new JTextField("South");
  77         contentPane.add(jTextField, BorderLayout.SOUTH);
  78     }
  79 
  80     @Override
  81     public void doTest() throws Exception {
  82         robot.waitForIdle(delay);
  83 
  84         Rectangle bounds = center.getBounds();
  85         Point loc = center.getLocationOnScreen();
  86 
  87         final int x = loc.x + bounds.width / 2;
  88         final int y = loc.y + bounds.height / 2;
  89 
  90         Color color = robot.getPixelColor(x, y);
  91         if (OVAL_COLOR.getRGB() != color.getRGB())
  92             throw new RuntimeException("bounds = " + bounds + "\n" +
  93                     "loc = " + loc + "\n" +
  94                     "background loc = " + background.getX() + ", " + background.getY() + "\n" +
  95                     "so middle point over background is " + (x - background.getX()) + ", " + (y - background.getY()) + "\n" +
  96                     "Oval is not opaque in the middle point (" + x + ", " + y + ", " + color + ")");
  97 
  98         color = robot.getPixelColor(loc.x - 5, loc.y - 5);
  99         if (FG_COLOR.getRGB() == color.getRGB())
 100             throw new RuntimeException("Background is not translucent (" + color + ")");
 101 
 102         EventQueue.invokeAndWait(this::dispose);
 103         robot.waitForIdle();
 104     }
 105 }