Bouncing picture applet


import java.awt.*;
import java.applet.*;

public class SwingPictures extends Applet implements Runnable
{

private static final long serialVersionUID = 1L;
//int width, height;
Thread runner;
private Image Buffer;
private Graphics gBuffer;
int x, x1, x2=100;
Image pic1;
// Image pic2, pic3, pic4, pic5;

public void init()
{
pic1=getImage(getCodeBase(),"Images/Jupiter.jpg");
//pic2=getImage(getCodeBase(),"Images/Earth.jpg");
//pic3=getImage(getCodeBase(),"Images/Saturn.jpg");




//create graphics buffer, the size of the applet
Buffer=createImage(getSize().width,getSize().height);
gBuffer=Buffer.getGraphics();
}
//moveRight is boolean variable used  to keep track of the
//direction.
boolean moveRight=true;

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

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

public void run()
{
while(true)
{
try {runner.sleep(10);}//Thread sleeps for 10 milliseconds.
catch (Exception e) { }

gBuffer.setColor(Color.black);//paint background black
gBuffer.fillRect(0,0,getSize().width,getSize().height);

if(moveRight==true)
{
if(x20)
x2-=2;
else
moveRight=true;
}

gBuffer.drawImage(pic1,x2,50,150,130,this);
// gBuffer.drawImage(pic2,x2-200,100,180,130,this);
// gBuffer.drawImage(pic3,x2-400,250,180,130,this);

repaint();
}
}

//is needed to avoid erasing the background by Java
public void update(Graphics g)
{
paint(g);
}

public void paint(Graphics g)
{
g.drawImage (Buffer,0,0, this);
}
}

/*


Font


*/