Login    New User    Forgot Password    Help      

Moving Star

Java Tutorials

// moving star

import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;
import java.applet.*;

public class app9 extends Applet implements Runnable
{

Thread t;
int i=10;
int j=20;
public void init()
{
setBackground(Color.red);
setForeground(Color.white);
}

public void start()
{
if (t == null)
{
t = new Thread(this);
t.start();
}
}

public void stop()
{
if (t != null)
{
t.stop();
t = null;
}
}

public void run()
{
while (true)
{
// calling paint again
i++;
if(i>450)
{
i=10;
j=j+3;
}
repaint();
try
{
Thread.sleep(10);
}
catch (InterruptedException e)
{ }
}
}

// run both with hiding the update function and run update function

//public void update(Graphics g)
//{
// g.drawString("*",i,j);

//}

public void paint(Graphics g)
{

g.drawString("*",i,j);

}
}

Output

Output in applet

Java Tutorials