/* "Hong_Kong_Master.java" */ /* 1996 J.H. Rosen */ /* java applet, "Hong Kong Master" (with kicking and punching action poses) */ import java.applet.Applet; import java.awt.*; public class Hong_Kong_Master extends java.applet.Applet implements Runnable { /* program constants */ /* button width and height */ final int btn_wdth = 86; final int btn_hght = 85; /* all btns: y coord */ final int btn_y = 514; /* btn 1: punch left x coord */ final int btn_1_x = 12; /* btn 2: kick left x coord */ final int btn_2_x = 108; /* btn 3: kick right x coord */ final int btn_3_x = 204; /* btn 4: punch right x coord */ final int btn_4_x = 300; /* variables */ /* font */ Font txt_Font; /* images */ /* Hong Kong Master body gif */ Image body_Image; /* Hong Kong Master left arm gifs */ Image arm_left_relaxed_Image; Image arm_left_punch_Image; /* Hong Kong Master right arm gifs */ Image arm_right_relaxed_Image; Image arm_right_punch_Image; /* Hong Kong Master left leg gifs */ Image leg_left_relaxed_Image; Image leg_left_kick_Image; /* Hong Kong Master right leg gifs */ Image leg_right_relaxed_Image; Image leg_right_kick_Image; /* Hong Kong Master buttons gif */ Image buttons_Image; /* Hong Kong Master, buttons relaxed/punch-kick flag (4 btns X 0=relaxed;1=punch-kick) */ int button_Flg[]; /* Hong Kong Master, MediaTracker */ MediaTracker hongKongMaster_MediaTracker; /* Hong Kong Master, offscreen graphics context (composite buffer) */ Image offHongKongMaster_Image; Graphics offHongKongMaster_Graphics; /* Hong Kong Master, animation thread */ Thread animation_Thread; /* methods invoked by Java */ /* "init()" * initialize the applet */ public void init() { /* init btns loop count */ int lpCnt; /* 398W X 611H canvas */ resize(398,611); /* text font */ txt_Font = new Font("Helvetica", Font.PLAIN, 9); /* create offscreen graphics context, Hong Kong Master composite buffer */ /* rect = 0,0,398,611 (398W X 611H) */ offHongKongMaster_Image = createImage(398,611); offHongKongMaster_Graphics = offHongKongMaster_Image.getGraphics(); /* retrieve .gif images */ /* body (198W X 333H) */ body_Image = getImage(getCodeBase(),"images/body.gif"); /* arm, left/relaxed (32W X 112H) */ arm_left_relaxed_Image = getImage(getCodeBase(),"images/arm_left_relaxed.gif"); /* arm, left/punch (114W X 39H) */ arm_left_punch_Image = getImage(getCodeBase(),"images/arm_left_punch.gif"); /* arm, right/relaxed (35W X 117H) */ arm_right_relaxed_Image = getImage(getCodeBase(),"images/arm_right_relaxed.gif"); /* arm, right/punch (149W X 60H) */ arm_right_punch_Image = getImage(getCodeBase(),"images/arm_right_punch.gif"); /* leg, left/relaxed (76W X 159H) */ leg_left_relaxed_Image = getImage(getCodeBase(),"images/leg_left_relaxed.gif"); /* leg, left/kick (176W X 71H) */ leg_left_kick_Image = getImage(getCodeBase(),"images/leg_left_kick.gif"); /* leg, right/relaxed (72W X 159H) */ leg_right_relaxed_Image = getImage(getCodeBase(),"images/leg_right_relaxed.gif"); /* leg, right/kick (187W X 65H) */ leg_right_kick_Image = getImage(getCodeBase(),"images/leg_right_kick.gif"); /* buttons (347W X 85H) */ buttons_Image = getImage(getCodeBase(),"images/buttons.gif"); /* init Hong Kong Master MediaTracker */ hongKongMaster_MediaTracker = new MediaTracker(this); /* load images into MediaTracker */ hongKongMaster_MediaTracker.addImage(body_Image,0); hongKongMaster_MediaTracker.addImage(arm_left_relaxed_Image,1); hongKongMaster_MediaTracker.addImage(arm_left_punch_Image,2); hongKongMaster_MediaTracker.addImage(arm_right_relaxed_Image,3); hongKongMaster_MediaTracker.addImage(arm_right_punch_Image,4); hongKongMaster_MediaTracker.addImage(leg_left_relaxed_Image,5); hongKongMaster_MediaTracker.addImage(leg_left_kick_Image,6); hongKongMaster_MediaTracker.addImage(leg_right_relaxed_Image,7); hongKongMaster_MediaTracker.addImage(leg_right_kick_Image,8); hongKongMaster_MediaTracker.addImage(buttons_Image,9); /* init buttons flag (4 btns X 0=relaxed;1=punch-kick) */ button_Flg = new int[4]; for (lpCnt = 0; lpCnt < 3; lpCnt++) button_Flg[lpCnt] = 0; // initially relaxed (0) } /* 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 Object, Hong Kong Master composite buffer */ offHongKongMaster_Graphics.dispose(); offHongKongMaster_Graphics = null; } /* end method "destroy" */ /* "mouseDown()" * called when the mouse button is down */ public boolean mouseDown(Event evt, int x, int y) { // begin: btn 1, punch left if ( ((x >= btn_1_x) && (x <= (btn_1_x + btn_wdth))) && ((y >= btn_y) && (y <= (btn_y + btn_hght))) ) { // toggle relaxed/punch if (button_Flg[0] == 0) button_Flg[0] = 1; else button_Flg[0] = 0; /* force repaint */ repaint(); } // end: btn 1, punch left // begin: btn 2, kick left else if ( ((x >= btn_2_x) && (x <= (btn_2_x + btn_wdth))) && ((y >= btn_y) && (y <= (btn_y + btn_hght))) ) { // toggle relaxed/punch if (button_Flg[1] == 0) button_Flg[1] = 1; else button_Flg[1] = 0; /* force repaint */ repaint(); } // end: btn 2, kick left // begin: btn 3, punch right else if ( ((x >= btn_3_x) && (x <= (btn_3_x + btn_wdth))) && ((y >= btn_y) && (y <= (btn_y + btn_hght))) ) { // toggle relaxed/punch if (button_Flg[2] == 0) button_Flg[2] = 1; else button_Flg[2] = 0; /* force repaint */ repaint(); } // end: btn 3, punch right // begin: btn 4, punch right else if ( ((x >= btn_4_x) && (x <= (btn_4_x + btn_wdth))) && ((y >= btn_y) && (y <= (btn_y + btn_hght))) ) { // toggle relaxed/punch if (button_Flg[3] == 0) button_Flg[3] = 1; else button_Flg[3] = 0; /* force repaint */ repaint(); } // end: btn 4, punch right return true; } /* end method "mouseDown" */ /* "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 */ offHongKongMaster_Graphics.setColor(Color.white); offHongKongMaster_Graphics.fillRect(0,0,398,611); /* frame entire buffer with black color */ offHongKongMaster_Graphics.setColor(Color.black); offHongKongMaster_Graphics.drawRect(0,0,397,610); /* wait for images to laod before drawing */ if (hongKongMaster_MediaTracker.checkAll(true) == true) { /* we can turn anim thread off now (we're done laoding) */ animation_Thread.stop(); /* draw body (in buffer) */ offHongKongMaster_Graphics.drawImage(body_Image,82,12,this); /* draw arms (in buffer) */ // left if (button_Flg[0] == 0) offHongKongMaster_Graphics.drawImage(arm_left_relaxed_Image,86,221,this); else offHongKongMaster_Graphics.drawImage(arm_left_punch_Image,4,221,this); // right if (button_Flg[3] == 0) offHongKongMaster_Graphics.drawImage(arm_right_relaxed_Image,244,216,this); else offHongKongMaster_Graphics.drawImage(arm_right_punch_Image,244,216,this); /* draw legs in buffer */ // left if (button_Flg[1] == 0) offHongKongMaster_Graphics.drawImage(leg_left_relaxed_Image,105,345,this); else offHongKongMaster_Graphics.drawImage(leg_left_kick_Image,11,345,this); // right if (button_Flg[2] == 0) offHongKongMaster_Graphics.drawImage(leg_right_relaxed_Image,201,345,this); else offHongKongMaster_Graphics.drawImage(leg_right_kick_Image,195,345,this); /* draw buttons in buffer */ offHongKongMaster_Graphics.drawImage(buttons_Image,12,514,this); /* button activate borders (?) */ // left arm punch if (button_Flg[0] == 1) { /* frame btn with black color */ offHongKongMaster_Graphics.setColor(Color.black); offHongKongMaster_Graphics.drawRect( btn_1_x - 3, btn_y - 3, btn_wdth + 5, btn_hght + 5); offHongKongMaster_Graphics.drawRect( btn_1_x - 4, btn_y - 4, btn_wdth + 7, btn_hght + 7); } // left leg kick if (button_Flg[1] == 1) { /* frame btn with black color */ offHongKongMaster_Graphics.setColor(Color.black); offHongKongMaster_Graphics.drawRect( btn_2_x - 3, btn_y - 3, btn_wdth + 5, btn_hght + 5); offHongKongMaster_Graphics.drawRect( btn_2_x - 4, btn_y - 4, btn_wdth + 7, btn_hght + 7); } // right leg kick if (button_Flg[3] == 1) { /* frame btn with black color */ offHongKongMaster_Graphics.setColor(Color.black); offHongKongMaster_Graphics.drawRect( btn_4_x - 3, btn_y - 3, btn_wdth + 5, btn_hght + 5); offHongKongMaster_Graphics.drawRect( btn_4_x - 4, btn_y - 4, btn_wdth + 7, btn_hght + 7); } // right arm punch if (button_Flg[2] == 1) { /* frame btn with black color */ offHongKongMaster_Graphics.setColor(Color.black); offHongKongMaster_Graphics.drawRect( btn_3_x - 3, btn_y - 3, btn_wdth + 5, btn_hght + 5); offHongKongMaster_Graphics.drawRect( btn_3_x - 4, btn_y - 4, btn_wdth + 7, btn_hght + 7); } } // end "if (hongKongMaster_MediaTracker.checkAll(true) == true)" /* begin: write text message, "Loading Images..." */ else { /* set text string font */ offHongKongMaster_Graphics.setFont(txt_Font); /* set text string color */ offHongKongMaster_Graphics.setColor(Color.black); /* write/draw text message */ offHongKongMaster_Graphics.drawString("Loading Images...",25,25); } /* end: write text message, "Loading Images..." */ /* blast buffer to screen */ g.drawImage(offHongKongMaster_Image,0,0,this); } /* end method "paint" */ } /* end class "Hong_Kong_Master" */ /* end file "Hong_Kong_Master.java" */