Java:Безопасная анимация с помощью Swing

Я создаю программу, которая использует JFrame, JPanel, JLabel и все другие виды компонентов Swing.

Я хочу создать 2D-анимацию на отдельной панели JPanel, предназначенной для этой анимации. Поэтому я заменю метод paintComponent (Graphics g).

У меня есть опыт создания анимации с помощью циклов for + Threads, но я слышал, что потоки небезопасны с Swing.

В связи с этим безопасно ли для меня создавать анимацию с использованием интерфейса Runnable? Если нет, что мне использовать (например, таймер), и, пожалуйста, приведите небольшой пример того, как лучше всего его использовать (или ссылку на веб-страницу).

РЕДАКТИРОВАТЬ:

Благодаря Джеффу, я буду использовать таймер для создать анимацию. Для будущих читателей этого вопроса, вот небольшая программа, которую я написал примерно за 5 минут, извините за грязный код.

Я также добавил несколько быстрых комментариев.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


class JFrameAnimation extends JFrame implements ActionListener
{

    JPanel panel;
    Timer timer;
    int x, y;

    public JFrameAnimation ()
    {
        super ();
        setDefaultCloseOperation (EXIT_ON_CLOSE);
        timer = new Timer (15, this); //@ First param is the delay (in milliseconds) therefore this animation is updated every 15 ms. The shorter the delay, the faster the animation.
        //This class iplements ActionListener, and that is where the animation variables are updated. Timer passes an ActionEvent after each 15 ms.

    }


    public void run ()
    {

        panel = new JPanel ()
        {
            public void paintComponent (Graphics g)  //The JPanel paint method we are overriding.
            {
                g.setColor (Color.white);
                g.fillRect (0, 0, 500, 500); //Setting panel background (white in this case);
                //g.fillRect (-1 + x, -1 + y, 50, 50);  //This is to erase the black remains of the animation. (not used because the background is always redrawn.
                g.setColor (Color.black);
                g.fillRect (0 + x, 0 + y, 50, 50); //This is the animation.

            }

        }
        ;
        panel.setPreferredSize (new Dimension (500, 500)); //Setting the panel size

        getContentPane ().add (panel); //Adding panel to frame.
        pack ();
        setVisible (true);
        timer.start (); //This starts the animation.
    }


    public void actionPerformed (ActionEvent e)
    {
        x++;
        y++;
        if (x == 250)
            timer.stop (); //This stops the animation once it reaches a certain distance.
        panel.repaint ();  //This is what paints the animation again (IMPORTANT: won't work without this).
        panel.revalidate (); //This isn't necessary, I like having it just in case.

    }


    public static void main (String[] args)
    {
        new JFrameAnimation ().run (); //Start our new application.
    }
}
5
задан Jimmy Huch 8 June 2011 в 03:30
поделиться