// Control panel for Artificial Termites. // // Author Matthew Caryl // Created 29.10.96 // // 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 ArtificialTermites; import java.awt.*; final class ControlFrame extends Frame { World owningWorld; Button ok; Button cancel; Choice wood; Choice termite; Choice width; Choice height; Choice block; public ControlFrame (World world) { super("Termite 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 for wood percentages starting with current value wood = new Choice(); wood.addItem(Integer.toString(owningWorld.woodPercentage())); wood.addItem("---"); for (int i = 5; i < 50; i += 5) wood.addItem(Integer.toString(i)); // Add option menu for termite percentages starting with current value termite = new Choice(); termite.addItem(Integer.toString(owningWorld.termitePercentage())); termite.addItem("---"); for (int i = 1; i < 10; i++) termite.addItem(Integer.toString(i)); // Add option menu for world width starting with current value width = new Choice(); width.addItem(Integer.toString(owningWorld.width())); width.addItem("---"); for (int i = 1; i < 1024; i *= 2) width.addItem(Integer.toString(i)); // Add option menu for world height starting with current value height = new Choice(); height.addItem(Integer.toString(owningWorld.height())); height.addItem("---"); for (int i = 1; i < 1024; i *= 2) height.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(5, 2, 0, 5)); // 5 rows, 2 columns, 0 horizontal, 5 verticle controlPanel.add(new Label("Wood percentage:")); controlPanel.add(wood); controlPanel.add(new Label("Termite percentage:")); controlPanel.add(termite); controlPanel.add(new Label("World width:")); controlPanel.add(width); controlPanel.add(new Label("World height:")); controlPanel.add(height); 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(); } 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 woodPercentage; int termitePercentage; int worldWidth; int worldHeight; int blockSize; try { woodPercentage = Integer.parseInt(wood.getSelectedItem()); } catch (NumberFormatException e2) { woodPercentage = owningWorld.woodPercentage(); } try { termitePercentage = Integer.parseInt(termite.getSelectedItem()); } catch (NumberFormatException e2) { termitePercentage = owningWorld.termitePercentage(); } try { worldWidth = Integer.parseInt(width.getSelectedItem()); } catch (NumberFormatException e2) { worldWidth = owningWorld.width(); } try { worldHeight = Integer.parseInt(height.getSelectedItem()); } catch (NumberFormatException e2) { worldHeight = owningWorld.height(); } try { blockSize = Integer.parseInt(block.getSelectedItem()); } catch (NumberFormatException e2) { blockSize = owningWorld.blockSize(); } owningWorld.initialiseWorld(worldWidth, worldHeight, blockSize, woodPercentage, termitePercentage); } // restart world and dispose of controls owningWorld.start(); this.hide(); this.dispose(); return true; } else return false; } }