/* "Floating_Nun.java" */ /* 1996 J.H. Rosen */ /* java applet, "Floating_Nun" (nun floats around screen, bouncing off window edges) */ import java.applet.Applet; import java.awt.*; public class Floating_Nun extends java.applet.Applet implements Runnable { /* constants */ /* window width X height */ final int Window_Width = 500; final int Window_Height = 250; /* Floating Nun, width X height */ final int Nun_Width = 68; final int Nun_Height = 176; /* Floating Nun, horiz/vert direction */ final int Nun_Move_Up = 0; final int Nun_Move_Down = 1; final int Nun_Move_Left = 0; final int Nun_Move_Right = 1; /* variables */ /* font */ Font txt_Font; /* Floating Nun image */ Image nun_Images[]; /* Floating Nun, MediaTracker */ MediaTracker nun_MediaTracker; /* Floating Nun, offscreen graphics context (composite buffer) */ Image offNun_Image; Graphics offNun_Graphics; /* Floating Nun, horiz/vert coords */ int hrz_nun_coord, vrt_nun_coord; /* Floating Nun, horiz/vert direction flags */ int hrz_nun_direction, vrt_nun_direction; /* Floating Nun, frame counter */ int nun_FrameCounter; /* Floating Nun, animation thread */ Thread animation_Thread; /* methods invoked by Java */ /* "init()" * initialize the applet */ public void init() { /* loop counter */ int lpCnt; /* Window_Width X Window_Height canvas */ resize(Window_Width,Window_Height); /* text font */ txt_Font = new Font("Helvetica", Font.PLAIN, 9); /* create offscreen graphics context, Floating Nun composite buffer */ /* rect = 0,0,Window_Width,Window_Height */ offNun_Image = createImage(Window_Width,Window_Height); offNun_Graphics = offNun_Image.getGraphics(); /* retrieve Floating Nun .gif images */ /* nun images (4 images) */ nun_Images = new Image[4]; for (lpCnt = 0; lpCnt <=3; lpCnt++) nun_Images[lpCnt] = getImage(getCodeBase(),("images/nun_" + (lpCnt + 1) + ".gif")); /* init Floating Nun MediaTracker */ nun_MediaTracker = new MediaTracker(this); /* load images into MediaTracker */ for (lpCnt = 0; lpCnt <=3; lpCnt++) nun_MediaTracker.addImage(nun_Images[lpCnt],lpCnt); /* init (random position within window) Floating Nun, horiz/vert coords */ hrz_nun_coord = (int) (Math.random() * (Window_Width - Nun_Width)); vrt_nun_coord = (int) (Math.random() * (Window_Height - Nun_Height)); /* init Floating Nun, horiz/vert (random up-down/right-left) direction flags */ hrz_nun_direction = (int) (Math.random() * 2); vrt_nun_direction = (int) (Math.random() * 2); /* init Floating Nun frame counter */ nun_FrameCounter = 0; } /* end method "init" */ /* "start()" * called when the applet becomes visible on screen */ public void start() { /* start scrolling logo animator thread */ animation_Thread = new Thread(this); animation_Thread.start(); } /* end method "start" */ /* "run()" * called (repeatedly) by the Thread created in "start()" */ public void run() { /* update scrolling logo animator thread */ while (Thread.currentThread() == animation_Thread) { /* repaint (update) the screen */ repaint(); /* begin: thread delay (125 milliseconds) */ try { Thread.sleep(125); } catch (InterruptedException e) { break; } /* end: thread delay */ } /* end "while (Thread.currentThread() == animation_Thread)" */ } /* end method "run" */ /* "stop()" * called when the applet is no longer visible on screen */ public void stop() { /* stop scrolling logo animator thread */ if (animation_Thread != null) animation_Thread.stop(); animation_Thread = null; } /* end method "stop" */ /* "destroy()" * called just before the browser exits */ public void destroy() { /* dispose offscreen Graphics Object, Floating Nun composite buffer */ offNun_Graphics.dispose(); offNun_Graphics = null; } /* end method "destroy" */ /* "update()" * update the screen */ public void update(Graphics g) { /* (intercepting "update" avoids background erase) */ /* call "paint" */ paint(g); } /* end method "update" */ /* "paint()" * paint the screen */ public void paint(Graphics g) { /* fill buffer with white paint */ offNun_Graphics.setColor(Color.white); offNun_Graphics.fillRect(0,0,Window_Width,Window_Height); /* frame entire buffer with black color */ offNun_Graphics.setColor(Color.black); offNun_Graphics.drawRect(0,0,(Window_Width - 1),(Window_Height - 1)); /* wait for images to laod before drawing */ if (nun_MediaTracker.checkAll(true) == true) { /* draw nun (in buffer) at specified coords */ offNun_Graphics.drawImage( nun_Images[nun_FrameCounter], hrz_nun_coord, vrt_nun_coord,this ); /* change horiz direction (?) */ if ((hrz_nun_coord + Nun_Width) >= (Window_Width - 1)) hrz_nun_direction = Nun_Move_Left; else if (hrz_nun_coord <= 1) hrz_nun_direction = Nun_Move_Right; /* change vert direction (?) */ if ((vrt_nun_coord + Nun_Height) >= (Window_Height - 1)) vrt_nun_direction = Nun_Move_Up; else if (vrt_nun_coord <= 1) vrt_nun_direction = Nun_Move_Down; /* adjust Floating Nun horiz/vert coords */ if (hrz_nun_direction == Nun_Move_Right) hrz_nun_coord = hrz_nun_coord + 2; else hrz_nun_coord = hrz_nun_coord - 2; if (vrt_nun_direction == Nun_Move_Down) vrt_nun_coord = vrt_nun_coord + 2; else vrt_nun_coord = vrt_nun_coord - 2; /* inc/reset Floating Nun frame counter */ if (nun_FrameCounter < 3) nun_FrameCounter++; else nun_FrameCounter = 0; } // end "if (nun_MediaTracker.checkAll(true) == true)" /* begin: write text message, "Loading Images..." */ else { /* set text string font */ offNun_Graphics.setFont(txt_Font); /* set text string color */ offNun_Graphics.setColor(Color.black); /* write/draw text message */ offNun_Graphics.drawString("Loading Images...",25,25); } /* end: write text message, "Loading Images..." */ /* blast buffer to screen */ g.drawImage(offNun_Image,0,0,this); } /* end method "paint" */ } /* end class "Floating_Nun" */ /* end file "Floating_Nun.java" */