Advertisement
Guest User

Oracle's "Translucent and Shaped Windows", slightly changed

a guest
Apr 12th, 2016
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.48 KB | None | 0 0
  1. /**
  2.  * ORIGINAL CODE BY ORACLE:
  3.  * <p>
  4.  * https://docs.oracle.com/javase/tutorial/uiswing/examples/misc/GradientTranslucentWindowDemoProject/src/misc/GradientTranslucentWindowDemo.java
  5.  * <p>
  6.  * THIS CODE HAS BEEN SLIGHTLY CHANGED TO TEACH MORE WITH LESS, AND TO BE MORE UP-TO-DATE.
  7.  * <p>
  8.  * <p>
  9.  * <p>
  10.  * <p>
  11.  * <p>
  12.  * <p>
  13.  * <p>
  14.  * <p>
  15.  * <p>
  16.  * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
  17.  * <p>
  18.  * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
  19.  * following conditions are met:
  20.  * <p>
  21.  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the following
  22.  * disclaimer.
  23.  * <p>
  24.  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
  25.  * disclaimer in the documentation and/or other materials provided with the distribution.
  26.  * <p>
  27.  * - Neither the name of Oracle or the names of its contributors may be used to endorse or promote products derived from
  28.  * this software without specific prior written permission.
  29.  * <p>
  30.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  32.  * DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  33.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  34.  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  35.  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  36.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37.  */
  38.  
  39. import com.sun.awt.AWTUtilities;
  40.  
  41. import javax.swing.*;
  42. import java.awt.*;
  43. import java.awt.event.ComponentAdapter;
  44. import java.awt.event.ComponentEvent;
  45. import java.awt.geom.Ellipse2D;
  46.  
  47. import static java.awt.GraphicsDevice.WindowTranslucency;
  48.  
  49.  
  50.  
  51.  
  52. public class TranslucentWindowTest extends JFrame {
  53.  
  54.  
  55.     public TranslucentWindowTest() {
  56.  
  57.         super("GradientTranslucentWindow");
  58.  
  59.         //        setUndecorated(true);
  60.  
  61.         setBackground(new Color(0, 0, 0, 0));
  62.         setSize(new Dimension(300, 200));
  63.         setLocationRelativeTo(null);
  64.         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  65.  
  66.         JPanel panel = new JPanel() {
  67.  
  68.             @Override
  69.             protected void paintComponent(Graphics g) {
  70.  
  71.                 if (g instanceof Graphics2D) {
  72.                     final int R = 240;
  73.                     final int G = 240;
  74.                     final int B = 240;
  75.  
  76.                     Paint p = new GradientPaint(0.0f, 0.0f, new Color(R, G, B, 0), 0.0f, getHeight(), new Color(R, G, B, 255), true);
  77.                     Graphics2D g2d = (Graphics2D) g;
  78.                     g2d.setPaint(p);
  79.                     g2d.fillRect(0, 0, getWidth(), getHeight());
  80.                 }
  81.             }
  82.         };
  83.         setContentPane(panel);
  84.         setLayout(new GridBagLayout());
  85.         add(new JButton("I am a Button"));
  86.  
  87.         addComponentListener(new ComponentAdapter() {
  88.  
  89.             @Override
  90.             public void componentResized(ComponentEvent evt) {
  91.  
  92.                 Shape shape = new Ellipse2D.Float(0, 0, getWidth(), getHeight());
  93.                 AWTUtilities.setWindowShape(TranslucentWindowTest.this, shape);
  94.             }
  95.         });
  96.     }
  97.  
  98.  
  99.     public static void main(String[] args) {
  100.  
  101.         // Determine what the GraphicsDevice can support.
  102.         final GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
  103.         final GraphicsDevice gd = ge.getDefaultScreenDevice();
  104.  
  105.         if (!gd.isWindowTranslucencySupported(WindowTranslucency.PERPIXEL_TRANSLUCENT)) {
  106.             System.err.println("Per-pixel translucency is not supported");
  107.             System.exit(0);
  108.         }
  109.  
  110.         if (!gd.isWindowTranslucencySupported(WindowTranslucency.PERPIXEL_TRANSPARENT)) {
  111.             System.err.println("Shaped windows are not supported");
  112.             System.exit(0);
  113.         }
  114.  
  115.         JFrame.setDefaultLookAndFeelDecorated(true); // EITHER DO THIS - OR UNCOMMENT "setUndecorated(true);" FURTHER UP
  116.  
  117.         SwingUtilities.invokeLater(() -> new TranslucentWindowTest().setVisible(true));
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement