// The Blues // // Control panel for reproduction weights and applet size. // // Author Matthew Caryl // Created 2.5.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 Blues; import java.awt.Button; import java.awt.Choice; import java.awt.Frame; import java.awt.Panel; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.Label; import java.awt.Event; import java.lang.String; final class ControlFrame extends Frame { World owningWorld; Button ok; Button cancel; Choice generations; Choice population; Choice mutation; Choice crossover; Choice black; Choice purple; Choice red; Choice blue; Choice block; public ControlFrame (World world) { super("Blues Control"); owningWorld = world; // Add an OK button to the base of the frame cancel = new Button("Cancel"); ok = new Button("OK"); Panel okPanel = new Panel(); okPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 15, 15)); okPanel.add(cancel); okPanel.add(ok); this.add("South", okPanel); // Add an signature to the top of the frame Panel sigPanel = new Panel(); sigPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 15, 15)); sigPanel.add(new Label("Matthew Caryl 1996")); this.add("North", sigPanel); // Add option menu generations starting with current value generations = new Choice(); generations.addItem(Integer.toString(owningWorld.generations())); generations.addItem("---"); for (int i = 32; i <= 512; i *= 2) generations.addItem(Integer.toString(i)); // Add option menu population starting with current value population = new Choice(); population.addItem(Integer.toString(owningWorld.population())); population.addItem("---"); for (int i = 8; i <= 128; i *= 2) population.addItem(Integer.toString(i)); // Add option menu mutation starting with current value mutation = new Choice(); mutation.addItem(Integer.toString(owningWorld.mutation())); mutation.addItem("---"); for (int i = 0; i <= 100; i += 5) mutation.addItem(Integer.toString(i)); // Add option menu crossover starting with current value crossover = new Choice(); crossover.addItem(Integer.toString(owningWorld.crossover())); crossover.addItem("---"); for (int i = 0; i <= 100; i += 5) crossover.addItem(Integer.toString(i)); // Add option menu black starting with current value black = new Choice(); black.addItem(Integer.toString(owningWorld.blackEvaluation())); black.addItem("---"); for (int i = 1; i <= 10; i += 1) black.addItem(Integer.toString(i)); // Add option menu purple starting with current value purple = new Choice(); purple.addItem(Integer.toString(owningWorld.purpleEvaluation())); purple.addItem("---"); for (int i = 1; i <= 10; i += 1) purple.addItem(Integer.toString(i)); // Add option menu red starting with current value red = new Choice(); red.addItem(Integer.toString(owningWorld.redEvaluation())); red.addItem("---"); for (int i = 1; i <= 10; i += 1) red.addItem(Integer.toString(i)); // Add option menu purple starting with current value blue = new Choice(); blue.addItem(Integer.toString(owningWorld.blueEvaluation())); blue.addItem("---"); for (int i = 1; i <= 10; i += 1) blue.addItem(Integer.toString(i)); // Add option menu for block size starting with current value block = new Choice(); block.addItem(Integer.toString(owningWorld.blockSize())); block.addItem("---"); for (int i = 1; i < 10; i++) block.addItem(Integer.toString(i)); // Add a grid to contain all the controls Panel controlPanel = new Panel(); controlPanel.setLayout(new GridLayout(9, 2, 0, 5)); // 8 rows, 2 columns, 0 horizontal, 5 verticle controlPanel.add(new Label("Number generations:")); controlPanel.add(generations); controlPanel.add(new Label("Population size:")); controlPanel.add(population); controlPanel.add(new Label("Mutation percentage:")); controlPanel.add(mutation); controlPanel.add(new Label("Crossover percentage:")); controlPanel.add(crossover); controlPanel.add(new Label("Black evaluation:")); controlPanel.add(black); controlPanel.add(new Label("Purple evaluation:")); controlPanel.add(purple); controlPanel.add(new Label("Red evaluation:")); controlPanel.add(red); controlPanel.add(new Label("Blue evaluation:")); controlPanel.add(blue); controlPanel.add(new Label("Block size:")); controlPanel.add(block); Panel alignPanel = new Panel(); alignPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5)); alignPanel.add("Center", controlPanel); this.add("Center", alignPanel); // Resize the window and display this.pack(); this.show(); } // Deal with button presses public boolean action(Event e, Object arg) { if (e.target == ok || e.target == cancel) { // capture new control values for ok if (e.target == ok) { int generationNumber; int populationSize; int mutationPercentage; int crossoverPercentage; int blackEvaluation; int purpleEvaluation; int redEvaluation; int blueEvaluation; int blockSize; // Capture all menu choice values or use defaults generationNumber = parseChoiceNumber(generations, owningWorld.generations()); populationSize = parseChoiceNumber(population, owningWorld.population()); mutationPercentage = parseChoiceNumber(mutation, owningWorld.mutation()); crossoverPercentage = parseChoiceNumber(crossover, owningWorld.crossover()); blackEvaluation = parseChoiceNumber(black, owningWorld.blackEvaluation()); purpleEvaluation = parseChoiceNumber(purple, owningWorld.purpleEvaluation()); redEvaluation = parseChoiceNumber(red, owningWorld.redEvaluation()); blueEvaluation = parseChoiceNumber(blue, owningWorld.blueEvaluation()); blockSize = parseChoiceNumber(block, owningWorld.blockSize()); // Reinitialise world with captured values owningWorld.initialiseWorld(generationNumber, populationSize, mutationPercentage, crossoverPercentage, blackEvaluation, purpleEvaluation, redEvaluation, blueEvaluation, blockSize); } // restart world and dispose of controls owningWorld.start(); owningWorld.cancel(); this.hide(); this.dispose(); return true; } else return false; } protected String paramString() { return "The Blues by Matthew Caryl 1996"; } // Capture value from choice menu or use default private int parseChoiceNumber(Choice c, int d) { try { return Integer.parseInt(c.getSelectedItem()); } catch (NumberFormatException e) { return d; } } }