My JProgressBar is worked perfectly up until i set my JFrame GlassPane visible. It then randomly decides to shoot across the screen and duplicate.
Here is an animated GIF displaying the bug. This happens no matter what my code is - even with an extremely basic GUI
http://i.gyazo.com/bd70df610a3cad6bfff258b80f21c78a.gif
This ONLY happens when i do this.getGlassPane().setVisible(true);
Here is the code:
public void setProgress(final int percent) {
pb.setValue(percent);
if (percent == 100) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
FadingMirror glassPane = new FadingMirror();
instance.setGlassPane(glassPane);
((FadingMirror)instance.getGlassPane()).startAnimation();
((FadingMirror)instance.getGlassPane()).setVisible(true);
}
});
}
}
@Override
public void addText(String text) {
System.out.println(text);
}
public static class FadingMirror extends JPanel implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 7341931028635559650L;
private float opacity = 0f;
private Timer fadeTimer;
public void startAnimation() {
fadeTimer = new javax.swing.Timer(75, this);
fadeTimer.setInitialDelay(0);
fadeTimer.start();
}
public void actionPerformed(ActionEvent e) {
opacity += .03;
if(opacity > 1) {
opacity = 1;
fadeTimer.stop();
fadeTimer = null;
}
repaint();
}
public void paintComponent(Graphics g) {
//super.paintComponent(g); //Cannot do this before setting composite without destroying the animation
((Graphics2D) g).setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity));
g.setColor(getBackground());
g.fillRect(0,0,getWidth(),getHeight());
}
}
}
Thanks for reading