Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * ORIGINAL CODE BY ORACLE:
- * <p>
- * https://docs.oracle.com/javase/tutorial/uiswing/examples/misc/GradientTranslucentWindowDemoProject/src/misc/GradientTranslucentWindowDemo.java
- * <p>
- * THIS CODE HAS BEEN SLIGHTLY CHANGED TO TEACH MORE WITH LESS, AND TO BE MORE UP-TO-DATE.
- * <p>
- * <p>
- * <p>
- * <p>
- * <p>
- * <p>
- * <p>
- * <p>
- * <p>
- * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
- * <p>
- * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
- * following conditions are met:
- * <p>
- * - Redistributions of source code must retain the above copyright notice, this list of conditions and the following
- * disclaimer.
- * <p>
- * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
- * disclaimer in the documentation and/or other materials provided with the distribution.
- * <p>
- * - Neither the name of Oracle or the names of its contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- * <p>
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
- * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- import com.sun.awt.AWTUtilities;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ComponentAdapter;
- import java.awt.event.ComponentEvent;
- import java.awt.geom.Ellipse2D;
- import static java.awt.GraphicsDevice.WindowTranslucency;
- public class TranslucentWindowTest extends JFrame {
- public TranslucentWindowTest() {
- super("GradientTranslucentWindow");
- // setUndecorated(true);
- setBackground(new Color(0, 0, 0, 0));
- setSize(new Dimension(300, 200));
- setLocationRelativeTo(null);
- setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- JPanel panel = new JPanel() {
- @Override
- protected void paintComponent(Graphics g) {
- if (g instanceof Graphics2D) {
- final int R = 240;
- final int G = 240;
- final int B = 240;
- Paint p = new GradientPaint(0.0f, 0.0f, new Color(R, G, B, 0), 0.0f, getHeight(), new Color(R, G, B, 255), true);
- Graphics2D g2d = (Graphics2D) g;
- g2d.setPaint(p);
- g2d.fillRect(0, 0, getWidth(), getHeight());
- }
- }
- };
- setContentPane(panel);
- setLayout(new GridBagLayout());
- add(new JButton("I am a Button"));
- addComponentListener(new ComponentAdapter() {
- @Override
- public void componentResized(ComponentEvent evt) {
- Shape shape = new Ellipse2D.Float(0, 0, getWidth(), getHeight());
- AWTUtilities.setWindowShape(TranslucentWindowTest.this, shape);
- }
- });
- }
- public static void main(String[] args) {
- // Determine what the GraphicsDevice can support.
- final GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
- final GraphicsDevice gd = ge.getDefaultScreenDevice();
- if (!gd.isWindowTranslucencySupported(WindowTranslucency.PERPIXEL_TRANSLUCENT)) {
- System.err.println("Per-pixel translucency is not supported");
- System.exit(0);
- }
- if (!gd.isWindowTranslucencySupported(WindowTranslucency.PERPIXEL_TRANSPARENT)) {
- System.err.println("Shaped windows are not supported");
- System.exit(0);
- }
- JFrame.setDefaultLookAndFeelDecorated(true); // EITHER DO THIS - OR UNCOMMENT "setUndecorated(true);" FURTHER UP
- SwingUtilities.invokeLater(() -> new TranslucentWindowTest().setVisible(true));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement