/* "Mallomars.java" (created 3/6/96; modified 4/29/96) */
/* Java Applet, make your own "Mallomar"  cookies */

import java.applet.Applet;
import java.awt.*;

public class Mallomars extends java.applet.Applet implements Runnable {

 /* variables */

 /* graham cracker (beige) RGB color */
 Color      grahamBeige_Color;
 /* marshmellow (gray) RGB color */
 Color      marshmellowGray_Color;
 /* chocolate (brown) RGB color */
 Color      chocolateBrown_Color;

 /* offscreen graphics context, "Mallomars" composite buffer */
 Image      offMallomars_Image; 
 Graphics   offMallomars_Graphics;

 /* "Mallomars" animator thread */      
 Thread     mallomars_Thread;

 /* "Mallomars" machine engine (counter) */
 /* (counts through 12 steps x 4 phases of Mallomar manufacture) */     
 int        mallomarsMachineCnt;

 /* "graham cracker" anim count and dimensions */       
 int        grahamAnimCnt;
 int        grahamTopCoord, grahamHeight;

 /* "marshmellow" anim count and dimensions */
 int        marshmellowAnimCnt; 
 int        marshmellowTopCoord, marshmellowHeight;

 /* "chocolate" anim count and arc angles */
 int        chocolateAnimCnt;   
 int        chocolateStartAngle, chocolateArcAngle;

 /* methods invoked by Java */

 /* "init()"
  * initialize the applet */
 public void init()
 {

  /* 384 X 172 canvas */
  resize(384,172);

  /* create offscreen graphics context, "Mallomars" composite buffer */
  /* 0,0,384,172 */
  offMallomars_Image = createImage(384,172);
  offMallomars_Graphics = offMallomars_Image.getGraphics();

  /* mix graham cracker (beige) RGB color */
  grahamBeige_Color = new Color(206,156,99);
  /* marshmellow (gray) RGB color */
  marshmellowGray_Color = new Color(239,239,239);
  /* chocolate (brown) RGB color */
  chocolateBrown_Color = new Color(156,49,0);

  /* init "Mallomars" machine engine (counter) */       
  mallomarsMachineCnt = 10; /* at startup, machine idle for only 2 loops */

  /* init "graham cracker" anim count and dimensions */ 
  grahamAnimCnt = 1;
  grahamTopCoord = 38;
  grahamHeight = 128;

  /* init "marshmellow" dimensions */
  marshmellowAnimCnt = 1;               
  marshmellowTopCoord = 144;
  marshmellowHeight = 34;

  /* init "chocolate" anim count and arc angles */
  chocolateAnimCnt = 1; 
  chocolateStartAngle = 97;
  chocolateArcAngle = -14;

 } /* end method "init" */


 /* "start()"
  * called when the applet becomes visible on screen */
 public void start() {
                
  /* start "Mallomars" animator thread */ 
  mallomars_Thread = new Thread(this);
  mallomars_Thread.start();
  /* immediately assume suspended Mallomar manufacture */
  mallomars_Thread.suspend();

 } /* end method "start" */


