test/java/awt/Window/TranslucentJAppletTest/TranslucentJAppletTest.java

Print this page




  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 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         applet.setBackground(new Color(0, 0, 0, 0));
  47         JPanel panel = new JPanel() {
  48             protected void paintComponent(Graphics g) {
  49                 paintComponentCalled = true;
  50                 g.setColor(Color.RED);
  51                 g.fillOval(0, 0, getWidth(), getHeight());
  52             }
  53         };
  54         panel.setDoubleBuffered(false);
  55         panel.setOpaque(false);
  56         applet.add(panel);
  57         frame.add(applet);
  58         frame.setBounds(100, 100, 200, 200);
  59         frame.setUndecorated(true);
  60         frame.setBackground(new Color(0, 0, 0, 0));
  61         frame.setVisible(true);
  62     }
  63 
  64     public static void main(String[] args)
  65         throws Exception
  66     {
  67         sun.awt.SunToolkit tk = (sun.awt.SunToolkit)Toolkit.getDefaultToolkit();





















  68 
  69         Robot r = new Robot();
  70         Color color1 = r.getPixelColor(100, 100); // (0, 0) in frame coordinates
  71 
  72         SwingUtilities.invokeAndWait(new Runnable() {
  73             public void run() {
  74                 initAndShowGUI();
  75             }
  76         });
  77         tk.realSync();
  78 
  79         if (!paintComponentCalled) {
  80             throw new RuntimeException("Test FAILED: panel's paintComponent() method is not called");
  81         }
  82 
  83         Color newColor1 = r.getPixelColor(100, 100);
  84         // unfortunately, robot.getPixelColor() doesn't work for some unknown reason
  85         // Color newColor2 = r.getPixelColor(200, 200);
  86         BufferedImage bim = r.createScreenCapture(new Rectangle(200, 200, 1, 1));
  87         Color newColor2 = new Color(bim.getRGB(0, 0));


  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         sun.awt.SunToolkit tk = (sun.awt.SunToolkit)Toolkit.getDefaultToolkit();
  69 
  70         final GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
  71         for (GraphicsDevice gd : ge.getScreenDevices()) {
  72             if (gd.isWindowTranslucencySupported(
  73                         GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSLUCENT))
  74             {
  75                 for (GraphicsConfiguration gc : gd.getConfigurations()) {
  76                     if (gc.isTranslucencyCapable()) {
  77                         graphicsConfig = gc;
  78                         break;
  79                     }
  80                 }
  81             }
  82             if (graphicsConfig != null) {
  83                 break;
  84             }
  85         }
  86         if (graphicsConfig == null) {
  87             System.err.println("The system does not support translucency. Consider the test passed.");
  88             return;
  89         }
  90 
  91         Robot r = new Robot();
  92         Color color1 = r.getPixelColor(100, 100); // (0, 0) in frame coordinates
  93 
  94         SwingUtilities.invokeAndWait(new Runnable() {
  95             public void run() {
  96                 initAndShowGUI();
  97             }
  98         });
  99         tk.realSync();
 100 
 101         if (!paintComponentCalled) {
 102             throw new RuntimeException("Test FAILED: panel's paintComponent() method is not called");
 103         }
 104 
 105         Color newColor1 = r.getPixelColor(100, 100);
 106         // unfortunately, robot.getPixelColor() doesn't work for some unknown reason
 107         // Color newColor2 = r.getPixelColor(200, 200);
 108         BufferedImage bim = r.createScreenCapture(new Rectangle(200, 200, 1, 1));
 109         Color newColor2 = new Color(bim.getRGB(0, 0));