How to make animation with Swing?

I'm making a japplet and stuck with an animation problem.

Here is my code:

        this.sprite.setBounds(0,0,20,17);
        this.sprite.setIcon(this.rangerDown);
        for(int i = 0; i< 16;i++)
        {
            this.sprite.repaint();
            this.sprite.setLocation(this.sprite.getX(), this.sprite.getY()+10);
            try{
                Thread.currentThread().sleep(100);
            }catch(InterruptedException e){
            }
        }       

There is no animation: nothing happens during the cycle, the repaint () method seems to work only after the sprite has stopped moving.

I would like to use only Swing for this, any ideas on how to proceed?

Thanks for reading.

+3
source share
2 answers

You should use javax.swing.TimerThread to perform the animation, not the spin. Here is a good link for you: http://java.sun.com/docs/books/tutorial/uiswing/misc/timer.html

Filthy Rich - , . , 12: , MovingButton, .

+4

, , .

, , . Swing ; , . - , , Swing.

, - SwingUtilities.invokeLater(Runnable r), r run(). Google "invokeLater" Swing, , .

+1

Source: https://habr.com/ru/post/1722034/


All Articles