// Control panel // // Author Matthew Caryl // Created 25.2.97 // // Although under copywrite to the author (Matthew Caryl) this code can be copied and modified for non-commercial // purposes as long as any derivatives contain this condition. package Bugs; import java.awt.Frame; import java.awt.Button; import java.awt.Panel; import java.awt.Choice; import java.awt.Label; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.Event; import java.lang.Thread; final class Control extends Frame implements Runnable { World owner; Bug bug; // only present for bug control Button reset; // only present for speed control Button cancel; Panel control; Panel display; Choice speed; // only present for speed control Label age; // only present for bug control Label energy; // only present for bug control Thread monitor;// only present for bug control public Control (World world) { super("Bug Control"); owner = world; // Add an cancel button to the base of the frame cancel = new Button("Cancel"); control = new Panel(); control.setLayout(new FlowLayout(FlowLayout.CENTER, 15, 15)); control.add(cancel); this.add("South", control); // Add an signature to the top of the frame Panel signiture = new Panel(); signiture.setLayout(new FlowLayout(FlowLayout.CENTER, 15, 15)); signiture.add(new Label("Matthew Caryl 1997")); this.add("North", signiture); // Add display to contain controls display = new Panel(); Panel align = new Panel(); align.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5)); align.add("Center", display); this.add("Center", align); } public Control(World W, int S) { this(W); // Reset button to restart simulation reset = new Button("Reset"); control.add(reset); speed = new Choice(); speed.addItem("Maximum"); speed.addItem("Medium"); speed.addItem("Minimum"); speed.select(S); display.setLayout(new GridLayout(1, 2, 5, 5)); // 1 rows, 2 columns, 5 horizontal, 5 verticle display.add(new Label("Speed")); display.add(speed); this.pack(); this.show(); } public Control(World W, Bug B) { this(W); bug = B; bug.select(); age = new Label(); energy = new Label(); // convert gene values into percentage probabilities int genes[] = new int[6]; int total = 0; for (int i = 5; i >= 0; i--) { genes[i] = 1 << bug.gene(i); total += genes[i]; } for (int i = 5; i >= 0; i--) genes[i] = Math.round((float) genes[i] / (float) total * 100f); display.setLayout(new GridLayout(4, 4, 5, 5)); // 4 rows, 4 columns, 5 horizontal, 5 verticle display.add(new Label("Age")); display.add(age); display.add(new Label("Energy")); display.add(energy); display.add(new Label("Forward")); display.add(new Label(Integer.toString(genes[0]) + "%")); display.add(new Label("Reverse")); display.add(new Label(Integer.toString(genes[3]) + "%")); display.add(new Label("Right")); display.add(new Label(Integer.toString(genes[1]) + "%")); display.add(new Label("Left")); display.add(new Label(Integer.toString(genes[5]) + "%")); display.add(new Label("Hard Right")); display.add(new Label(Integer.toString(genes[2]) + "%")); display.add(new Label("Hard Left")); display.add(new Label(Integer.toString(genes[4]) + "%")); // Thread to update bug information periodically monitor = new Thread(this); try { monitor.setPriority(Thread.MAX_PRIORITY); } catch (IllegalArgumentException e) { System.err.println("Report to programmer - control"); } monitor.start(); this.pack(); this.show(); } public void dispose() { if (monitor != null) stop(); if (bug != null) bug.unselect(); } public void stop() { monitor.stop(); monitor = null; } public void run() { while (true) { age.setText(Integer.toString(bug.age())); energy.setText(Integer.toString(bug.energy())); if (bug.dead()) stop(); try { monitor.sleep(1000); } catch (InterruptedException e) { // do nothing } } } public boolean action(Event e, Object arg) { if (e.target == reset|| e.target == cancel) { if (e.target == reset) owner.reset(); else if (speed != null) owner.cancel(); // cancel only required for speed control this.hide(); this.dispose(); return true; } else if (e.target == speed) { owner.speed(speed.getSelectedIndex()); return true; } else return false; } }