 /* "run()"
  *     called (repeatedly) by the Thread created in "start()" */
 public void run() {
                
  /* update "Mallomars" animator thread */
  while (Thread.currentThread() == mallomars_Thread) {

   /* repaint (update) the screen */
   repaint(); 

   /* begin: which phase of manufacture? */

   /* Mallomar machine idle */
   if ((mallomarsMachineCnt >= 1) && (mallomarsMachineCnt < 12))
    /* inc Mallomar machine counter */
    mallomarsMachineCnt++;

   /* graham cracker */
   else if ((mallomarsMachineCnt >= 12) && (mallomarsMachineCnt < 24)) {
    /* inc graham cracker anim count and dimensions (?) */
    if (grahamAnimCnt < 12) {
     grahamAnimCnt++;
     grahamTopCoord = grahamTopCoord + 9;
     grahamHeight = grahamHeight - 9;
    } /* end "if (cookieAnimCnt < 12)" */
    /* reset graham cracker anim count and dimensions (?) */
    else if (grahamAnimCnt == 12) {
     grahamAnimCnt = 1;
     grahamTopCoord = 38;
     grahamHeight = 128;
    } /* end "else if (grahamAnimCnt == 12)" */
    /* inc Mallomar machine counter */
    mallomarsMachineCnt++;
   } /* end "graham cracker" */

   /* marshmellow */
   else if ((mallomarsMachineCnt >= 24) && (mallomarsMachineCnt < 36)) {
    /* inc marshmellow anim count and dimensions (?) */
    if (marshmellowAnimCnt < 12) {
     marshmellowAnimCnt++;
     marshmellowTopCoord = marshmellowTopCoord - 5;
     marshmellowHeight = marshmellowHeight + 10;
    } /* end "if (marshmellowAnimCnt < 12)" */
    /* reset marshmellow anim count and dimensions (?) */
    else if (marshmellowAnimCnt == 12) {
     marshmellowAnimCnt = 1;
     marshmellowTopCoord = 144;
     marshmellowHeight = 34;
    } /* end "else if (marshmellowAnimCnt == 12)" */
    /* inc Mallomar machine counter */
    mallomarsMachineCnt++;
   } /* end "marshmellow" */

   /* chocolate */
   else if ((mallomarsMachineCnt >= 36) && (mallomarsMachineCnt < 48)) {
    /* inc chocolate anim count and dimensions (?) */
    if (chocolateAnimCnt < 12) {
     chocolateAnimCnt++;
     chocolateStartAngle = chocolateStartAngle + 7;
     chocolateArcAngle = chocolateArcAngle - 13;
    } /* end "if (chocolateAnimCnt < 12)" */
    /* reset "chocolate" anim count and arc angles (?) */
    else if (chocolateAnimCnt == 12) {
     chocolateAnimCnt = 1;      
     chocolateStartAngle = 97;
     chocolateArcAngle = -14;
    } /* end "else if (chocolateAnimCnt == 12)" */
    /* inc Mallomar machine counter */
    mallomarsMachineCnt++;
   } /* end "chocolate" */

   /* reset */
   else if (mallomarsMachineCnt == 48)
    /* reset Mallomar machine counter */
    mallomarsMachineCnt = 1;

   /* begin: thread delay (125 milliseconds) */
   try {
    Thread.sleep(125);
   } catch (InterruptedException e) {
      break;
     } /* end: thread delay */

  } /* end "while (Thread.currentThread() == mallomars_Thread)" */

 } /* end method "run" */


 /* "stop()"
  *     called when the applet is no longer visible on screen */
 public void stop() {
                
  /* stop "Mallomars" animator thread */
  mallomars_Thread.stop(); 
  mallomars_Thread = null;

  /* dispose offscreen Graphics Object, "Mallomars" composite buffer */
  offMallomars_Graphics = null; 

 } /* end method "stop" */


 /* "mouseEnter()"
  * mouse enters applet */
 public boolean mouseEnter(Event e, int x, int y)
 {

  /* on mouse entry into applet, resume Mallomar manufacture */
  mallomars_Thread.resume();
  return true;

 } /* end method "mouseEnter" */


 /* "mouseExit()"
  * mouse leaves applet */
 public boolean mouseExit(Event e, int x, int y)
 {

  /* on mouse exit from applet, suspend Mallomar manufacture */
  mallomars_Thread.suspend();
  return true;

 } /* end method "mouseExit" */


 /* "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)
 { 

  /* marshmellow and chocolate (pouring) */
  int   pour_topCood;

  /* "Mallomars" text string's font */
  Font  mallomarsFont = new Font("Helvetica", Font.BOLD, 36);

  /* fill the entire buffer with yellow paint */
  offMallomars_Graphics.setColor(Color.yellow);
  offMallomars_Graphics.fillRect(0,0,384,172);
  offMallomars_Graphics.setColor(Color.black);

  /* write "Mallomars" text string to buffer */
  offMallomars_Graphics.setColor(Color.blue);
  offMallomars_Graphics.setFont(mallomarsFont);
  offMallomars_Graphics.drawString("Mallomars",5,31);

  /* begin: draw make Mallomars cookies buttons in buffer */

