1 /*
   2  * Copyright (c) 2008, 2013, 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 /*
  25  * @test %I% %E%
  26  * @bug 6683728
  27  * @summary Tests that a JApplet in a translucent JFrame works properly
  28  * @author Kenneth.Russell@sun.com: area=Graphics
  29  * @compile -XDignore.symbol.file=true TranslucentJAppletTest.java
  30  * @run main/manual/othervm TranslucentJAppletTest
  31  */
  32 
  33 import java.awt.*;
  34 import java.awt.image.*;
  35 
  36 import javax.swing.*;
  37 
  38 public class TranslucentJAppletTest {
  39 
  40     private static volatile GraphicsConfiguration graphicsConfig = null;
  41     private static JFrame frame;
  42     private static volatile boolean paintComponentCalled = false;
  43 
  44     private static void initAndShowGUI() {
  45         frame = new JFrame(graphicsConfig);
  46         JApplet applet = new JApplet();
  47         applet.setBackground(new Color(0, 0, 0, 0));
  48         JPanel panel = new JPanel() {
  49             protected void paintComponent(Graphics g) {
  50                 paintComponentCalled = true;
  51                 g.setColor(Color.RED);
  52                 g.fillOval(0, 0, getWidth(), getHeight());
  53             }
  54         };
  55         panel.setDoubleBuffered(false);
  56         panel.setOpaque(false);
  57         applet.add(panel);
  58         frame.add(applet);
  59         frame.setBounds(100, 100, 200, 200);
  60         frame.setUndecorated(true);
  61         frame.setBackground(new Color(0, 0, 0, 0));
  62         frame.setVisible(true);
  63     }
  64 
  65     public static void main(String[] args)
  66         throws Exception
  67     {
  68 
  69         final GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
  70         for (GraphicsDevice gd : ge.getScreenDevices()) {
  71             if (gd.isWindowTranslucencySupported(
  72                         GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSLUCENT))
  73             {
  74                 for (GraphicsConfiguration gc : gd.getConfigurations()) {
  75                     if (gc.isTranslucencyCapable()) {
  76                         graphicsConfig = gc;
  77                         break;
  78                     }
  79                 }
  80             }
  81             if (graphicsConfig != null) {
  82                 break;
  83             }
  84         }
  85         if (graphicsConfig == null) {
  86             System.out.println("The system does not support translucency. Consider the test passed.");
  87             return;
  88         }
  89 
  90         Robot r = new Robot();
  91         Color color1 = r.getPixelColor(100, 100); // (0, 0) in frame coordinates
  92 
  93         SwingUtilities.invokeAndWait(new Runnable() {
  94             public void run() {
  95                 initAndShowGUI();
  96             }
  97         });
  98         r.waitForIdle();
  99 
 100         if (!paintComponentCalled) {
 101             throw new RuntimeException("Test FAILED: panel's paintComponent() method is not called");
 102         }
 103         Thread.sleep(1500);
 104 
 105         Color newColor1 = r.getPixelColor(100, 100);
 106         Color newColor2 = r.getPixelColor(200, 200);
 107 
 108         // Frame must be transparent at (100, 100) in screen coords
 109         if (!color1.equals(newColor1)) {
 110             System.err.println("color1 = " + color1);
 111             System.err.println("newColor1 = " + newColor1);
 112             throw new RuntimeException("Test FAILED: frame pixel at (0, 0) is not transparent");
 113         }
 114 
 115         // Frame must be RED at (200, 200) in screen coords
 116         if (!newColor2.equals(Color.RED)) {
 117             System.err.println("newColor2 = " + newColor2);
 118             throw new RuntimeException("Test FAILED: frame pixel at (100, 100) is not red (transparent?)");
 119         }
 120 
 121         System.out.println("Test PASSED");
 122     }
 123 }