1163N/A/*
2362N/A * Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved.
1163N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1163N/A *
1163N/A * This code is free software; you can redistribute it and/or modify it
1163N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
1163N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
1163N/A *
1163N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1163N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1163N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1163N/A * version 2 for more details (a copy is included in the LICENSE file that
1163N/A * accompanied this code).
1163N/A *
1163N/A * You should have received a copy of the GNU General Public License version
1163N/A * 2 along with this work; if not, write to the Free Software Foundation,
1163N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1163N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1163N/A */
1163N/A
1163N/Aimport java.awt.BorderLayout;
1163N/Aimport java.awt.Color;
1163N/Aimport java.awt.Dimension;
1163N/Aimport java.awt.Frame;
1163N/Aimport java.awt.Graphics;
1163N/Aimport java.awt.GraphicsConfiguration;
1163N/Aimport java.awt.GraphicsDevice;
1163N/Aimport java.awt.GraphicsDevice.WindowTranslucency;
1163N/Aimport java.awt.GraphicsEnvironment;
1163N/Aimport java.awt.RenderingHints;
1163N/Aimport java.awt.event.MouseAdapter;
1163N/Aimport java.awt.event.MouseEvent;
1163N/Aimport java.awt.event.WindowAdapter;
1163N/Aimport java.awt.event.WindowEvent;
1163N/Aimport java.awt.Canvas;
1163N/Aimport java.awt.Component;
1163N/Aimport java.awt.GradientPaint;
1163N/Aimport java.awt.Graphics2D;
1163N/Aimport java.awt.Paint;
1163N/Aimport java.util.Random;
1163N/Aimport java.awt.geom.Ellipse2D;
1163N/Aimport javax.swing.JApplet;
1163N/Aimport javax.swing.JButton;
1163N/Aimport javax.swing.JComponent;
1163N/Aimport javax.swing.JFrame;
1163N/Aimport javax.swing.JPanel;
1163N/Aimport javax.swing.SwingUtilities;
1163N/A
1163N/Apublic class TSFrame {
1163N/A
1163N/A static volatile boolean done = false;
1163N/A
1163N/A static final boolean useSwing = System.getProperty("useswing") != null;
1163N/A static final boolean useShape = System.getProperty("useshape") != null;
1163N/A static final boolean useTransl = System.getProperty("usetransl") != null;
1163N/A static final boolean useNonOpaque = System.getProperty("usenonop") != null;
1163N/A
1163N/A static final Random rnd = new Random();
1163N/A private static void render(Graphics g, int w, int h, boolean useNonOpaque) {
1163N/A if (useNonOpaque) {
1163N/A Graphics2D g2d = (Graphics2D)g;
1163N/A GradientPaint p =
1163N/A new GradientPaint(0.0f, 0.0f,
1163N/A new Color(rnd.nextInt(0xffffff)),
1163N/A w, h,
1163N/A new Color(rnd.nextInt(0xff),
1163N/A rnd.nextInt(0xff),
1163N/A rnd.nextInt(0xff), 0),
1163N/A true);
1163N/A g2d.setPaint(p);
1163N/A g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
1163N/A RenderingHints.VALUE_ANTIALIAS_ON);
1163N/A g2d.fillOval(0, 0, w, h);
1163N/A } else {
1163N/A g.setColor(new Color(rnd.nextInt(0xffffff)));
1163N/A g.fillRect(0, 0, w, h);
1163N/A }
1163N/A }
1163N/A
1163N/A private static class MyCanvas extends Canvas {
1163N/A @Override
1163N/A public void paint(Graphics g) {
1163N/A render(g, getWidth(), getHeight(), false);
1163N/A }
1163N/A @Override
1163N/A public Dimension getPreferredSize() {
1163N/A return new Dimension(200, 100);
1163N/A }
1163N/A }
1163N/A private static class NonOpaqueJFrame extends JFrame {
1163N/A NonOpaqueJFrame() {
1163N/A super("NonOpaque Swing JFrame");
1163N/A JPanel p = new JPanel() {
1163N/A public void paintComponent(Graphics g) {
1163N/A super.paintComponent(g);
1163N/A render(g, getWidth(), getHeight(), true);
1163N/A g.setColor(Color.red);
1163N/A g.drawString("Non-Opaque Swing JFrame", 10, 15);
1163N/A }
1163N/A };
1163N/A p.setDoubleBuffered(false);
1163N/A p.setOpaque(false);
1163N/A add(p);
1163N/A setUndecorated(true);
1163N/A }
1163N/A }
1163N/A private static class NonOpaqueJAppletFrame extends JFrame {
1163N/A JPanel p;
1163N/A NonOpaqueJAppletFrame() {
1163N/A super("NonOpaque Swing JAppletFrame");
1163N/A JApplet ja = new JApplet() {
1163N/A public void paint(Graphics g) {
1163N/A super.paint(g);
1163N/A System.err.println("JAppletFrame paint called");
1163N/A }
1163N/A };
1163N/A p = new JPanel() {
1163N/A public void paintComponent(Graphics g) {
1163N/A super.paintComponent(g);
1163N/A render(g, getWidth(), getHeight(), true);
1163N/A g.setColor(Color.red);
1163N/A g.drawString("Non-Opaque Swing JFrame", 10, 15);
1163N/A }
1163N/A };
1163N/A p.setDoubleBuffered(false);
1163N/A p.setOpaque(false);
1163N/A ja.add(p);
1163N/A add(ja);
1163N/A setUndecorated(true);
1163N/A }
1163N/A }
1163N/A private static class NonOpaqueFrame extends Frame {
1163N/A NonOpaqueFrame() {
1163N/A super("NonOpaque AWT Frame");
1163N/A // uncomment to test with hw child
1163N/A// setLayout(null);
1163N/A// Component c = new Panel() {
1163N/A// public void paint(Graphics g) {
1163N/A// g.setColor(new Color(1.0f, 1.0f, 1.0f, 0.5f));
1163N/A// g.fillRect(0, 0, getWidth(), getHeight());
1163N/A// }
1163N/A// };
1163N/A// c.setSize(100, 100);
1163N/A// c.setBackground(Color.red);
1163N/A// c.setForeground(Color.red);
1163N/A// add(c);
1163N/A// c.setLocation(130, 130);
1163N/A }
1163N/A @Override
1163N/A public void paint(Graphics g) {
1163N/A render(g, getWidth(), getHeight(), true);
1163N/A g.setColor(Color.red);
1163N/A g.drawString("Non-Opaque AWT Frame", 10, 15);
1163N/A }
1163N/A }
1163N/A
1163N/A private static class MyJPanel extends JPanel {
1163N/A @Override
1163N/A public void paintComponent(Graphics g) {
1163N/A render(g, getWidth(), getHeight(), false);
1163N/A }
1163N/A }
1163N/A
1163N/A public static Frame createGui(
1163N/A final boolean useSwing,
1163N/A final boolean useShape,
1163N/A final boolean useTransl,
1163N/A final boolean useNonOpaque,
1163N/A final float factor)
1163N/A {
1163N/A Frame frame;
1163N/A done = false;
1163N/A
1163N/A if (useNonOpaque) {
1163N/A if (useSwing) {
1163N/A frame = new NonOpaqueJFrame();
1163N/A// frame = new NonOpaqueJAppletFrame(gc);
1163N/A } else {
1163N/A frame = new NonOpaqueFrame();
1163N/A }
1163N/A animateComponent(frame);
1163N/A } else if (useSwing) {
1163N/A frame = new JFrame("Swing Frame");
1163N/A JComponent p = new JButton("Swing!");
1163N/A p.setPreferredSize(new Dimension(200, 100));
1163N/A frame.add("North", p);
1163N/A p = new MyJPanel();
1163N/A animateComponent(p);
1163N/A frame.add("Center", p);
1163N/A } else {
1163N/A frame = new Frame("AWT Frame") {
1163N/A public void paint(Graphics g) {
1163N/A g.setColor(Color.red);
1163N/A g.fillRect(0, 0, 100, 100);
1163N/A }
1163N/A };
1163N/A frame.setLayout(new BorderLayout());
1163N/A Canvas c = new MyCanvas();
1163N/A frame.add("North", c);
1163N/A animateComponent(c);
1163N/A c = new MyCanvas();
1163N/A frame.add("Center", c);
1163N/A animateComponent(c);
1163N/A c = new MyCanvas();
1163N/A frame.add("South", c);
1163N/A animateComponent(c);
1163N/A }
1163N/A final Frame finalFrame = frame;
1163N/A frame.addWindowListener(new WindowAdapter() {
1163N/A @Override
1163N/A public void windowClosing(WindowEvent e) {
1163N/A finalFrame.dispose();
1163N/A done = true;
1163N/A }
1163N/A });
1163N/A frame.addMouseListener(new MouseAdapter() {
1163N/A @Override
1163N/A public void mouseClicked(MouseEvent e) {
1163N/A finalFrame.dispose();
1163N/A done = true;
1163N/A }
1163N/A });
1163N/A frame.setPreferredSize(new Dimension(800, 600));
1163N/A
1163N/A if (useShape) {
1163N/A frame.setUndecorated(true);
1163N/A }
1163N/A
1163N/A frame.setLocation(450, 10);
1163N/A frame.pack();
1163N/A
1163N/A GraphicsDevice gd = frame.getGraphicsConfiguration().getDevice();
1163N/A if (useShape) {
1163N/A if (gd.isWindowTranslucencySupported(WindowTranslucency.PERPIXEL_TRANSPARENT)) {
1163N/A System.out.println("applying PERPIXEL_TRANSPARENT");
1163N/A frame.setShape(new Ellipse2D.Double(0, 0, frame.getWidth(),
1163N/A frame.getHeight()/3));
1163N/A frame.setTitle("PERPIXEL_TRANSPARENT");
1163N/A } else {
1163N/A System.out.println("Passed: PERPIXEL_TRANSPARENT unsupported");
1163N/A }
1163N/A }
1163N/A if (useTransl) {
1163N/A if (gd.isWindowTranslucencySupported(WindowTranslucency.TRANSLUCENT)) {
1163N/A System.out.println("applying TRANSLUCENT");
1163N/A frame.setOpacity(factor);
1163N/A frame.setTitle("TRANSLUCENT");
1163N/A } else {
1163N/A System.out.println("Passed: TRANSLUCENT unsupported");
1163N/A }
1163N/A }
1163N/A if (useNonOpaque) {
1163N/A if (gd.isWindowTranslucencySupported(WindowTranslucency.PERPIXEL_TRANSLUCENT)) {
1163N/A System.out.println("applying PERPIXEL_TRANSLUCENT");
1163N/A frame.setBackground(new Color(0, 0, 0, 0));
1163N/A frame.setTitle("PERPIXEL_TRANSLUCENT");
1163N/A } else {
1163N/A System.out.println("Passed: PERPIXEL_TRANSLUCENT unsupported");
1163N/A }
1163N/A }
1163N/A frame.setVisible(true);
1163N/A return frame;
1163N/A }
1163N/A
1163N/A public static void stopThreads() {
1163N/A done = true;
1163N/A }
1163N/A
1163N/A private static void animateComponent(final Component comp) {
1163N/A Thread t = new Thread(new Runnable() {
1163N/A public void run() {
1163N/A do {
1163N/A try {
1163N/A Thread.sleep(50);
1163N/A } catch (InterruptedException ex) {}
1163N/A comp.repaint();
1163N/A } while (!done);
1163N/A }
1163N/A });
1163N/A t.start();
1163N/A }
1163N/A
1163N/A public static void main(String[] args) throws Exception {
1163N/A SwingUtilities.invokeLater(new Runnable() {
1163N/A public void run() {
1163N/A TSFrame.createGui(useSwing,
1163N/A useShape,
1163N/A useTransl,
1163N/A useNonOpaque,
1163N/A 0.7f);
1163N/A }
1163N/A });
1163N/A }
1163N/A}