package com.caplet.manna; import java.awt.*; /** * Each side of the Manna Mouse screen is a separately growing culture dish. * A Culture includes a GA, a visualisation of the GA: a GAPainter, and the * various GUI controls specific to each GA. * * @author Mark S. Miller, markm@caplet.com * @author Terry Stanley, tstanley@cocoon.com */ public class Culture extends Panel { private Panel myControls; /*package*/ GA myGA; /*package*/ GAPainter ourEvolver = null; /** * Makes a new Culture with the default settings */ public Culture() { myGA = new GA(); myControls = new Panel(); myControls.setForeground(new Color(0, 0, 51)); // blue-black myControls.setBackground(Color.white); myControls.setFont(new Font("Times Roman", Font.BOLD, 9)); myControls.setLayout(new BorderLayout()); Panel north, center; Choice popSize, mutants; north = new Panel(); north.setForeground(new Color(0, 0, 51)); // blue-black north.setBackground(Color.white); myControls.setFont(new Font("Times Roman", Font.BOLD, 9)); north.setLayout(new FlowLayout(FlowLayout.CENTER)); north.add(new Label("population:")); popSize = new Choice(); popSize.addItem("512"); popSize.addItem("1024"); popSize.addItem("2048"); popSize.select("1024"); north.add(popSize); north.add(new Label("mutants:")); mutants = new Choice(); mutants.addItem("0%"); mutants.addItem("2%"); mutants.addItem("5%"); mutants.select("2%"); north.add(mutants); myControls.add("North", north); center = new Panel(); center.setForeground(new Color(0, 0, 51)); // blue-black center.setBackground(Color.white); myControls.setFont(new Font("Times Roman", Font.BOLD, 9)); center.setLayout(new FlowLayout(FlowLayout.CENTER)); Checkbox gray = new Checkbox("gray", null, false); Checkbox uniform = new Checkbox("uniform", null, true); Checkbox local = new Checkbox("local", null, false); Checkbox consumed = new Checkbox("consumed", null, true); //"enabled(boolean)" is deprecated in 1.1, but no alternative exists //in the horrible AWT 1.0x. gray.enable(false); local.enable(false); center.add(gray); center.add(uniform); center.add(local); center.add(consumed); myControls.add("Center", center); setLayout(new BorderLayout()); add("South", myControls); add("Center", myGA); } /** * This dispatches on a UI event in the horrible way appropriate to * AWT 1.0x. * * @deprecated but used until we can stop supporting Java 1.0x */ public boolean action(Event event, Object arg) { if (event.target instanceof Choice) { float newMutants; int newPopSize; if (event.arg == "0%") newMutants = (float)0.0; else if (event.arg == "2%") newMutants = (float)0.02; else if (event.arg == "5%") newMutants = (float)0.05; else { if (event.arg == "512") newPopSize = 512; else if (event.arg == "1024") newPopSize = 1024; else if (event.arg == "2048") newPopSize = 2048; else return false; synchronized(ourEvolver) { if (newPopSize != myGA.popSize()) { myGA.setPopSize(newPopSize); myGA.update(myGA.getGraphics()); // clear, then paint } return true; } } synchronized (ourEvolver) { myGA.myMutationRate = newMutants; return true; } } else if (event.target instanceof Checkbox) { boolean value = ((Boolean)event.arg).booleanValue(); synchronized (ourEvolver) { if (((Checkbox)event.target).getLabel() == "gray") { myGA.amGray = value; return true; } else if (((Checkbox)event.target).getLabel() == "uniform") { myGA.amUniform = value; return true; } else if (((Checkbox)event.target).getLabel() == "consumed") { myGA.amConsumed = value; return true; } else if (((Checkbox)event.target).getLabel() == "local") { myGA.amLocal = value; return true; } } } return false; } }