package com.caplet.manna; import java.applet.Applet; import java.awt.*; /** * The MannaMouse applet as a whole. * * @author Mark S. Miller, markm@caplet.com * @author Terry Stanley, tstanley@cocoon.com */ public class Demographics extends Applet { /*package*/ GAPainter myEvolver; /*package*/ Panel myControls, myPetri; /*package*/ Button myGo; /*package*/ int myDiversity =2; /** * Creates and initializes all the parts */ public void init() { setLayout(new BorderLayout()); setForeground(Color.black); setBackground(new Color(204, 204, 204)); Panel myControls = new Panel(); myControls.setLayout(new FlowLayout(FlowLayout.CENTER)); myGo = new Button(" go "); // cannot get around LayoutManager on this one myGo.setForeground(new Color(0, 0, 51)); // blue-black myGo.setBackground(Color.white); myGo.setFont(new Font("TimesRoman", Font.BOLD, 14)); myControls.add(myGo); add("North", myControls); myPetri = new Panel(); myPetri.setLayout(new FlowLayout(FlowLayout.CENTER)); GA[] gas = new GA[myDiversity]; for (int i = 0; i < myDiversity; i++) { Culture culture = new Culture(); myPetri.add(culture); gas[i] = culture.myGA; } myEvolver = new GAPainter(gas); for (int i = 0; i < myDiversity; i++) { ((Culture)myPetri.getComponent(i)).ourEvolver = myEvolver; gas[i].ourEvolver = myEvolver; } add("Center", myPetri); } /** * Dispatches on an event in the horrible way required by AWT 1.0x */ public boolean handleEvent(Event event) { switch (event.id) { case Event.ACTION_EVENT: if (event.target == myGo) { if (((String)event.arg).equals(" go ")) { myGo.setLabel("stop"); myEvolver.go(); } else if (((String)event.arg).equals("stop")) { myGo.setLabel(" go "); myEvolver.pause(); } return true; } break; case Event.MOUSE_DOWN: case Event.MOUSE_DRAG: Component nested; // nx, ny myPetri local coordinates int nx = event.x - myPetri.location().x; int ny = event.y - myPetri.location().y; if((nested = myPetri.locate(nx, ny)) != null) { // cx, cy culture local coordinates int cx = nx - nested.location().x; int cy = ny - nested.location().y; Component moreNested; if ((moreNested = nested.locate(cx,cy)) != null && (moreNested instanceof GA)) { int gx = cx - moreNested.location().x; //ga local int gy = cy - moreNested.location().y; myEvolver.addManna(gx, gy, GA.TOP_FITNESS); return true; } } break; default: break; } return false; } }