我想像窗口一样制作smth - 我有一个完全透明的JFrame和JPanel并在鼠标后移动窗口,但它没有刷新面板 - 很快窗口变成完全红色。这是我的代码:
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();
}
}
你们可以帮我一点吗?
答案 0 :(得分:2)
结帐Backgrounds With Transparency。也许你可以使用AlphaContainer
类。
基本上,当您想要使用透明背景时,必须使组件不透明,并覆盖paintComponent()方法以绘制背景。 AlphaContainer
为您完成此操作。