import java.awt.*; import java.applet.*; public class moving_gif extends Applet implements Runnable { int x, y, xx, yy; int d_x=1, d_y=1, d_xx=-1, d_yy=-1; int speed=4, speedd=2; Image image1,image2,image3,image4,image5,image6,image7,image8; private Thread flow; boolean done, pause; public void init() { image1 = getImage(getDocumentBase(), "image/s1.gif"); image2 = getImage(getDocumentBase(), "image/s2.gif"); image3 = getImage(getDocumentBase(), "image/s3.gif"); image4 = getImage(getDocumentBase(), "image/s4.gif"); image5 = getImage(getDocumentBase(), "image/s5.gif"); image6 = getImage(getDocumentBase(), "image/s6.gif"); image7 = getImage(getDocumentBase(), "image/s7.gif"); image8 = getImage(getDocumentBase(), "image/s8.gif"); setBackground(Color.black); } public void start() { done = false; pause = false; x = 0; y = 0; xx = 500; yy = 200; flow = new Thread(this); flow.start(); } public void run() { try { while (!done) { Thread.sleep(200); if (!pause) repaint(); } } catch (Exception e){}; } public void paint(Graphics g) { Dimension d = size(); if(d_x==1 && d_y==1) g.drawImage(image1, x, y, 50, 50, this); else if(d_x==-1 && d_y==1) g.drawImage(image2, x, y, 50, 50, this); else if(d_x==1 && d_y==-1) g.drawImage(image3, x, y, 50, 50, this); else if(d_x==-1 && d_y==-1) g.drawImage(image4, x, y, 50, 50, this); if(d_xx==1 && d_yy==1) g.drawImage(image5, xx, yy, 40, 40, this); else if(d_xx==-1 && d_yy==1) g.drawImage(image6, xx, yy, 40, 40, this); else if(d_xx==1 && d_yy==-1) g.drawImage(image7, xx, yy, 40, 40, this); else if(d_xx==-1 && d_yy==-1) g.drawImage(image8, xx, yy, 40, 40, this); x += d_x * (3+speed); y += d_y * (3+speed); xx += d_xx * (3+speedd); yy += d_yy * (3+speedd); if( (xx>x-40) && (xxy-40) && (yyx+35){ if(d_xx==-1) d_xx=1; if(d_x==1) d_x=-1; } else if(yy>y+35){ if(d_yy==-1) d_yy=1; if(d_y==1) d_y=-1; } } if(x<0) d_x = 1; if(y<0) d_y = 1; if(x>d.width-50) d_x = -1; if(y>d.height-50) d_y = -1; if(xx<0) d_xx = 1; if(yy<0) d_yy = 1; if(xx>d.width-40) d_xx = -1; if(yy>d.height-40) d_yy = -1; } public boolean mouseDown(java.awt.Event evt, int x, int y) { pause = !pause; if(pause == true){ speed = (speed+2)%7; speedd = (speedd+1)%6; } return(true); } public void stop() { done = true; flow.stop(); } }