/*----------------------------------------------------------------------+ | Title: Moth.java | | | | Author: David E. Joyce | | Department of Mathematics and Computer Science | | Clark University | | Worcester, MA 01610-1477 | | U.S.A. | | | http://aleph0.clarku.edu/~djoyce/home.html | | djoyce@clarku.edu | | | | Date: February, 2001. | +----------------------------------------------------------------------*/ import java.awt.*; import java.applet.Applet; import java.awt.event.*; import java.util.StringTokenizer; public class Moth extends Applet implements ActionListener, AdjustmentListener { // default parameters determined by html page int benefit; // benefit from altruist, in 10ths of a unit int cost; // cost to altruist, in 10ths of a unit int herd_size; // each herd has this population int init_alt; // how many initial altruists int n_herds; // the number of herds int deaths; // this many die and are born each day in each herd int max_days; // how many days to run the simulation int band_size; // each migrating band has this size int migration_period; // how many days between migrations int timeout; // milliseconds between days Color background; // background color for applet Range R = new Range(this); // the range where the herds of animals live RangeDisplay RD; // the panel to display the range // Panels Panel controlPanel = new Panel(); CardLayout controlManager = new CardLayout(); Panel startControlPanel = new Panel(); Panel runControlPanel = new Panel(); Panel statisticsPanel = new Panel(); // Start Control Panel components Button startButton = new Button("Start"); Button resetButton = new Button("Reset"); Label n_herds_label = new Label(); Scrollbar n_herds_scrollbar; Label deaths_label = new Label(); Scrollbar deaths_scrollbar; Label herd_size_label = new Label(); Scrollbar herd_size_scrollbar; Label init_alt_label = new Label(); Scrollbar init_alt_scrollbar; Label cost_label = new Label(); Scrollbar cost_scrollbar; Label benefit_label = new Label(); Scrollbar benefit_scrollbar; Label band_size_label = new Label(); Scrollbar band_size_scrollbar; Label migration_period_label = new Label(); Scrollbar migration_period_scrollbar; // Run Control Panel components Button pauseButton = new Button("Pause"); Button restartButton = new Button("Restart"); Button stopButton = new Button("Stop"); Label cost_label2 = new Label(); Scrollbar cost_scrollbar2; Label benefit_label2 = new Label(); Scrollbar benefit_scrollbar2; Label deaths_label2 = new Label(); Scrollbar deaths_scrollbar2; Label band_size_label2 = new Label(); Scrollbar band_size_scrollbar2; Label migration_period_label2 = new Label(); Scrollbar migration_period_scrollbar2; Label timeout_label = new Label(); Scrollbar timeout_scrollbar; // Statistics Panel components TimeGraph indivStats; TimeGraph totStats; public String getAppletInfo() { return "Velcro. Copyright 2001, David Joyce, Clark University. Version 1.0"; } public String[][] getParameterInfo() { String[][] pinfo = { {"cost", "int","cost to altruist in 10ths of a unit"}, {"benefit", "int","benefit from altruist in 10ts of a unit"}, {"deaths", "int","deaths per day in each herd"}, {"herd_size", "int","each herd has this population"}, {"init_alt", "int","how many initial altruists"}, {"n_herds", "int","the number of herds"}, {"deaths", "int","this many die and are born each day in each herd"}, {"max_days", "int","how many days to run the simulation"}, {"band_size", "int","each migrating band has this size"}, {"migration_period","int","how many days between migrations"}, {"timeout", "int","milliseconds between days"}, {"background", "color","applet background color"}, }; return pinfo; } static String colorName[] = { "black", "blue", "cyan", "darkGray", "gray", "green","lightGray", "magenta", "orange", "pink", "red", "white", "yellow"}; static Color constColor[] = { Color.black, Color.blue, Color.cyan, Color.darkGray, Color.gray, Color.green, Color.lightGray, Color.magenta, Color.orange, Color.pink, Color.red, Color.white, Color.yellow}; Color parseColor (String str) { if (str==null || "none".equals(str)) return null; for (int i=0; i R.herd_size) R.init_alt = R.herd_size; if (R.band_size > R.herd_size/3) R.band_size = R.herd_size/3; resetScrollBars(); } else if (e.getSource() == init_alt_scrollbar) { R.init_alt = e.getValue(); R.resetHerds(); resetScrollBars(); } else if (e.getSource() == band_size_scrollbar || e.getSource() == band_size_scrollbar2) { R.band_size = e.getValue(); if (e.getSource() == band_size_scrollbar) R.resetHerds(); resetScrollBars(); } else if (e.getSource() == migration_period_scrollbar || e.getSource() == migration_period_scrollbar2) { R.migration_period = e.getValue(); if (e.getSource() == migration_period_scrollbar) R.resetHerds(); resetScrollBars(); } else if (e.getSource() == cost_scrollbar || e.getSource() == cost_scrollbar2) { R.cost = e.getValue(); resetScrollBars(); } else if (e.getSource() == benefit_scrollbar || e.getSource() == benefit_scrollbar2) { R.benefit = e.getValue(); resetScrollBars(); } else if (e.getSource() == timeout_scrollbar) { R.timeout = e.getValue(); resetScrollBars(); } } void resetScrollBars () { n_herds_label.setText(R.n_herds+" herds"); n_herds_scrollbar.setValue(R.n_herds); herd_size_label.setText("herd size: "+R.herd_size); herd_size_scrollbar.setValue(R.herd_size); deaths_label.setText("death rate: "+R.deaths); deaths_scrollbar.setValue(R.deaths); deaths_scrollbar.setMaximum(R.herd_size/2); deaths_label2.setText("death rate: "+R.deaths); deaths_scrollbar2.setValue(R.deaths); deaths_scrollbar2.setMaximum(R.herd_size/2); init_alt_label.setText("altruists: "+R.init_alt); init_alt_scrollbar.setValue(R.init_alt); init_alt_scrollbar.setMaximum(R.herd_size+1); band_size_label.setText("band size: "+R.band_size); band_size_label2.setText("band size: "+R.band_size); band_size_scrollbar.setValue(R.band_size); band_size_scrollbar.setMaximum(R.herd_size/3); band_size_scrollbar2.setValue(R.band_size); band_size_scrollbar2.setMaximum(R.herd_size/3); migration_period_label.setText("migration period: "+R.migration_period); migration_period_scrollbar.setValue(R.migration_period); migration_period_label2.setText("migration period: "+R.migration_period); migration_period_scrollbar2.setValue(R.migration_period); cost_label.setText("cost: "+R.cost/10.0); cost_scrollbar.setValue(R.cost); cost_label2.setText("cost: "+R.cost/10.0); cost_scrollbar2.setValue(R.cost); benefit_label.setText("benefit: "+R.benefit/10.0); benefit_scrollbar.setValue(R.benefit); benefit_label2.setText("benefit: "+R.benefit/10.0); benefit_scrollbar2.setValue(R.benefit); timeout_label.setText("timeout: "+R.timeout/1000.0); timeout_scrollbar.setValue(R.timeout); repaint(); } }