简体   繁体   English

Java swing/awt 剪裁透明背景叠加

[英]Java swing/awt clipping transparent background overlay

I'm trying to do a transparent background with a clipped region that doesn't occupy the background.我正在尝试使用不占用背景的剪切区域来制作透明背景。 Somewhat like the windows snipping tool, when you drag a rectangle, everything around that rectangle is covered by a white transparent overlay except the region you drag the rectangel.有点像 windows 截图工具,当你拖动一个矩形时,除了你拖动矩形的区域外,该矩形周围的所有东西都被白色透明覆盖层覆盖。

Now I wonder if you can do this with JFrames and the Graphics.setClip-Method.现在我想知道您是否可以使用 JFrames 和 Graphics.setClip-Method 来做到这一点。

Can something like this be done in Java?可以在 Java 中完成这样的事情吗?

Maybe something like this is what you are looking for:也许你正在寻找这样的东西:

在此处输入图像描述

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import static java.awt.GraphicsDevice.WindowTranslucency.*;

public class FrameTranslucent2 extends JFrame
{
    public FrameTranslucent2()
    {
        super("Frame Translucent");

        setBackground(new Color(0,0,0,0));
        setSize(new Dimension(600,600));
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel(new BorderLayout())
        {
            @Override
            protected void paintComponent(Graphics g)
            {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D)g;

                //  Simple approach to make a rectangular area fully transparent

                g2d.clearRect(100, 100, 200, 200);

                //  Make a Shape area fully transparent
/*
                Shape inner = new Ellipse2D.Double(100, 100, 200, 200);
                g2d.setClip( inner );
                g2d.clearRect(0, 0, getWidth(), getHeight());
*/
            }
        };

        panel.setBackground( new Color(128, 128, 128, 64) );
        setContentPane(panel);
    }

    public static void main(String[] args)
    {
        // Determine what the GraphicsDevice can support.

        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gd = ge.getDefaultScreenDevice();
        boolean isPerPixelTranslucencySupported = gd.isWindowTranslucencySupported(PERPIXEL_TRANSLUCENT);

        //If translucent windows aren't supported, exit.

        if (!isPerPixelTranslucencySupported)
        {
            System.out.println("Per-pixel translucency is not supported");
            System.exit(0);
        }

        JFrame.setDefaultLookAndFeelDecorated(true);

        // Create the GUI on the event-dispatching thread

        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run() {
                FrameTranslucent2 gtw = new FrameTranslucent2();

                // Display the window.
                gtw.setVisible(true);
            }
        });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM