/* "RevAl_AnimPortrait.java" */ /* 1997 Joe Rosen */ /* java applet, "Rev. Al Sharpton" (Animated Portrait) */ import java.applet.Applet; import java.awt.*; public class RevAl_AnimPortrait extends java.applet.Applet implements Runnable { /* program constants */ /* main portrait width and height */ final int MAIN_WDTH = 488; final int MAIN_HGHT = 623; /* animated portrait top/left */ final int ANIM_TOP = 5; final int ANIM_LEFT = 5; /* animated portrait width and height */ final int ANIM_WDTH = 132; final int ANIM_HGHT = 185; /* variables */ /* font */ Font txt_Font; /* images */ /* main portrait gif */ Image main_Image; /* animated portrait gifs */ Image anim_Image[]; /* animated portrait flip-flop (alternate between 2 images) */ int anim_Image_FlipFlop; /* Rev. Al, MediaTracker */ MediaTracker revAl_MediaTracker; /* Rev. Al, offscreen graphics context (composite buffer) */ Image offRevAl_Image; Graphics offRevAl_Graphics; /* Rev. Al, animation thread */ Thread animation_Thread = null; /* methods invoked by Java */ /* "init()" * initialize the applet */ public void init() { /* resize canvas */ resize(MAIN_WDTH + 1,MAIN_HGHT + 1); /* text font */ txt_Font = new Font("Helvetica", Font.PLAIN, 9); /* create offscreen graphics context, Rev. Al composite buffer */ offRevAl_Image = createImage(MAIN_WDTH,MAIN_HGHT); offRevAl_Graphics = offRevAl_Image.getGraphics(); /* retrieve .gif images */ /* main portrait (488W X 623H) */ main_Image = getImage(getCodeBase(),"images/main_portrait.gif"); /* anim portrait (132W X 185H) */ anim_Image = new Image[2]; anim_Image[0] = getImage(getCodeBase(),"images/anim_portrait_1.gif"); anim_Image[1] = getImage(getCodeBase(),"images/anim_portrait_2.gif"); /* init Rev. Al MediaTracker */ revAl_MediaTracker = new MediaTracker(this); /* load images into MediaTracker */ revAl_MediaTracker.addImage(main_Image,0); revAl_MediaTracker.addImage(anim_Image[0],1); revAl_MediaTracker.addImage(anim_Image[1],2); /* init animated portrait flip-flop (alternate between 2 images) */ anim_Image_FlipFlop = 0; } /* end method "init" */ /* "start()" * called when the applet becomes visible on screen */ public void start() { /* start/resume animator thread */ if (animation_Thread == null) { animation_Thread = new Thread(this); animation_Thread.start(); } else animation_Thread.resume(); } /* end method "start" */ /* "run()" * called (repeatedly) by the Thread created in "start()" */ public void run() { /* update animator thread */ while (Thread.currentThread() == animation_Thread) { /* animated portrait flip-flop (alternate between 2 images */ if (anim_Image_FlipFlop == 1) anim_Image_FlipFlop = 0; else anim_Image_FlipFlop = 1; /* 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() { /* suspend animator thread */ if (animation_Thread != null) animation_Thread.suspend(); } /* end method "stop" */ /* "destroy()" * called when the browsert has closed down or the applet is uncached */ public void destroy() { /* dispose animator thread */ if (animation_Thread != null) animation_Thread = null; /* dispose offscreen Graphics Object, Rev. Al composite buffer */ if (offRevAl_Graphics != null) { offRevAl_Graphics.dispose(); offRevAl_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) { /* wait for images to laod before drawing */ if (revAl_MediaTracker.checkAll(true) == true) { /* draw main portrait (in buffer) */ offRevAl_Graphics.drawImage(main_Image,0,0,this); /* draw animated portrait (flip-flop/alternate between 2 images) */ offRevAl_Graphics.drawImage(anim_Image[anim_Image_FlipFlop],ANIM_LEFT,ANIM_TOP,this); /* frame animated portrait */ offRevAl_Graphics.setColor(Color.black); offRevAl_Graphics.drawRect(ANIM_LEFT - 1,ANIM_TOP - 1,ANIM_WDTH,ANIM_HGHT); } // end "if (revAl_MediaTracker.checkAll(true) == true)" /* begin: write text message, "Loading Images..." */ else { /* fill buffer with white paint */ offRevAl_Graphics.setColor(Color.white); offRevAl_Graphics.fillRect(0,0,MAIN_WDTH,MAIN_HGHT); /* set text string font */ offRevAl_Graphics.setFont(txt_Font); /* set text string color */ offRevAl_Graphics.setColor(Color.black); /* write/draw text message */ offRevAl_Graphics.drawString("Loading Images...",25,25); } /* end: write text message, "Loading Images..." */ /* frame entire buffer with black color */ offRevAl_Graphics.setColor(Color.black); offRevAl_Graphics.drawRect(0,0,MAIN_WDTH - 1,MAIN_HGHT - 1); /* blast buffer to screen */ g.drawImage(offRevAl_Image,0,0,this); } /* end method "paint" */ } /* end class "RevAl_AnimPortrait" */ /* end file "RevAl_AnimPortrait.java" */