1 /*
   2  * Copyright (c) 2008, 2009, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 import java.awt.BorderLayout;
  27 import java.awt.Color;
  28 import java.awt.Dimension;
  29 import java.awt.Frame;
  30 import java.awt.Graphics;
  31 import java.awt.GraphicsConfiguration;
  32 import java.awt.GraphicsDevice;
  33 import java.awt.GraphicsDevice.WindowTranslucency;
  34 import java.awt.GraphicsEnvironment;
  35 import java.awt.RenderingHints;
  36 import java.awt.event.MouseAdapter;
  37 import java.awt.event.MouseEvent;
  38 import java.awt.event.WindowAdapter;
  39 import java.awt.event.WindowEvent;
  40 import java.awt.Canvas;
  41 import java.awt.Component;
  42 import java.awt.GradientPaint;
  43 import java.awt.Graphics2D;
  44 import java.awt.Paint;
  45 import java.util.Random;
  46 import java.awt.geom.Ellipse2D;
  47 import javax.swing.JApplet;
  48 import javax.swing.JButton;
  49 import javax.swing.JComponent;
  50 import javax.swing.JFrame;
  51 import javax.swing.JPanel;
  52 import javax.swing.SwingUtilities;
  53 
  54 public class TSFrame {
  55 
  56     static volatile boolean done = false;
  57 
  58     static final boolean useSwing = System.getProperty("useswing") != null;
  59     static final boolean useShape = System.getProperty("useshape") != null;
  60     static final boolean useTransl = System.getProperty("usetransl") != null;
  61     static final boolean useNonOpaque = System.getProperty("usenonop") != null;
  62 
  63     static final Random rnd = new Random();
  64     private static void render(Graphics g, int w, int h, boolean useNonOpaque) {
  65         if (useNonOpaque) {
  66             Graphics2D g2d = (Graphics2D)g;
  67             GradientPaint p =
  68                 new GradientPaint(0.0f, 0.0f,
  69                                   new Color(rnd.nextInt(0xffffff)),
  70                                   w, h,
  71                                   new Color(rnd.nextInt(0xff),
  72                                             rnd.nextInt(0xff),
  73                                             rnd.nextInt(0xff), 0),
  74                                   true);
  75             g2d.setPaint(p);
  76             g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  77                                  RenderingHints.VALUE_ANTIALIAS_ON);
  78             g2d.fillOval(0, 0, w, h);
  79         } else {
  80             g.setColor(new Color(rnd.nextInt(0xffffff)));
  81             g.fillRect(0, 0, w, h);
  82         }
  83     }
  84 
  85     private static class MyCanvas extends Canvas {
  86         @Override
  87         public void paint(Graphics g) {
  88             render(g, getWidth(), getHeight(), false);
  89         }
  90         @Override
  91         public Dimension getPreferredSize() {
  92             return new Dimension(200, 100);
  93         }
  94     }
  95     private static class NonOpaqueJFrame extends JFrame {
  96         NonOpaqueJFrame() {
  97             super("NonOpaque Swing JFrame");
  98             JPanel p = new JPanel() {
  99                 public void paintComponent(Graphics g) {
 100                     super.paintComponent(g);
 101                     render(g, getWidth(), getHeight(), true);
 102                     g.setColor(Color.red);
 103                     g.drawString("Non-Opaque Swing JFrame", 10, 15);
 104                 }
 105             };
 106             p.setDoubleBuffered(false);
 107             p.setOpaque(false);
 108             add(p);
 109             setUndecorated(true);
 110         }
 111     }
 112     private static class NonOpaqueJAppletFrame extends JFrame {
 113         JPanel p;
 114         NonOpaqueJAppletFrame() {
 115             super("NonOpaque Swing JAppletFrame");
 116             JApplet ja = new JApplet() {
 117                 public void paint(Graphics g) {
 118                     super.paint(g);
 119                     System.err.println("JAppletFrame paint called");
 120                 }
 121             };
 122             p = new JPanel() {
 123                 public void paintComponent(Graphics g) {
 124                     super.paintComponent(g);
 125                     render(g, getWidth(), getHeight(), true);
 126                     g.setColor(Color.red);
 127                     g.drawString("Non-Opaque Swing JFrame", 10, 15);
 128                 }
 129             };
 130             p.setDoubleBuffered(false);
 131             p.setOpaque(false);
 132             ja.add(p);
 133             add(ja);
 134             setUndecorated(true);
 135         }
 136     }
 137     private static class NonOpaqueFrame extends Frame {
 138         NonOpaqueFrame() {
 139             super("NonOpaque AWT Frame");
 140             // uncomment to test with hw child
 141 //            setLayout(null);
 142 //            Component c = new Panel() {
 143 //                public void paint(Graphics g) {
 144 //                    g.setColor(new Color(1.0f, 1.0f, 1.0f, 0.5f));
 145 //                    g.fillRect(0, 0, getWidth(), getHeight());
 146 //                }
 147 //            };
 148 //            c.setSize(100, 100);
 149 //            c.setBackground(Color.red);
 150 //            c.setForeground(Color.red);
 151 //            add(c);
 152 //            c.setLocation(130, 130);
 153         }
 154         @Override
 155         public void paint(Graphics g) {
 156             render(g, getWidth(), getHeight(), true);
 157             g.setColor(Color.red);
 158             g.drawString("Non-Opaque AWT Frame", 10, 15);
 159         }
 160     }
 161 
 162     private static class MyJPanel extends JPanel {
 163         @Override
 164         public void paintComponent(Graphics g) {
 165             render(g, getWidth(), getHeight(), false);
 166         }
 167     }
 168 
 169     public static Frame createGui(
 170                                   final boolean useSwing,
 171                                   final boolean useShape,
 172                                   final boolean useTransl,
 173                                   final boolean useNonOpaque,
 174                                   final float factor)
 175     {
 176         Frame frame;
 177         done = false;
 178 
 179         if (useNonOpaque) {
 180             if (useSwing) {
 181                 frame = new NonOpaqueJFrame();
 182 //                frame = new NonOpaqueJAppletFrame(gc);
 183             } else {
 184                 frame = new NonOpaqueFrame();
 185             }
 186             animateComponent(frame);
 187         } else if (useSwing) {
 188             frame = new JFrame("Swing Frame");
 189             JComponent p = new JButton("Swing!");
 190             p.setPreferredSize(new Dimension(200, 100));
 191             frame.add("North", p);
 192             p = new MyJPanel();
 193             animateComponent(p);
 194             frame.add("Center", p);
 195         } else {
 196             frame = new Frame("AWT Frame") {
 197                 public void paint(Graphics g) {
 198                     g.setColor(Color.red);
 199                     g.fillRect(0, 0, 100, 100);
 200                 }
 201             };
 202             frame.setLayout(new BorderLayout());
 203             Canvas c = new MyCanvas();
 204             frame.add("North", c);
 205             animateComponent(c);
 206             c = new MyCanvas();
 207             frame.add("Center", c);
 208             animateComponent(c);
 209             c = new MyCanvas();
 210             frame.add("South", c);
 211             animateComponent(c);
 212         }
 213         final Frame finalFrame = frame;
 214         frame.addWindowListener(new WindowAdapter() {
 215             @Override
 216             public void windowClosing(WindowEvent e) {
 217                 finalFrame.dispose();
 218                 done = true;
 219             }
 220         });
 221         frame.addMouseListener(new MouseAdapter() {
 222             @Override
 223             public void mouseClicked(MouseEvent e) {
 224                 finalFrame.dispose();
 225                 done = true;
 226             }
 227         });
 228         frame.setPreferredSize(new Dimension(800, 600));
 229 
 230         if (useShape) {
 231             frame.setUndecorated(true);
 232         }
 233 
 234         frame.setLocation(450, 10);
 235         frame.pack();
 236 
 237         GraphicsDevice gd = frame.getGraphicsConfiguration().getDevice();
 238         if (useShape) {
 239             if (gd.isWindowTranslucencySupported(WindowTranslucency.PERPIXEL_TRANSPARENT)) {
 240                 System.out.println("applying PERPIXEL_TRANSPARENT");
 241                 frame.setShape(new Ellipse2D.Double(0, 0, frame.getWidth(),
 242                                                     frame.getHeight()/3));
 243                 frame.setTitle("PERPIXEL_TRANSPARENT");
 244             } else {
 245                 System.out.println("Passed: PERPIXEL_TRANSPARENT unsupported");
 246             }
 247         }
 248         if (useTransl) {
 249             if (gd.isWindowTranslucencySupported(WindowTranslucency.TRANSLUCENT)) {
 250                 System.out.println("applying TRANSLUCENT");
 251                 frame.setOpacity(factor);
 252                 frame.setTitle("TRANSLUCENT");
 253             } else {
 254                 System.out.println("Passed: TRANSLUCENT unsupported");
 255             }
 256         }
 257         if (useNonOpaque) {
 258             if (gd.isWindowTranslucencySupported(WindowTranslucency.PERPIXEL_TRANSLUCENT)) {
 259                 System.out.println("applying PERPIXEL_TRANSLUCENT");
 260                 frame.setBackground(new Color(0, 0, 0, 0));
 261                 frame.setTitle("PERPIXEL_TRANSLUCENT");
 262             } else {
 263                 System.out.println("Passed: PERPIXEL_TRANSLUCENT unsupported");
 264             }
 265         }
 266         frame.setVisible(true);
 267         return frame;
 268     }
 269 
 270     public static void stopThreads() {
 271         done = true;
 272     }
 273 
 274     private static void animateComponent(final Component comp) {
 275         Thread t = new Thread(new Runnable() {
 276             public void run() {
 277                 do {
 278                     try {
 279                         Thread.sleep(50);
 280                     } catch (InterruptedException ex) {}
 281                     comp.repaint();
 282                 } while (!done);
 283             }
 284         });
 285         t.start();
 286     }
 287 
 288     public static void main(String[] args) throws Exception {
 289         SwingUtilities.invokeLater(new Runnable() {
 290             public void run() {
 291                 TSFrame.createGui(useSwing,
 292                                   useShape,
 293                                   useTransl,
 294                                   useNonOpaque,
 295                                   0.7f);
 296             }
 297         });
 298     }
 299 }