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. 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.
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. The AlphaContainer
does this for you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.