
//Dear Deepak i want to write a code which mover the ball along x-co-ordinates 9 times on for loop execution, but the ball is not moving, please help. Thanks. :)
//MoveBall.java import javax.swing.*;
import java.awt.*;
public class MoveBall
{
public static void main(String[] args)
{
MoveBall d = new MoveBall(); new Thread().start(); }
public MoveBall()
{
JFrame frame = new JFrame("Drawing with Alpha");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new Ball());
frame.setSize(400,400);
frame.setVisible(true);
}
public class Ball extends JComponent implements Runnable
{
int x=80, y=100;
int radius = 20;
public void paint(Graphics g)
{
g.setColor(Color.magenta);
g.fillOval(x-radius, y-radius,2*radius,2*radius);
//g.drawOval(20,20, x, y);
}
public void start()
{
Thread th = new Thread();
th.start();
}
public void run()
{
for(int i=0; i<10; i++)
{
repaint();
try
{
Thread.sleep(200);
x = x+10;
}
catch(Exception e)
{
}
}
}
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.