import java.awt.*; import Individual; import Executor; import GenerationsContainer; import ExceptionDialog; import DialogLayout; public class GenerationsGeneratorDialog extends CloseableDialog { boolean init = false; DialogLayout panelLayout; // Control definitions //-------------------------------------------------------------------------- Button OkayButton; Button CancelButton; Label MutationLabel1; TextField MutationField; Label CrossoverLabel; TextField CrossoverField; Label GenSizeLabel; TextField GenSizeField; GenerationsGenerator gg; Frame parent; // Constructor //-------------------------------------------------------------------------- public GenerationsGeneratorDialog(Frame parent, GenerationsGenerator gg) { super(parent,"Genetic Algorithm Settings",true); this.parent=parent; setGenerationsGenerator(gg); CreateControls(); } public GenerationsGeneratorDialog (Frame parent, Executor exec) { this(parent, exec.getGenerationsGenerator()); } public void setGenerationsGenerator(GenerationsGenerator gg) { System.out.println("Reached setGenerationsGenerator"); this.gg=gg; } public void setExecutor(Executor exec) { setGenerationsGenerator(exec.getGenerationsGenerator()); } // Initialization. //-------------------------------------------------------------------------- public boolean CreateControls() { // CreateControls should be called only once //---------------------------------------------------------------------- if (init) return false; // Since a given font may not be supported across all platforms, it // is safe to modify only the size of the font, not the typeface. //---------------------------------------------------------------------- setResizable(true); Font OldFnt = this.getFont(); if (OldFnt != null) { Font NewFnt = new Font(OldFnt.getName(), OldFnt.getStyle(), 8); this.setFont(NewFnt); } // All position and sizes are in dialog logical units, so we use a // DialogLayout as our layout manager. //---------------------------------------------------------------------- panelLayout = new DialogLayout(this, 270, 107); this.setLayout(panelLayout); this.addNotify(); Dimension size = panelLayout.getDialogSize(); Insets insets = this.insets(); this.resize(insets.left + size.width + insets.right, insets.top + size.height + insets.bottom); // Control creation //---------------------------------------------------------------------- OkayButton = new Button ("Okay"); this.add(OkayButton); panelLayout.setShape(OkayButton, 74, 86, 50, 14); CancelButton = new Button ("Cancel"); this.add(CancelButton); panelLayout.setShape(CancelButton, 146, 86, 50, 14); MutationLabel1 = new Label ("Mutation Rate", Label.LEFT); this.add(MutationLabel1); panelLayout.setShape(MutationLabel1, 7, 19, 52, 11); MutationField = new TextField (""); this.add(MutationField); panelLayout.setShape(MutationField, 63, 16, 61, 14); CrossoverLabel = new Label ("Crossover Rate", Label.LEFT); this.add(CrossoverLabel); panelLayout.setShape(CrossoverLabel, 7, 42, 52, 11); CrossoverField = new TextField (""); this.add(CrossoverField); panelLayout.setShape(CrossoverField, 63, 39, 61, 14); GenSizeLabel = new Label ("Generation Size", Label.LEFT); this.add(GenSizeLabel); panelLayout.setShape(GenSizeLabel, 146, 19, 52, 11); GenSizeField = new TextField (""); this.add(GenSizeField); panelLayout.setShape(GenSizeField, 202, 16, 61, 14); //this.pack(); setResizable(false); setFields(); init = true; return true; } protected void setFields() { GenSizeField.setText(String.valueOf(gg.getGenSize())); CrossoverField.setText(String.valueOf(gg.getCrossoverProbability())); MutationField.setText(String.valueOf(gg.getMutationProbability())); } protected void loadFieldsToExec() throws NumberFormatException { int valueOfGenSize=(Integer.valueOf(GenSizeField.getText())).intValue(); float valueOfMutation=(Float.valueOf(MutationField.getText())).floatValue(); float valueOfCrossover=(Float.valueOf(CrossoverField.getText())).floatValue(); if (valueOfGenSize <= 0) throw new NumberFormatException("Generation size must be greater than 0"); if (valueOfMutation<0||valueOfMutation>1) throw new NumberFormatException("Mutation probability must be between 0 and 1 (inclusive)"); if (valueOfCrossover<0||valueOfCrossover>1) throw new NumberFormatException("Crossover probability must be between 0 and 1 (inclusive)"); gg.setMutationProbability(valueOfMutation); gg.setCrossoverProbability(valueOfCrossover); gg.setGenSize(valueOfGenSize); } public boolean action(Event e, Object arg) { if (e.target==OkayButton) { try { loadFieldsToExec(); hide(); dispose(); } catch (NumberFormatException exception) { ExceptionDialog eDialog = new ExceptionDialog(parent,exception); eDialog.show(); } return true; } else if (e.target==CancelButton) { hide(); dispose(); return true; } return super.action(e, arg); } }