

import java.awt.*;
import java.util.*;
import java.awt.event.*;

 class World extends Frame implements WindowListener, MouseListener

   {

     private static World x_world;
     private static final int STARTWIDTH = 600;  // Initial size of
     private static final int STARTHEIGHT = 500; //  window.
     private static int width;      //  current width of frame (pixels)
     private static int height;     //  current height of frame (pixels)
     private static Population pop; // Contains the array of creatures.
                                    // Manages creatures, and their
                                    // interactions with world and each
                                    // other.
     private static int t=0;        // Counter for number of time steps.
     private static Graphics g;
     private static boolean end;    //  Not really used yet.

     public static Random rand_generator = new Random();

     public static int north, east, south, west;  //  Coordinates
         // of the sides of the "active area" in the world, in pixels.
         //  "north" is the y coordinate of the north wall,
         //  "east" is the x coordinate of the east wall, etc.
         //  Note that north and west are NOT changed by a user
         //  resizing the window, while east and south DO change.
         //  Creatures venturing, or cuaght, outside the world
         //  should make every effort to return!

     public static void main(String [] arguments) throws Exception
        {

          x_world = new World();
          end = false;
          while(!end)
            {
              Thread.sleep(20);   //  Pause between screen updates
              t++;                //  Time moves ahead
              pop.advance_time(t); //  Have pop move creatures, etc.
              x_world.paint(g);    // Re-paint everything.
            }
        }



     public World()
        {
           super("  My Creature World  ");  //  Create and name window
           setSize(STARTWIDTH,STARTHEIGHT); //  Set initial size
           setBackground(Color.green.brighter()); // Initial back. color
           setVisible(true);                //  Display it!

           addWindowListener(this); //  Tell Java runner that this frame
           addMouseListener(this);  //   obj will handle Window events
                                    //   and mouse events, so give them
                                    //   to us!

           pop = new Population();  //  Init population
           t= 0;                      //  Start timer at 0.
           g = getGraphics();

        }

     public void paint(Graphics g)
        {
//         width = getWidth();   //  Window might have re-sized.  So get
//          height = getHeight();  //  latest dimensions.
          width = getSize().width;
          height = getSize().height;
          west = 20;             //  West and north sides never move
          north = 50;
          south = height -40;   //  South and east sides change as
          east = width - 60;    // window is re-sized.

          g.clearRect(west-5,north-5,width+5,height+5);  // Erase the
                                                         // entire active
                                                         //  area
          g.drawRect(west,north,east-west,south-north);  //  Draw borders

          pop.paint(g);              //  Draw creatures

          pop.drawStats(g,t,west+10, north-10);          //  Draw the basic pop statistics


         }


  //  Methods needed to implement the "WindowListener" interface

     public void windowOpened(WindowEvent win_ev)  {}
     public void windowClosing(WindowEvent win_ev)  // Catches click on
                 {                                  //  the window's "X"
                  dispose();
                  }
     public void windowClosed(WindowEvent win_ev)   {}
     public void windowActivated(WindowEvent win_ev)  {}
     public void windowDeactivated(WindowEvent win_ev)  {}
     public void windowIconified(WindowEvent win_ev)  {}
     public void windowDeiconified(WindowEvent win_ev)  {}

  //  Methods needed to implement the "MouseListener" interface

     public void mouseClicked(MouseEvent me)
                  {
                    System.out.println("mouse click at " +
                                       me.getX() + ", " + me.getY());
                    }
     public void mouseEntered(MouseEvent me)  {}
     public void mouseExited(MouseEvent me)  {}
     public void mousePressed(MouseEvent me)  {}
     public void mouseReleased(MouseEvent me)  {}

   }