I have created an infinite transparent loading jframe however it flickers when setting Background to transparent with a call to f.setBackground(new Color(0, 0, 0, 0));, if setBackground is commented the JFrame shows the animated icon correctly.
Can anybody fix this?
import java.awt.Color;
import java.awt.EventQueue;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class InfiniteLoad extends JPanel
{
private JPanel loadingPanel() throws MalformedURLException
{
JPanel panel = new JPanel();
BoxLayout layoutMgr = new BoxLayout(panel, BoxLayout.PAGE_AXIS);
panel.setLayout(layoutMgr);
java.net.URL imageURL = new URL("http://oi59.tinypic.com/106a6vo.jpg");
ImageIcon imageIcon = new ImageIcon(imageURL);
JLabel iconLabel = new JLabel();
iconLabel.setIcon(imageIcon);
imageIcon.setImageObserver(iconLabel);
JLabel label = new JLabel("Loading...");
panel.add(iconLabel);
panel.add(label);
panel.setOpaque(false);
return panel;
}
public static void main(String[] args) throws MalformedURLException
{
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
JFrame f = new JFrame("Window");
f.setUndecorated(true);
//AWTUtilities.setWindowOpacity(f, 0.6f);
//f.setOpacity(0.4f);
InfiniteLoad imagePanel = new InfiniteLoad();
JPanel jp = null;
try
{
jp = imagePanel.loadingPanel();
}
catch (MalformedURLException ex)
{
Logger.getLogger(InfiniteLoad.class.getName()).log(Level.SEVERE, null, ex);
}
f.setContentPane(jp);
f.setBackground(new Color(0, 0, 0, 0)); //<-- THIS CAUSES FLICKERING
f.pack();
f.setVisible(true);
}
});
}
}
EDIT:
Ok problem was OpenJDK Java related, it displayed the mentioned problem with
java version "1.7.0_55"
OpenJDK Runtime Environment (IcedTea 2.4.7) (7u55-2.4.7-1ubuntu1~0.13.10.1)
OpenJDK 64-Bit Server VM (build 24.51-b03, mixed mode)
while running with
java version "1.7.0_45"
Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)
shows correctly. If anyone can show a workaround to make it work correctly also under buggy implementations, you're welcome.