Moving Ball using thread and applet in Java


Moving Ball using thread and applet in Java

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code = “Ball” width=400 height=500>
</applet>*/
public class Ball extends Applet implements Runnable
{
Thread mythread=null;
int pos=0;
public void start()
{
mythread=new Thread(this);
mythread.start();
}
public void run()
{
while(true)
{
for(pos=0;pos<getSize().width;pos=pos+5)
{
repaint();
try
{
mythread.sleep(10);
}
catch(InterruptedException e)
{
}
} } }
public void stop()
{
mythread.stop();
mythread=null;
}
public void paint(Graphics g)
{
g.setColor(Color.red);
g.fillOval(pos,50,30,30);
g.setColor(Color.yellow);
g.fillOval(pos+6,58,5,5);
g.fillOval(pos+20,58,5,5);
g.drawLine(pos+15,58,pos+15,68);
g.drawLine(pos+12,68,pos+15,68);
g.drawArc(pos,45,30,30,-50,-70);
} }

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s