I am still a student. I am trying to learn how to draw a ball and move by myself.
Here is the code :
import javax.swing.*;
import java.awt.*;
public class Ball extends JFrame
{
int x = 50;
int y = 50;
int rad = 30;
Ball(){
setSize(500,500);
setTitle("Ball");
setVisible(true);
}
void move()
{
if (x < getWidth() - rad){
x = x + 1 ;
}
try
{
Thread.sleep(100);
}
catch( Exception e)
{
}
}
public void paint( Graphics g)
{
super.paint(g);
g.fillOval(x,y,rad,rad);
}
public static void main(String args[])
{
Ball b = new Ball();
while(true){
b.move();
b.repaint();
}
}
}
I would say this code work 60% of it because
when i run the program the ball is moving to the right, but it keep flashing for some reason and i dont know why.
it is my computer problem , or the code or some kind of bug?
i am using eclipse luna