You are on page 1of 8

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

*; public class picmove extends Applet implements Runnable{ public static int XRES =780; //X dimension of the applet public static int YRES =400; //Y dimension of the applet int x=0; //current x and y positions int y=0; //of the picture int inc=3; //How far the picture moves per frame int xcent=5; int ycent=100; Image offscreen; //Double buffer image Graphics dbuffer; //Unused Image puzshort,puzlong,puzbot; //GIF of top short puzzle piece public Rectangle puzS,puzL,puzB; //piece's width and height Thread animator = null; //Animation thread boolean stopit = false; //Checks if the animation is moving public Rectangle AppBorder; //Applet dimensions

public void init() { //Called first upon running applet AppBorder = bounds(); //Check applet dimensions XRES=AppBorder.width; //Set the x and y dim. of the applet YRES=AppBorder.height; offscreen = createImage (XRES,YRES); //make a offscreen copy of applet loadpics(); } public void paint (Graphics gr) { gr.setColor(new Color(40,80,200)); gr.fillRect(0,0,XRES,YRES); gr.drawImage(puzshort,puzS.x+xcent,puzS.y,null); gr.drawImage(puzlong,puzL.x+xcent,puzL.y,null); gr.drawImage(puzbot,puzB.x+xcent,puzB.y,null); }

public void run(){

int dif=puzL.width-puzS.width; while (true){ drawall(); for (int i=0; i<puzL.width; i+=inc) { if (i<dif/2) { puzL.y-=inc; } else if (i>puzL.width-(dif/2)){ puzL.y+=inc; } else { puzL.x-=inc; } puzS.x+=inc; drawit(); } puzS.x=puzL.width; puzS.y=ycent; puzL.x=0; puzL.y=ycent;

drawit(); try {Thread.sleep(4000);} catch(InterruptedException e){} drawall(); for (int i=0; i<puzL.width; i+=inc) { if (i<dif/2) { puzL.y-=inc; } else if (i>puzL.width-(dif/2)){ puzL.y+=inc; } else { puzL.x+=inc; } puzS.x-=inc; drawit(); } puzS.x=0; puzS.y=ycent; puzL.x=puzS.width; puzL.y=ycent; drawit();

try {Thread.sleep(4000);} catch(InterruptedException e){} } } public void drawit(){ Graphics gr=offscreen.getGraphics(); //get the buffered image gr.clipRect(0,puzL.y-incinc,puzB.width+xcent,puzS.height+ycent-puzL.y+inc+inc); paint(gr); //paint the buffered image gr=this.getGraphics(); //get the real image gr.clipRect(0,puzL.y-incinc,puzB.width+xcent,puzS.height+ycent-puzL.y+inc+inc); gr.drawImage(offscreen,0,0,this); //automatically paints the real image } public void drawall(){ Graphics gr=offscreen.getGraphics(); //get the buffered image gr.clipRect(0,0,XRES,YRES);

paint(gr); //paint the buffered image gr=this.getGraphics(); //get the real image gr.clipRect(0,0,XRES,YRES); gr.drawImage(offscreen,0,0,this); //automatically paints the real image } public void start(){ animator=new Thread(this); animator.start(); } public void stop(){ if (animator!=null) animator.stop(); animator=null; } public void loadpics(){ puzshort = getImage(getCodeBase(),"p_short.jpg"); //load jpg puzS=new Rectangle(); while(puzshort.getWidth(null)==-1){ } while(puzshort.getHeight(null)==-1){

} puzS.height=puzshort.getHeight(null); puzS.width=puzshort.getWidth(null); puzS.x=0; puzS.y=ycent;

puzlong = getImage(getCodeBase(),"p_long.jpg"); //load jpg puzL=new Rectangle(); while(puzlong.getWidth(null)==-1){ } while(puzlong.getHeight(null)==-1){ } puzL.height=puzlong.getHeight(null); puzL.width=puzlong.getWidth(null); puzL.x=puzS.width; puzL.y=ycent;

puzbot = getImage(getCodeBase(),"p_bottom.jpg"); //load jpg puzB=new Rectangle();

while(puzbot.getWidth(null)==-1){ } while(puzbot.getHeight(null)==-1){ } puzB.height=puzbot.getHeight(null); puzB.width=puzbot.getWidth(null); puzB.x=0; puzB.y=ycent+puzS.height; } }