package com.caplet.manna; import java.awt.*; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; /** * 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 implements ItemListener { 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.addItemListener(this); popSize.addItem("512"); popSize.addItem("1024"); popSize.addItem("2048"); popSize.select("1024"); north.add(popSize); north.add(new Label("mutants:")); mutants = new Choice(); mutants.addItemListener(this); 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); gray.addItemListener(this); Checkbox uniform = new Checkbox("uniform", null, true); uniform.addItemListener(this); Checkbox local = new Checkbox("local", null, false); local.addItemListener(this); Checkbox consumed = new Checkbox("consumed", null, true); consumed.addItemListener(this); gray.setEnabled(false); local.setEnabled(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); } public void itemStateChanged(ItemEvent e) { int state = e.getStateChange(); ItemSelectable item = e.getItemSelectable(); if (item instanceof Checkbox) { boolean value = (state == ItemEvent.SELECTED) ? true : false; String who = ((Checkbox)item).getLabel(); synchronized (ourEvolver) { if (who.indexOf("gray") != -1) { myGA.amGray = value; } else if (who.indexOf("uniform") != -1) { myGA.amUniform = value; } else if (who.indexOf("local") != -1) { myGA.amLocal = value; } else if (who.indexOf("consumed") != -1) { myGA.amConsumed = value; } } } else if (item instanceof Choice && state == ItemEvent.SELECTED) { String who = ((Choice)item).getSelectedItem(); float newMutationRate = (float)0.0; int newPopulationSize = myGA.popSize(); if (who.equals("0%")) { newMutationRate = (float)0.0; } else if (who.equals("2%")) { newMutationRate = (float)0.02; } else if (who.equals("5%")) { newMutationRate = (float)0.05; } else if (who.equals("512")) { newPopulationSize = 512; } else if (who.equals("1024")) { newPopulationSize = 1024; } else if (who.equals("2048")) { newPopulationSize = 2048; } synchronized (ourEvolver) { myGA.myMutationRate = newMutationRate; if (newPopulationSize != myGA.popSize()) { myGA.setPopSize(newPopulationSize); myGA.update(myGA.getGraphics()); // clear, then paint } } } } }