I have a weird problem. Trying to create a transparent window with java swing works on Java 1.7, but fails on Java 1.8. Unfortunately, I can't figure out how to solve it there. In fact Graphics.clearRect() works for 1.7, whereas it doesn't for 1.8. I've tried to implement https://tips4java.wordpress.com/2009/05/31/backgrounds-with-transparency/, still no success. Here's the code for recreation:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Testpanel
{
private JLabel Testlabel;
private JFrame Hintergrundfenster;
private Bewegungspanel meinBewegungspanel;
public class Bewegungspanel extends JPanel
{
@Override
protected void paintComponent(Graphics Grafik)
{
Grafik.clearRect(0,0,getWidth(), getHeight());
}
public Bewegungspanel()
{
addMouseListener(new MouseAdapter()
{
public void mouseEntered(MouseEvent Ereignis)
{
Testlabel.setText("Hi");
}
public void mouseExited(MouseEvent Ereignis)
{
Testlabel.setText("Bye");
}
});
}
}
public static void main(String args[])
{
Testpanel meinTestpanel = new Testpanel();
}
public Testpanel()
{
Testlabel = new JLabel("Hello")
/*{
@Override
protected void paintComponent(Graphics Grafik)
{
Grafik.clearRect(0,0,0,0);
}
}*/;
Hintergrundfenster = new JFrame();
Hintergrundfenster.setUndecorated(true);
Hintergrundfenster.setSize(100,100);
meinBewegungspanel = new Bewegungspanel();
meinBewegungspanel.setLayout(new GridLayout(2,2));
meinBewegungspanel.add(Testlabel);
meinBewegungspanel.setBackground(new Color(0,0,0,140));
Hintergrundfenster.setBackground(new Color(0,0,0,0));
Hintergrundfenster.add(meinBewegungspanel);
Hintergrundfenster.setVisible(true);
}
}
When hovering the mouse over the label, its text changes. The new data is drawn over the old one only on 1.8, resulting in ugly artefacts:
This is the version 1.8 result:
With java 1.7:
I'd appreciate any ideas to solve the problem. Thanks in advance.