package com.caplet.manna; import java.applet.Applet; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.awt.event.MouseEvent; /** * The MannaMouse applet as a whole. * * @author Mark S. Miller, markm@caplet.com * @author Terry Stanley, tstanley@cocoon.com */ public class Demographics extends Applet implements ActionListener, MouseListener, MouseMotionListener { /*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)); myGo.addActionListener(this); 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; gas[i].addMouseListener(this); gas[i].addMouseMotionListener(this); } myEvolver = new GAPainter(gas); for (int i = 0; i < myDiversity; i++) { ((Culture)myPetri.getComponent(i)).ourEvolver = myEvolver; gas[i].ourEvolver = myEvolver; } add("Center", myPetri); } public void actionPerformed(ActionEvent e) { String cmd = e.getActionCommand(); if (cmd.equals(" go ")) { myGo.setLabel("stop"); myEvolver.go(); } else if (cmd.equals("stop")) { myGo.setLabel(" go "); myEvolver.pause(); } } public void mouseClicked(MouseEvent e) { } public void mousePressed(MouseEvent e) { myEvolver.addManna(e.getX(), e.getY(), GA.TOP_FITNESS); } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseDragged(MouseEvent e) { myEvolver.addManna(e.getX(), e.getY(), GA.TOP_FITNESS); } public void mouseMoved(MouseEvent e) { } }