import java.awt.*;

import java.awt.event.*;

/*

   File: JabbApp.java

   Author: Kenneth L Moore 2/8/01

   Purpose: Implement instantiations of a Jabberwocky. 

   Notes: Must have Jabberwocky.java in the same directory to compile.

   Relies on Java clean up to release instantiated Jabberwockies.

   Cycles color and sex of Jabberwockies instantiated.

   Must implement ActionListener to get mouse clicks on buttons.

   (implement circumvents single inheritence)

*/

 

public class JabbApp extends java.applet.Applet implements ActionListener

{

 

  // Applet wide variables.

  Button b1, b2, b3;

  boolean male = true; // set first Jabberwocky to male then cycle

  int currentColor = 0; // set color to first color

  Jabberwocky myMonster; // this will be the instance of a Jabberwocky

  Color myPurple = new Color(165, 110, 255); // define purple

  Color myBlue   = new Color(  0, 220, 255); // define a light blue

  Font f2;

           

  /*

  This method is a built in part of all Applets.  It is called by the

  browser when the Applet starts.  It allows the programmer to initialize

  variables/graphic elements.

 */

  public void init()

  {

           

    // button font

    f2 = new Font("Helvetica",Font.BOLD,12);

                       

    // instantiate (create) button objects

    b1 = new Button("FEED Jabberwocky");

    b2 = new Button("Create Jabberwocky");

    b3 = new Button("Display Jabberwocky");

                       

    // set initial button colors

    b1.setBackground(Color.red);

    b1.setForeground(Color.white);

    b2.setBackground(Color.yellow);

    b3.setBackground(Color.green);

                       

    // prepare to respond to mouse clicks

    b1.addActionListener(this);

    b2.addActionListener(this);

    b3.addActionListener(this);

    b1.setFont(f2);

    b2.setFont(f2);

    b3.setFont(f2);

                       

    // put the buttons in the window using default placement

    add(b1);

    add(b2);

    add(b3);

                       

    // instantiate (create an object) the first Jabberwocky

    myMonster = new Jabberwocky();

                       

    // override default window size

    resize(450,200);

    }

 

    /*

    This method is called by the browser in response to the need for

    refreshing the window.  For example, when the window is resized.

    It is also called when the programmer asks for a repaint().

    This method should NEVER be called directly by the programmer.

    This method is a built in part of all Applets.

    */

    public void paint(Graphics g)

   {

     myMonster.displayAttr(g);

    }

           

    /*

   This method is the button response method.  It must be used when

    a button is put into an Applet.  If it is not implemented, the

    compiler say that the Applet is virtual (beyond scope of discussion)

    */

  public void actionPerformed(ActionEvent e)

  {

           

    // see what button pressed by title

    if (e.getActionCommand().equals("FEED Jabberwocky")){

 

      // do feeding

      myMonster.setHungry(!(myMonster.getHungry()));

      myMonster.setDisplayFlag(false);

      myMonster.setFeedFlag(true);

      setBackground(Color.red);

      }

    else if (e.getActionCommand().equals("Create Jabberwocky")){

                       

        // create new Jabberwocky - old Jabberwocky cleaned by

        // Java memory management.

        myMonster = new Jabberwocky();

        myMonster.setDisplayFlag(true);

        myMonster.setFeedFlag(false);

 

        // toggle sex on each creation

        if (male){

            myMonster.setSex("Male");

            male = false;

            }

        else {

             myMonster.setSex("Female");

             male = true;

            }

                                               

        // cycle through all colors 

        if(currentColor == 0){

            myMonster.setColor("gray");

            setBackground(Color.gray);

            b2.setBackground(Color.gray);

            }

        else if (currentColor == 1){

            myMonster.setColor("pink");

            setBackground(Color.pink);

            b2.setBackground(Color.pink);

            }

        else if (currentColor == 2){

            myMonster.setColor("orange");

            setBackground(Color.orange);

            b2.setBackground(Color.orange);

             }

         else if (currentColor == 3){

            myMonster.setColor("Purple");

            setBackground(myPurple);

            b2.setBackground(myPurple);

            }

         else if (currentColor == 4){

            myMonster.setColor("Blue");

            setBackground(myBlue);

            b2.setBackground(myBlue);

            }

         currentColor++;

         if(currentColor > 4) currentColor = 0;

      }

   else if (e.getActionCommand().equals("Display Jabberwocky")){

                       

       // do display

       myMonster.setDisplayFlag(true);

       myMonster.setFeedFlag(false);

       setBackground(Color.green);

    }

                       

     // for all button clicks.

     repaint(); // have browser call paint

 

  }

}