I am creating a program that uses JFrame, JPanel, JLabel and all other swing components.
What I want to do is create a 2D animation on a separate JPanel, which is designed for this animation. Therefore, I will override the paintComponent (Graphics g) method.
I have experience creating animations with loops + Threads, but I hear that threads are not safe with swings.
Because of this, is it safe for me to make animations using the Runnable interface? If not, what should I use (e.g. Timer) and please give a small example of how best to use it (or a link to a web page).
EDIT:
Thanks Jeff, I will use a timer to create animations. For future viewers of this question, here is a short program that I encoded in about 5 minutes, sorry dirty code.
I also added some quick comments.
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);
source share