Imri Persiado
Imri Persiado

Reputation: 1889

Changing the opacity from 0.0 0.1 .. to 1.0 to create an animation effect

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);
    }

Upvotes: 1

Views: 82

Answers (2)

Reimeus
Reimeus

Reputation: 159754

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

Upvotes: 4

Kyle Saltzberg
Kyle Saltzberg

Reputation: 64

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.

Upvotes: 0

Related Questions