简体   繁体   English

刷新完全透明的JFrame

[英]Refreshing fully transparent JFrame

I want to make smth like window - I have completely transparent JFrame and JPanel and move window after a mouse, but it doesn't refresh panel - soon window becomes completely red. 我想像窗口一样制作smth - 我有一个完全透明的JFrame和JPanel并在鼠标后移动窗口,但它不刷新面板 - 很快窗口变成完全红色。 Here's my code: 这是我的代码:

public class Main {

    public static HolePanel panel = new HolePanel();
    public static JFrame frame = new JFrame();

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JFrame.setDefaultLookAndFeelDecorated(true);
        frame.setUndecorated(true);
        frame.setBackground(new Color(0, 0, 0, 0));
        frame.setAlwaysOnTop(true);
        frame.setBounds(0, 0, (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth(), (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight());
        frame.setContentPane(panel);

        frame.addMouseMotionListener(new MouseList());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

}



public class HolePanel extends JPanel {

public double centerX = 200;
public double centerY = 200;
public double diameter = 300;

HolePanel(){
    setBackground(new Color(0, 0, 0, 0));
    repaint();
}

public void paintComponent(Graphics badG){
    super.paintComponent(badG);
    Graphics2D g = (Graphics2D)badG;

    g.setColor(Color.RED.darker().darker());

    Area a = new Area (new Rectangle2D.Double(0, 0, (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth(), (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight()));
    a.subtract(new Area(new Ellipse2D.Double(centerX - diameter / 2, centerY - diameter / 2, diameter,diameter)));
    g.fill(a);
}

}


public class MouseList implements MouseMotionListener {

public void mouseDragged(MouseEvent e) {
}

@Override
public void mouseMoved(MouseEvent e) {
    Main.panel.centerX = e.getXOnScreen();
    Main.panel.centerY = e.getYOnScreen();  
    Main.panel.repaint();
}

}

Could you guys help me someway? 你能帮帮我吗?

Check out Backgrounds With Transparency . 查看具有透明度的背景 Maybe you can use the AlphaContainer class. 也许你可以使用AlphaContainer类。

Basically when you want to use a transparent background then you must make your component non-opaque and override the paintComponent() method to do the painting of the background. 基本上,当您想要使用透明背景时,必须使组件不透明,并覆盖paintComponent()方法以绘制背景。 The AlphaContainer does this for you. AlphaContainer为您完成此任务。

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

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