/* "PeanutReign.java" */ /* 1997 Joe Rosen */ /* java applet, Jimmy Carter's "Peanut Reign" */ import java.applet.Applet; import java.awt.*; // "Peanut" class */ class Peanut { // vars /* top/left Peanut coords */ int peanut_top; int peanut_left; /* peanut image frame (flip-flop 0/1) */ int peanut_frame; /* flag, is Peanut activated ? */ boolean peanut_active_flg; // true= activated // methods // constructer Peanut(int top, int left) { /* top/left coords on creation */ peanut_top = top; peanut_left = left; /* peanut image frame (flip-flop 0/1) */ peanut_frame = 0; // initially frame 0 /* peanut activation flag */ peanut_active_flg = false; // initially inactive } } // end "Peanut" class // "PeanutReign" applet class public class PeanutReign extends java.applet.Applet implements Runnable { /* program constants */ /* applet canvas size */ final int APPLET_WDTH = 329; final int APPLET_HGHT = 396; /* bust width and height */ final int BUST_WDTH = 223; final int BUST_HGHT = 325; /* bust top/left */ final int BUST_TOP = APPLET_HGHT - BUST_HGHT; final int BUST_LEFT = APPLET_WDTH - BUST_WDTH; /* bust image indices */ final int BUST_FROWN = 0; final int BUST_SMILE = 1; /* eyes width and height */ final int EYES_WDTH = 79; final int EYES_HGHT = 21; /* eyes top/left (offset from bust) */ final int EYES_TOP = BUST_TOP + 86; final int EYES_LEFT = BUST_LEFT + 57; /* eyes open/close flag */ final int EYES_CLD = 0; final int EYES_OPN = 1; /* american flag width and height */ final int USAFLAG_WDTH = 102; final int USAFLAG_HGHT = 392; /* american flag top/left */ final int USAFLAG_TOP = 2; final int USAFLAG_LEFT = 2; /* total number of Peanut objects */ final int TOTAL_PEANUTS = 6; /* Peanut width and height */ final int PEANUT_WDTH = 46; final int PEANUT_HGHT = 112; /* variables */ /* images */ /* bust gifs (2) */ Image bust[]; /* eyes open gifs (2) */ Image eyes_opn[]; /* eyes closed gifs (2) */ Image eyes_cld[]; /* eyes patriotic gifs (2) */ Image eyes_patriot[]; /* american flag gifs (2) */ Image americanflag[]; /* peanut gifs (2) */ Image peanut[]; /* Jimmy Carter, MediaTracker */ MediaTracker jimmyCarter_MediaTracker; /* Jimmy Carter, offscreen graphics context (composite buffer) */ Image offJimmyCarter_Image; Graphics offJimmyCarter_Graphics; /* bust index */ int bust_Idx; /* frown/smile delay */ long frownSmile_Delay; /* eyes status flag (0= closed/1= open/2= patriotic) */ int eyesStatus_Flg; /* eyes blink flg */ boolean eyesBlink_Flg; /* eyes blink delay */ long eyesBlink_Delay; /* eyes patriotic delay */ long eyesPatriot_Delay; /* eyes patriotic duration start time (eyes always stay patriotic 1 second) */ long eyesPatriot_Duration; /* american flag index (2 frames) */ int americanflag_Idx; /* Peanut objects (TOTAL_PEANUTS) */ Peanut a_Peanut[]; /* Peanut animation activation flag */ boolean peanutAnim_Flg; // true= active /* Peanut animation start time (start a new raining Peanut[] every .5 to 1 sec) */ long peanutAnim_StartTime; /* Jimmy Carter, animation thread */ Thread animation_Thread = null; /* methods invoked by Java */ /* "init()" * initialize the applet */ public void init() { /* initialization loop counter */ int lpCnt; /* Peanut[] left */ int left_coord; /* resize canvas */ resize(APPLET_WDTH,APPLET_HGHT); /* create offscreen graphics context, Jimmy Carter composite buffer */ offJimmyCarter_Image = createImage(APPLET_WDTH,APPLET_HGHT); offJimmyCarter_Graphics = offJimmyCarter_Image.getGraphics(); /* retrieve .gif images */ /* bust */ bust = new Image[2]; bust[BUST_FROWN] = getImage(getCodeBase(),"images/bust_frown.gif"); bust[BUST_SMILE] = getImage(getCodeBase(),"images/bust_smile.gif"); /* eyes (open) */ eyes_opn = new Image[2]; eyes_opn[BUST_FROWN] = getImage(getCodeBase(),"images/eyes_opn_frown.gif"); eyes_opn[BUST_SMILE] = getImage(getCodeBase(),"images/eyes_opn_smile.gif"); /* eyes (closed) */ eyes_cld = new Image[2]; eyes_cld[BUST_FROWN] = getImage(getCodeBase(),"images/eyes_cld_frown.gif"); eyes_cld[BUST_SMILE] = getImage(getCodeBase(),"images/eyes_cld_smile.gif"); /* eyes patriotic gifs (2) */ eyes_patriot = new Image[2]; eyes_patriot[BUST_FROWN] = getImage(getCodeBase(),"images/eyes_patriot_frown.gif"); eyes_patriot[BUST_SMILE] = getImage(getCodeBase(),"images/eyes_patriot_smile.gif"); /* american flag gifs (2) */ americanflag = new Image[2]; americanflag[0] = getImage(getCodeBase(),"images/flag_1.gif"); americanflag[1] = getImage(getCodeBase(),"images/flag_2.gif"); /* peanut gifs (2) */ peanut = new Image[2]; peanut[0] = getImage(getCodeBase(),"images/peanut_1.gif"); peanut[1] = getImage(getCodeBase(),"images/peanut_2.gif"); /* init Jimmy Carter MediaTracker */ jimmyCarter_MediaTracker = new MediaTracker(this); /* load images into MediaTracker */ jimmyCarter_MediaTracker.addImage(bust[BUST_FROWN],0); jimmyCarter_MediaTracker.addImage(bust[BUST_SMILE],1); jimmyCarter_MediaTracker.addImage(eyes_opn[BUST_FROWN],2); jimmyCarter_MediaTracker.addImage(eyes_opn[BUST_SMILE],3); jimmyCarter_MediaTracker.addImage(eyes_cld[BUST_FROWN],4); jimmyCarter_MediaTracker.addImage(eyes_cld[BUST_SMILE],5); jimmyCarter_MediaTracker.addImage(eyes_patriot[BUST_FROWN],6); jimmyCarter_MediaTracker.addImage(eyes_patriot[BUST_SMILE],7); jimmyCarter_MediaTracker.addImage(americanflag[0],8); jimmyCarter_MediaTracker.addImage(americanflag[1],9); jimmyCarter_MediaTracker.addImage(peanut[0],10); jimmyCarter_MediaTracker.addImage(peanut[1],11); /* init bust index */ bust_Idx = BUST_FROWN; /* init frown/smile delay (initially .25 to 1.5 secs) */ frownSmile_Delay = System.currentTimeMillis() + (((long) (Math.random() * 1250)) + 250); /* init eyes status flag (0= closed/1= open/2= patriotic) */ eyesStatus_Flg = 1; // initially eyes open /* init eyes blink flg */ eyesBlink_Flg = false; // initially no blinking /* init eyes blink delay (blink every .50 to 1.5 seconds) */ eyesBlink_Delay = System.currentTimeMillis() + (((long) (Math.random() * 1000)) + 500); /* init eyes patriotic delay (patriotic every 5 to 10 seconds) */ eyesPatriot_Delay = System.currentTimeMillis() + (((long) (Math.random() * 5000)) + 5000); /* init american flag index (2 frames) */ americanflag_Idx = 0; // initially frame 0 /* create Peanut objects (TOTAL_PEANUTS # of Peanut[]s) */ a_Peanut = new Peanut[TOTAL_PEANUTS]; left_coord = 9; for (lpCnt = 0; lpCnt < TOTAL_PEANUTS; lpCnt++) { a_Peanut[lpCnt] = new Peanut(5 - PEANUT_HGHT,left_coord); left_coord = left_coord + (PEANUT_WDTH + 7); } /* init, Peanut animation activation flag */ peanutAnim_Flg = false; // initially inactive } /* end method "init" */ /* "start()" * called when the applet becomes visible on screen */ public void start() { /* start 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 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 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 context, Jimmy Carter composite buffer */ offJimmyCarter_Graphics.dispose(); offJimmyCarter_Graphics = null; } /* end method "destroy" */ /* "mouseDown()" * called when the mouse button is pressed down */ public boolean mouseDown(Event evt, int x, int y) { /* activate 1st Peanut[] */ ActivatePeanut(); /* Peanut animation activation flag */ peanutAnim_Flg = true; // activate; /* force immediate repaint */ repaint(); return true; } /* end method "mouseDown" */ /* "mouseUp()" * called when the mouse button comes back up */ public boolean mouseUp(Event evt, int x, int y) { /* loop counter */ int lpCnt; /* Peanut animation activation flag */ peanutAnim_Flg = false; // deactivate; /* deactivate all active Peanut[]s */ for (lpCnt = 0; lpCnt < TOTAL_PEANUTS; lpCnt++) if (a_Peanut[lpCnt].peanut_active_flg == true) a_Peanut[lpCnt].peanut_active_flg = false; /* force immediate repaint */ repaint(); return true; } /* end method "mouseUp" */ /* "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) { /* loop counter */ int lpCnt; /* idx of activated Peanut[]s */ int peanut_Idx; /* font */ Font txt_Font; /* minimum time between frown/smile */ long minimumFrownSmileTime; /* fill buffer with white paint */ offJimmyCarter_Graphics.setColor(Color.white); offJimmyCarter_Graphics.fillRect(0,0,APPLET_WDTH,APPLET_HGHT); /* wait for images to laod before drawing */ if (jimmyCarter_MediaTracker.checkAll(true) == true) { if (peanutAnim_Flg == false) { /* frown/smile anim update */ if (frownSmile_Delay <= System.currentTimeMillis()) { if (bust_Idx == BUST_SMILE) { bust_Idx = BUST_FROWN; /* minimum time between frown/smile */ minimumFrownSmileTime = 250; } else { bust_Idx = BUST_SMILE; /* minimum time between frown/smile */ minimumFrownSmileTime = 1250; // smile's longer than frown } /* re-init frown/smile delay */ frownSmile_Delay = System.currentTimeMillis() + (((long) (Math.random() * 1250)) + minimumFrownSmileTime); } /* eyes blink update (never blink during patriotic eyes) */ if ((eyesBlink_Delay <= System.currentTimeMillis()) && (eyesStatus_Flg != 2)) { /* mark eyes status flag */ eyesStatus_Flg = 0; // flag eyes closed /* re-init eyes blink delay (blink every .50 to 1.5 seconds) */ eyesBlink_Delay = System.currentTimeMillis() + (((long) (Math.random() * 1000)) + 500); } /* eyes patriotic update */ if (eyesPatriot_Delay <= System.currentTimeMillis()) { /* mark eyes status flag */ eyesStatus_Flg = 2; // flag eyes patriotic /* re-init eyes patriotic delay (blink every 5 to 10 seconds) */ eyesPatriot_Delay = System.currentTimeMillis() + (((long) (Math.random() * 5000)) + 5000); /* init eyes patriotic duration start time (eyes always stay patriotic 1 second) */ eyesPatriot_Duration = System.currentTimeMillis(); } /* draw bust (in buffer) */ offJimmyCarter_Graphics.drawImage(bust[bust_Idx],BUST_LEFT,BUST_TOP,this); /* draw eyes (in buffer) */ /* patriotic */ if (eyesStatus_Flg == 2) { offJimmyCarter_Graphics.drawImage(eyes_patriot[bust_Idx],EYES_LEFT,EYES_TOP,this); /* eyes patriotic return (after 1.25 seconds) */ if (System.currentTimeMillis() >= (eyesPatriot_Duration + 1250)) /* mark eyes status flag */ eyesStatus_Flg = 1; // flag eyes open } /* closed */ else if (eyesStatus_Flg == 0) { offJimmyCarter_Graphics.drawImage(eyes_cld[bust_Idx],EYES_LEFT,EYES_TOP,this); /* mark eyes status flag */ eyesStatus_Flg = 1; // flag eyes open } /* open */ else if (eyesStatus_Flg == 1) offJimmyCarter_Graphics.drawImage(eyes_opn[bust_Idx],EYES_LEFT,EYES_TOP,this); /* draw american flag */ offJimmyCarter_Graphics.drawImage( americanflag[americanflag_Idx],USAFLAG_LEFT,USAFLAG_TOP,this); /* alternate american flag images (2 frames flip-flop) */ if (americanflag_Idx == 1) americanflag_Idx = 0; else americanflag_Idx = 1; } // begin: Peanut animation else { /* loop through each Peanut */ for (lpCnt = 0; lpCnt < TOTAL_PEANUTS; lpCnt++) { // is Peanut active? if (a_Peanut[lpCnt].peanut_active_flg == true) { /* set color */ offJimmyCarter_Graphics.setColor(Color.black); /* draw Peanut */ offJimmyCarter_Graphics.drawImage( peanut[a_Peanut[lpCnt].peanut_frame], a_Peanut[lpCnt].peanut_left, a_Peanut[lpCnt].peanut_top, this); // flip-flop Peanut frames (1/0) if (a_Peanut[lpCnt].peanut_frame == 0) a_Peanut[lpCnt].peanut_frame = 1; else a_Peanut[lpCnt].peanut_frame = 0; // advance Peanut[] position? if ( (a_Peanut[lpCnt].peanut_top + 25) < APPLET_HGHT ) a_Peanut[lpCnt].peanut_top = a_Peanut[lpCnt].peanut_top + 25; // Peanut[] at bottom of screen (deactivate) ? else a_Peanut[lpCnt].peanut_active_flg = false; } // end "is Peanut active?" } // end "for (lpCnt = 0; lpCnt < TOTAL_PEANUTS; lpCnt++)" // start another raining Peanut[]? (every .5 to 1 sec) if ( System.currentTimeMillis() > (peanutAnim_StartTime + (((long) (Math.random() * 1000)) + 500)) ) /* activate another Peanut[] */ ActivatePeanut(); } // end: "Peanut animation" } // end "if (jimmyCarter_MediaTracker.checkAll(true) == true)" /* begin: write text message, "Loading Images..." */ else { /* text font */ txt_Font = new Font("Helvetica", Font.PLAIN, 9); /* set text string font */ offJimmyCarter_Graphics.setFont(txt_Font); /* set text string color */ offJimmyCarter_Graphics.setColor(Color.black); /* write/draw text message */ offJimmyCarter_Graphics.drawString("Loading Images...",25,25); } /* end: write text message, "Loading Images..." */ /* frame entire buffer with black color */ offJimmyCarter_Graphics.setColor(Color.black); offJimmyCarter_Graphics.drawRect(0,0,APPLET_WDTH - 1,APPLET_HGHT - 1); /* blast buffer to screen */ g.drawImage(offJimmyCarter_Image,0,0,this); } /* end method "paint" */ /* functions */ /* begin: function "ActivatePeanut" */ void ActivatePeanut() { /* loop counter */ int lpCnt; /* inactive Peanut[] flag */ boolean inactivePeanut_Flg = false; // assume all Peanut[]s are active /* Peanut[] index */ int peanut_Idx; /* ensure there are Peanut[]s available to activate */ for (lpCnt = 0; lpCnt < TOTAL_PEANUTS; lpCnt++) { if (a_Peanut[lpCnt].peanut_active_flg == false) { // flag if there's at least 1 inactive peanut inactivePeanut_Flg = true; // break from for loop break; } } // return immediately if there are no inactive Peanut[]s if (inactivePeanut_Flg == false) return; // attempt to randomly select a Peanut to activate peanut_Idx = (int) (Math.random() * TOTAL_PEANUTS); while(a_Peanut[peanut_Idx].peanut_active_flg == true) peanut_Idx = (int) (Math.random() * TOTAL_PEANUTS); // activate Peanut[] a_Peanut[peanut_Idx].peanut_active_flg = true; // re-set Peanut[]'s top coord a_Peanut[peanut_Idx].peanut_top = 5 - PEANUT_HGHT; /* init Peanut animation start time (start a new raining Peanut[] every .5 to 1 sec) */ peanutAnim_StartTime = System.currentTimeMillis(); // return return; } /* end: function "ActivatePeanut" */ } /* end "PeanutReign" applet class */ /* end file "PeanutReign.java" */