Hi, I've started to learn about threads in java. I'm trying to make this simple animation of a rectangle. Where am I going wrong? I was under the impression that if a class implements Runnable, when it is executed, it looks for the start method, which in turn starts the thread and run() is executed. Should I be overriding the start method? I saw that the thread.stop() is now deprecated so I'm skeptical about anything I'm doing now because I must have learnt from an old version of java.
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.util.ArrayList;
import javax.swing.JPanel;
public class AnimationWithThreads extends JPanel implements Runnable {
Thread t;
private int x,y,dx=2,dy=2;
public AnimationWithThreads(){
x=50;
y=50;
}
public void start(){
if(t==null){
t = new Thread(this);
t.start();
}
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.fillRect(x, y, 40, 40);
}
public void run() {
x+=dx;
y+=dy;
repaint();
}
}