What's the problem in my code while generating a simple animation, where a green ball moves from the top left corner of the window diagonally downwards. But what i am getting is ball appears at the screen after a long time and it just remains stationary.What can i do?
import javax.swing.*;
import java.awt.*;
class Show_starter
{
int x, y;
JFrame window = new JFrame("Graphic_show");
Graphic_panel jp = new Graphic_panel();
public static void main(String[] args) {
Show_starter start = new Show_starter();
start.go();
}
private void go()
{
window.getContentPane().add(BorderLayout.CENTER, jp);
window.setSize(600,800);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class Graphic_panel extends JPanel
{
public void paintComponent(Graphics g)
{
for ( int i = 0; i < 100; ++i){
g.setColor(Color.white);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(Color.green);
g.fillOval(x, y, 40, 40);
x++;
y++;
try{
Thread.sleep(50);
}catch (Exception e){}
jp.repaint();
}
}
}
EDIT: i am calling repaint() also but its not working.