jdk/test/java/awt/Window/TranslucentJAppletTest/TranslucentJAppletTest.java
changeset 2648 aa45a227fce3
child 5506 202f599c92aa
equal deleted inserted replaced
2647:ea80a312972e 2648:aa45a227fce3
       
     1 /*
       
     2  * Copyright 2008-2009 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any 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 JFrame frame;
       
    41     private static volatile boolean paintComponentCalled = false;
       
    42 
       
    43     private static void initAndShowGUI() {
       
    44         frame = new JFrame();
       
    45         JApplet applet = new JApplet();
       
    46         JPanel panel = new JPanel() {
       
    47             protected void paintComponent(Graphics g) {
       
    48                 paintComponentCalled = true;
       
    49                 g.setColor(Color.RED);
       
    50                 g.fillOval(0, 0, getWidth(), getHeight());
       
    51             }
       
    52         };
       
    53         panel.setDoubleBuffered(false);
       
    54         panel.setOpaque(false);
       
    55         applet.add(panel);
       
    56         frame.add(applet);
       
    57         frame.setBounds(100, 100, 200, 200);
       
    58         frame.setUndecorated(true);
       
    59         frame.setBackground(new Color(0, 0, 0, 0));
       
    60         frame.setVisible(true);
       
    61     }
       
    62 
       
    63     public static void main(String[] args)
       
    64         throws Exception
       
    65     {
       
    66         sun.awt.SunToolkit tk = (sun.awt.SunToolkit)Toolkit.getDefaultToolkit();
       
    67 
       
    68         Robot r = new Robot();
       
    69         Color color1 = r.getPixelColor(100, 100); // (0, 0) in frame coordinates
       
    70 
       
    71         SwingUtilities.invokeAndWait(new Runnable() {
       
    72             public void run() {
       
    73                 initAndShowGUI();
       
    74             }
       
    75         });
       
    76         tk.realSync();
       
    77 
       
    78         if (!paintComponentCalled) {
       
    79             throw new RuntimeException("Test FAILED: panel's paintComponent() method is not called");
       
    80         }
       
    81 
       
    82         Color newColor1 = r.getPixelColor(100, 100);
       
    83         // unfortunately, robot.getPixelColor() doesn't work for some unknown reason
       
    84         // Color newColor2 = r.getPixelColor(200, 200);
       
    85         BufferedImage bim = r.createScreenCapture(new Rectangle(200, 200, 1, 1));
       
    86         Color newColor2 = new Color(bim.getRGB(0, 0));
       
    87 
       
    88         // Frame must be transparent at (100, 100) in screen coords
       
    89         if (!color1.equals(newColor1)) {
       
    90             System.err.println("color1 = " + color1);
       
    91             System.err.println("newColor1 = " + newColor1);
       
    92             throw new RuntimeException("Test FAILED: frame pixel at (0, 0) is not transparent");
       
    93         }
       
    94 
       
    95         // Frame must be RED at (200, 200) in screen coords
       
    96         if (!newColor2.equals(Color.RED)) {
       
    97             System.err.println("newColor2 = " + newColor2);
       
    98             throw new RuntimeException("Test FAILED: frame pixel at (100, 100) is not red (transparent?)");
       
    99         }
       
   100 
       
   101         System.out.println("Test PASSED");
       
   102     }
       
   103 }