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(x2 |