  /* begin: graham cracker btn */
  /* btn box */
  offMallomars_Graphics.setColor(Color.white);
  offMallomars_Graphics.fillRect(25,120,49,41);
  offMallomars_Graphics.setColor(Color.black);
  offMallomars_Graphics.drawRect(25,120,49,41);
  /* graham cracker */
  offMallomars_Graphics.setColor(grahamBeige_Color);
  offMallomars_Graphics.fillOval(28,149,43,10);
  offMallomars_Graphics.setColor(Color.black);
  offMallomars_Graphics.drawOval(28,149,43,10);
  /* btn hilite (?) */
  if ((mallomarsMachineCnt >= 12) && (mallomarsMachineCnt < 24)) {
   offMallomars_Graphics.setColor(Color.black);
   offMallomars_Graphics.drawRect(21,116,57,49);
   offMallomars_Graphics.drawRect(22,117,55,47);
  } /* end "btn hilite (?)" */
  /* end: graham cracker btn */

  /* begin: marshmellow btn */
  /* btn box */
  offMallomars_Graphics.setColor(Color.white);
  offMallomars_Graphics.fillRect(99,80,49,41);
  offMallomars_Graphics.setColor(Color.black);
  offMallomars_Graphics.drawRect(99,80,49,41);
  /* graham cracker */
  offMallomars_Graphics.setColor(grahamBeige_Color);
  offMallomars_Graphics.fillOval(102,109,43,10);
  offMallomars_Graphics.setColor(Color.black);
  offMallomars_Graphics.drawOval(102,109,43,10);
  /* marshmellow */
  offMallomars_Graphics.setColor(marshmellowGray_Color);
  offMallomars_Graphics.fillArc(104,86,39,58,176,-172);
  offMallomars_Graphics.setColor(Color.black);
  offMallomars_Graphics.drawArc(104,86,39,58,176,-172);
  /* btn hilite (?) */
  if ((mallomarsMachineCnt >= 24) && (mallomarsMachineCnt < 36)) {
   offMallomars_Graphics.setColor(Color.black);
   offMallomars_Graphics.drawRect(95,76,57,49);
   offMallomars_Graphics.drawRect(96,77,55,47);
  } /* end "btn hilite (?)" */
  /* end: marshmellow btn */

  /* begin: chocolate btn */
  /* btn box */
  offMallomars_Graphics.setColor(Color.white);
  offMallomars_Graphics.fillRect(25,40,49,41);
  offMallomars_Graphics.setColor(Color.black);
  offMallomars_Graphics.drawRect(25,40,49,41);
  /* graham cracker (cover in chocolate) */
  offMallomars_Graphics.setColor(chocolateBrown_Color);
  offMallomars_Graphics.fillOval(28,69,43,10);
  offMallomars_Graphics.setColor(Color.black);
  offMallomars_Graphics.drawOval(28,69,43,10);
  /* marshmellow (cover in chocolate) */
  offMallomars_Graphics.setColor(chocolateBrown_Color);
  offMallomars_Graphics.fillArc(30,46,39,58,176,-172);
  offMallomars_Graphics.setColor(Color.black);
  offMallomars_Graphics.drawArc(30,46,39,58,176,-172);
  /* btn hilite (?) */
  if ((mallomarsMachineCnt >= 36) && (mallomarsMachineCnt < 48)) {
   offMallomars_Graphics.setColor(Color.black);
   offMallomars_Graphics.drawRect(21,36,57,49);
   offMallomars_Graphics.drawRect(22,37,55,47);
  } /* end "btn hilite (?)" */
  /* end: chocolate btn */

  /* end: draw make Mallomars cookies buttons in buffer */

  /* complete Mallomar (?) */
  if ((mallomarsMachineCnt >= 1) && (mallomarsMachineCnt < 12)) {
   /* graham cracker (already formed) */
   offMallomars_Graphics.setColor(chocolateBrown_Color);
   offMallomars_Graphics.fillOval(246,146,128,20);
   offMallomars_Graphics.setColor(Color.black);
   offMallomars_Graphics.drawOval(246,146,128,20);
   /* marshmellow (dollop already poured) */
   offMallomars_Graphics.setColor(chocolateBrown_Color);
   offMallomars_Graphics.fillArc(252,84,116,154,175,-170);
   offMallomars_Graphics.setColor(Color.black);
   offMallomars_Graphics.drawArc(252,84,116,154,175,-170);
  } /* end "complete Mallomar" */

