1

I'm trying to change the opacity of my frame from 0 to 10 (0.0,0.1,0.2..1.0) so it will create an animation effect.

I tried this code at the end of the constructor but it produce only the last value of the opacity:

for(float k=0;k<=10;k++)
    {
        try {
            Thread.sleep(200);
        } catch (InterruptedException ex) {
        }
        setOpacity(k/10);
    }
Imri Persiado
  • 1,857
  • 8
  • 29
  • 45

2 Answers2

4

Swing is single threaded. Calling Thread.sleep in the EDT prevents UI updates. Use a Swing Timer instead.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
0

When the animation is needed to initialize set some boolean variable to true, and then in the render function check if the boolean variable is true, and if it is, add .1, and when it reaches one set the boolean variable back to false.