/* "ManipulableMilhous.java" */ /* "Manipulable Richard M. Nixon" Applet */ /* 1996 MNI Interactive (coded by Joe Rosen) */ import java.applet.Applet; import java.awt.*; /* begin: applet */ public class ManipulableMilhous extends java.applet.Applet implements Runnable { /* variables */ /* font */ Font txt_Font; // slider btn Rectangle sliderBtn_Rect; // groove slider btn rides in Rectangle sliderGroove_Rect; // slider button state (dragging/not dragging) boolean sliderBtn_boolean; /* offscreen anim/image buffer, "ManipulableMilhous" canvas */ Image offScreen_Image; Graphics offScreen_Graphics; /* images */ Image headBody_Image; // head and body Image headBodyPeace_Image; // head and body with peace signs Image[] mouthFrown_Image; // mouth frown (3 frames) Image[] mouthSmile_Image; // mouth smile (3 frames) /* "ManipulableMilhous" MediaTracker */ MediaTracker manipulableMilhous_MediaTracker; /* animation thread */ Thread animation_Thread; /* animation counters */ int mouth_anim_count; // mouth anim (3 frames) /* methods invoked by Java */ /* "init()" * initialize the applet */ public void init() { /* array index */ int aryIdx; /* 340W X 370H canvas */ resize(340,370); /* text font */ txt_Font = new Font("Helvetica", Font.PLAIN, 9); /* create offscreen anim/image buffer, "ManipulableMilhous" canvas */ /* 340W X 370H */ offScreen_Image = createImage(340,370); offScreen_Graphics = offScreen_Image.getGraphics(); // init slider btn sliderBtn_Rect = new Rectangle(20,325,15,40); // init slider groove rect sliderGroove_Rect = new Rectangle(10,340,320,10); // init slider button state (dragging/not dragging) sliderBtn_boolean = false; // initially not dragging (false) /* retrieve head and body image (336W X 315H) */ headBody_Image = getImage(getCodeBase(),"images/headbody.gif"); /* retrieve head and body with peace signs image (336W X 315H) */ headBodyPeace_Image = getImage(getCodeBase(),"images/headbody_peace.gif"); /* init mouth frown and smile image array (3 frames each) */ mouthFrown_Image = new Image[3]; mouthSmile_Image = new Image[3]; /* retrieve mouth images (frown = 61W X 29H/smile = 61W X 29H) */ for (aryIdx = 0; aryIdx <= 2; aryIdx++) { mouthFrown_Image[aryIdx] = getImage(getCodeBase(),("images/mouthfrown_" + (aryIdx + 1) + ".gif")); mouthSmile_Image[aryIdx] = getImage(getCodeBase(),("images/mouthsmile_" + (aryIdx + 1) + ".gif")); } /* init "ManipulableMilhous" MediaTracker */ manipulableMilhous_MediaTracker = new MediaTracker(this); /* load images into MediaTracker */ manipulableMilhous_MediaTracker.addImage(headBody_Image,0); manipulableMilhous_MediaTracker.addImage(headBodyPeace_Image,1); manipulableMilhous_MediaTracker.addImage(mouthFrown_Image[0],2); manipulableMilhous_MediaTracker.addImage(mouthFrown_Image[1],3); manipulableMilhous_MediaTracker.addImage(mouthFrown_Image[2],4); manipulableMilhous_MediaTracker.addImage(mouthSmile_Image[0],5); manipulableMilhous_MediaTracker.addImage(mouthSmile_Image[1],6); manipulableMilhous_MediaTracker.addImage(mouthSmile_Image[2],7); /* animation counters */ mouth_anim_count = 0; // mouth frown (3 frames, 0 = frame 1) } /* end method "init" */ /* "start()" * called when the applet becomes visible on screen */ public void start() { /* start numanim animation thread */ animation_Thread = new Thread(this); animation_Thread.start(); } /* end method "start" */ /* "stop()" * called when the applet is no longer visible on screen */ public void stop() { /* stop numanim animation 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 anim/image buffer, "ManipulableMilhous" canvas */ offScreen_Graphics.dispose(); offScreen_Graphics = null; } /* end method "destroy" */ /* "run()" * called (repeatedly) by the Thread created in "start()" */ public void run() { /* update animation thread */ while (Thread.currentThread() == animation_Thread) { /* force repaint */ repaint(); /* inc/reset animation counters */ // inc/reset mouth anim cnt (3 frames, 0 = frame 1) if (mouth_anim_count < 2) mouth_anim_count++; else mouth_anim_count = 0; /* 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" */ /* "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 entire buffer with white color */ offScreen_Graphics.setColor(Color.white); offScreen_Graphics.fillRect(0,0,340,370); /* frame entire buffer with black color */ offScreen_Graphics.setColor(Color.black); offScreen_Graphics.drawRect(0,0,339,369); /* wait for images to laod before drawing */ if (manipulableMilhous_MediaTracker.checkAll(true) == true) { // draw slider groove offScreen_Graphics.setColor(Color.black); offScreen_Graphics.drawRect( sliderGroove_Rect.x, sliderGroove_Rect.y, sliderGroove_Rect.width, sliderGroove_Rect.height ); // draw slider button offScreen_Graphics.setColor(Color.white); offScreen_Graphics.fillRect( sliderBtn_Rect.x, sliderBtn_Rect.y, sliderBtn_Rect.width, sliderBtn_Rect.height ); offScreen_Graphics.setColor(Color.black); offScreen_Graphics.drawRect( sliderBtn_Rect.x, sliderBtn_Rect.y, sliderBtn_Rect.width, sliderBtn_Rect.height ); // draw head and body image if ( (sliderBtn_Rect.x + (sliderBtn_Rect.width / 2)) <= ((320 / 3) * 2) ) offScreen_Graphics.drawImage(headBody_Image,2,2,this); else offScreen_Graphics.drawImage(headBodyPeace_Image,2,2,this); // draw mouth image if ( (sliderBtn_Rect.x + (sliderBtn_Rect.width / 2)) <= (320 / 3) ) // draw frown @ 131 + 2,143 + 2 offScreen_Graphics.drawImage(mouthFrown_Image[mouth_anim_count],133,145,this); else // draw smile @ 135 + 2,143 + 2 offScreen_Graphics.drawImage(mouthSmile_Image[mouth_anim_count],137,145,this); } // end "if (manipulableMilhous_MediaTracker.checkAll(true) == true)" /* begin: write text message, "Loading Images..." */ else { /* set text string font */ offScreen_Graphics.setFont(txt_Font); /* set text string color */ offScreen_Graphics.setColor(Color.black); /* write/draw text message */ offScreen_Graphics.drawString("Loading Images...",25,25); } /* end: write text message, "Loading Images..." */ /* blast offscreen anim/image buffer to screen */ g.drawImage(offScreen_Image,0,0,this); } /* end method "paint" */ /* method "mouseDown()" */ public boolean mouseDown(Event e, int x, int y) { /* wait for images to laod before drawing */ if (manipulableMilhous_MediaTracker.checkAll(true) == true) { if ( ( (x > sliderBtn_Rect.x) && (x < (sliderBtn_Rect.x + sliderBtn_Rect.width)) ) && ( (y > sliderBtn_Rect.y) && (y < (sliderBtn_Rect.y + sliderBtn_Rect.height)) ) ) if (sliderBtn_boolean == false) sliderBtn_boolean = true; } // end "if (manipulableMillhous_MediaTracker.checkAll(true) == true)" return true; } /* end method "mouseDown" */ /* method "mouseUp()" */ public boolean mouseUp(Event e, int x, int y) { /* wait for images to laod before drawing */ if (manipulableMilhous_MediaTracker.checkAll(true) == true) { if (sliderBtn_boolean == true) sliderBtn_boolean = false; } // end "if (manipulableMilhous_MediaTracker.checkAll(true) == true)" return true; } /* end method "mouseUp" */ /* method "mouseDrag()" */ public boolean mouseDrag(Event e, int x, int y) { /* wait for images to laod before drawing */ if (manipulableMilhous_MediaTracker.checkAll(true) == true) { if (sliderBtn_boolean == true) { if ( (x > (sliderGroove_Rect.x + (sliderBtn_Rect.width / 2))) && (x < ((sliderGroove_Rect.x + sliderGroove_Rect.width) - (sliderBtn_Rect.width / 2))) ) { sliderBtn_Rect.x = x - (sliderBtn_Rect.width / 2); /* force repaint */ repaint(); } else if ( (x < (sliderGroove_Rect.x + (sliderBtn_Rect.width / 2))) && (sliderBtn_Rect.x > sliderGroove_Rect.x) ) { sliderBtn_Rect.x = sliderGroove_Rect.x; /* force repaint */ repaint(); } else if ( (x > ((sliderGroove_Rect.x + sliderGroove_Rect.width) - (sliderBtn_Rect.width / 2))) && ((sliderBtn_Rect.x + sliderBtn_Rect.width) < (sliderGroove_Rect.x + sliderGroove_Rect.width)) ) { sliderBtn_Rect.x = (sliderGroove_Rect.x + sliderGroove_Rect.width) - sliderBtn_Rect.width; /* force repaint */ repaint(); } } // end "if (sliderBtn_boolean == true)" } // end "if (manipulableMilhous_MediaTracker.checkAll(true) == true)" return true; } /* end method "mouseDrag" */ } /* end class "ManipulableMilhous" */ /* end file "ManipulableMilhous.java */