  /* graham cracker animation (?) */
  else if ((mallomarsMachineCnt >= 12) && (mallomarsMachineCnt < 24)) {
   offMallomars_Graphics.setColor(grahamBeige_Color);
   offMallomars_Graphics.fillOval(246,grahamTopCoord,128,grahamHeight);
   offMallomars_Graphics.setColor(Color.black);
   offMallomars_Graphics.drawOval(246,grahamTopCoord,128,grahamHeight);
  } /* end "graham cracker animation" */

  /* marshmellow animation (?) */
  else if ((mallomarsMachineCnt >= 24) && (mallomarsMachineCnt < 36)) {
   /* graham cracker (already formed) */
   offMallomars_Graphics.setColor(grahamBeige_Color);
   offMallomars_Graphics.fillOval(246,146,128,20);
   offMallomars_Graphics.setColor(Color.black);
   offMallomars_Graphics.drawOval(246,146,128,20);
   /* marshmellow (dollop) */
   offMallomars_Graphics.setColor(marshmellowGray_Color);
   offMallomars_Graphics.fillArc ( 252,marshmellowTopCoord,
                                   116,marshmellowHeight,175,-170);
   offMallomars_Graphics.setColor(Color.black);
   offMallomars_Graphics.drawArc ( 252,marshmellowTopCoord,
                                   116,marshmellowHeight,175,-170);
   /* marshmellow (pouring) */
   if ((mallomarsMachineCnt % 2) == 0)
    pour_topCood = -10;
   else  /* "else if ((mallomarsMachineCnt % 2) == 1)" */
    pour_topCood = 14;
   while (pour_topCood < 144) {
    offMallomars_Graphics.setColor(marshmellowGray_Color);
    offMallomars_Graphics.fillOval(304,pour_topCood,12,24);
    offMallomars_Graphics.setColor(Color.black);
    offMallomars_Graphics.drawOval(304,pour_topCood,12,24);
    pour_topCood = pour_topCood + 48;
   } /* end "marshmellow (pouring)" */
  } /* end "marshmellow animation" */

  /* chocolate animation (?) */
  else if ((mallomarsMachineCnt >= 36) && (mallomarsMachineCnt < 48)) {
   /* graham cracker (already formed) */
   offMallomars_Graphics.setColor(grahamBeige_Color);
   offMallomars_Graphics.fillOval(246,146,128,20);
   offMallomars_Graphics.setColor(Color.black);
   offMallomars_Graphics.drawOval(246,146,128,20);
   /* marshmellow (dollop already poured) */
   offMallomars_Graphics.setColor(marshmellowGray_Color);
   offMallomars_Graphics.fillArc(252,84,116,154,175,-170);
   offMallomars_Graphics.setColor(Color.black);
   offMallomars_Graphics.drawArc(252,84,116,154,175,-170);
   /* chocolate (spread sauce) */
   offMallomars_Graphics.setColor(chocolateBrown_Color);
   offMallomars_Graphics.fillArc ( 252,84,116,154,
                                   chocolateStartAngle,chocolateArcAngle);
   offMallomars_Graphics.setColor(Color.black);
   offMallomars_Graphics.drawArc ( 252,84,116,154,
                                   chocolateStartAngle,chocolateArcAngle);
   /* chocolate (pouring) */
   if ((mallomarsMachineCnt % 2) == 0)
    pour_topCood = -10;
   else  /* "else if ((mallomarsMachineCnt % 2) == 1)" */
    pour_topCood = 14;
   while (pour_topCood < 144) {
    offMallomars_Graphics.setColor(chocolateBrown_Color);
    offMallomars_Graphics.fillOval(304,pour_topCood,12,24);
    offMallomars_Graphics.setColor(Color.black);
    offMallomars_Graphics.drawOval(304,pour_topCood,12,24);
    pour_topCood = pour_topCood + 48;
   } /* end "chocolate (pouring)" */
  } /* end "chocolate animation" */

  /* end: draw make Mallomars cookies buttons in buffer */

  /* blast "Mallomars" anim/image buffer to screen */
  g.drawImage(offMallomars_Image,0,0,this);

 } /* end method "paint" */

} /* end class "Mallomars" */

/* end file "Mallomars.java" */

[return]