2

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.

dendini
  • 3,842
  • 9
  • 37
  • 74
  • (my curiosity) and is there the same issue by using [undecorated JButton](http://stackoverflow.com/a/7944388/714968) – mKorbel Jul 17 '14 at 07:56
  • sorry I don't understand.. which button? I have no buttons in my code – dendini Jul 17 '14 at 07:58
  • I just ran it 3 times in 1.8.0_05(?) on Windows 7 and did not notice any flickering, just an animated image. – Andrew Thompson Jul 17 '14 at 08:26
  • Hmm interesting.. I ran it on Ubuntu, java version "1.7.0_55" and it flickers, now I'll try on Windows. Seems to be related to the fact that the background is used to simulate an animation when instead it is used to hide the old image. – dendini Jul 17 '14 at 08:38
  • Seems to work just fine for me, what OS are you using? – MadProgrammer Jul 17 '14 at 08:40
  • @Andrew Thompson interesting I'm didn't see anything only description "Loading..." runs from Win8/Java7 – mKorbel Jul 17 '14 at 08:40
  • Try adding the `jp` to the frame instead of setting it as the content pane... – MadProgrammer Jul 17 '14 at 08:42

1 Answers1

1

sorry I don't understand.. which button? I have no buttons in my code

  • I'm tested animated Images always in undecorated JButton (maybe one of my bad habits)

  • can't found (not deepest research) good animated image saved as JPEG that works (all are transparent, nothing is visible from code attn in my post, then nothing flickering)

  • there couldn't be/isn't an issue with animated GIF

    enter image description here

code

import com.sun.awt.AWTUtilities;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Insets;
import java.net.MalformedURLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
//http://stackoverflow.com/q/24797774/714968
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);
        ImageIcon imageIcon = new ImageIcon((getClass().getResource("/Images/smiley_018.gif")));
        JButton button = new JButton();
        button.setBorderPainted(false);
        button.setBorder(null);
        button.setFocusable(false);
        button.setMargin(new Insets(0, 0, 0, 0));
        button.setContentAreaFilled(false);
        button.setIcon(imageIcon);
        JLabel myLabel = new JLabel(imageIcon);
        JLabel label = new JLabel("Loading...", JLabel.CENTER);
        panel.add(button);
        panel.add(label);
        panel.add(myLabel);
        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.1f);
                //f.setOpacity(0.1f);
                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);
            }
        });
    }
}
Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319