My approach was to use 'reprint()' inside a loop was successful to move an image that means updating its position without overlapping but now I want to see the image moving and I used 'thread.sleep()' to give time gaps between the repaint()s but it doesn't seem to work
import java.awt.*;
import javax.swing.*;
public class jp extends JPanel implements ActionListener{
Timer t;
JPanel jl=new JPanel();
int x,y;
jp(){
x=10;
//y=10;
t=new Timer(5,this);
t.start();
}
public void actionPerformed(ActionEvent e){
x++;
y++;
if(x>500){
x=0;
y=0;
}
repaint();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
this.setBackground(Color.black);
g.setColor(Color.blue);
g.fillRect(x,20,50,50);
}
}
public class Jpanel extends JFrame{
public static void main(String[] args) {
jp p=new jp();
JFrame j=new JFrame("TEST_CASE-1");
j.add(p);
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j.setSize(700,500);
j.setVisible(true);
}
}