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