//------------------------------------------------------------------------------ // RunInformationDialog.java: // Implementation of "control creator" class RunInformationDialog //------------------------------------------------------------------------------ import java.awt.*; import java.util.Observer; import java.util.Observable; import Executor; import BooleanIndividual; import Grapher; import GenerationsContainerGraph; import HistoryViewer; public class RunInformationDialog extends CloseableFrame implements Observer { Panel p; boolean init = false; DialogLayout panelLayout; Thread runner=null; Executor exec; // Control definitions //-------------------------------------------------------------------------- Button runButton; Button resetButton; Button configureButton; Label genSizeLabel; Label genSizeField; Label currGenLabel; Label currGenField; Label crossoverLabel; Label crossoverField; Label mutationLabel; Label mutationField; Grapher grapher; Renderer renderer; // Constructor //-------------------------------------------------------------------------- public RunInformationDialog (Executor exec) { super("Genetic Algorithms"); this.exec=exec; CreateControls(); } // 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. //---------------------------------------------------------------------- setLayout(new BorderLayout(0,0)); p=new Panel(); add("Center",p); this.show(); //maybe that will create the panel this.hide(); Font OldFnt = p.getFont(); if (OldFnt != null) { Font NewFnt = new Font(OldFnt.getName(), OldFnt.getStyle(), 10); p.setFont(NewFnt); System.out.println("Null font in RunInformationDialog.createControls()"); } // All position and sizes are in dialog logical units, so we use a // DialogLayout as our layout manager. //---------------------------------------------------------------------- panelLayout = new DialogLayout(p, 366, 366); p.setLayout(panelLayout); p.addNotify(); Dimension size = panelLayout.getDialogSize(); Insets insets = p.insets(); Insets thisInsets=this.insets(); p.resize(insets.left + size.width + insets.right, insets.top + size.height + insets.bottom); this.resize(insets.left+size.width+insets.right, insets.top+size.height+insets.bottom); // Control creation //---------------------------------------------------------------------- runButton = new Button ("Start"); p.add(runButton); panelLayout.setShape(runButton, 265, 23, 80, 14); resetButton = new Button ("Reset"); p.add(resetButton); panelLayout.setShape(resetButton, 265, 49, 80, 14); configureButton = new Button ("Configure"); p.add(configureButton); panelLayout.setShape(configureButton, 265, 74, 80, 14); genSizeLabel = new Label ("Generation Size", Label.LEFT); p.add(genSizeLabel); panelLayout.setShape(genSizeLabel, 271, 124, 78, 8); genSizeField = new Label ("", Label.LEFT); p.add(genSizeField); panelLayout.setShape(genSizeField, 271, 136, 79, 13); currGenLabel = new Label ("Current Generation", Label.LEFT); p.add(currGenLabel); panelLayout.setShape(currGenLabel, 271, 148, 79, 10); currGenField = new Label ("0", Label.LEFT); p.add(currGenField); panelLayout.setShape(currGenField, 271, 160, 80, 12); crossoverLabel = new Label ("Crossover Probability", Label.LEFT); p.add(crossoverLabel); panelLayout.setShape(crossoverLabel, 271, 172, 80, 11); crossoverField = new Label ("", Label.LEFT); p.add(crossoverField); panelLayout.setShape(crossoverField, 271, 184, 80, 14); mutationLabel = new Label ("Mutation Probability", Label.LEFT); p.add(mutationLabel); panelLayout.setShape(mutationLabel, 271, 196, 80, 9); mutationField = new Label ("", Label.LEFT); p.add(mutationField); panelLayout.setShape(mutationField, 271, 208, 80, 14); int realWidth=panelLayout.translateX(240); int realHeight=panelLayout.translateY(166); grapher = new Grapher(realWidth,realHeight,50,0, exec.getGenerationsGenerator().getBaseIndividual().getMaxFitness()); grapher.setAxes(5,40); grapher.setShowLabels(true); resetPressed(); p.add(grapher); panelLayout.setShape(grapher, 15, 23, 240, 166); renderer=new Renderer(); p.add(renderer); panelLayout.setShape(renderer, 15, 204, 240, 80); setResizable(false); refreshFields(); init = true; return true; } /*public boolean mouseDown(Event evt, int x, int y) { return false; } */ /*public boolean mouseUp(Event evt, int x, int y) { // TODO: Place applet mouseUp code here return true; } */ /*public boolean mouseDrag(Event evt, int x, int y) { // TODO: Place applet mouseDrag code here return true; } */ /*public boolean mouseMove(Event evt, int x, int y) { // TODO: Place applet mouseMove code here return true; } */ /*public boolean mouseEnter(Event evt, int x, int y) { // TODO: Place applet mouseEnter code here return true; } */ /*public boolean mouseExit(Event evt, int x, int y) { // TODO: Place applet mouseExit code here return true; } */ public boolean action(Event evt, Object arg) { if (evt.target == runButton) { runPressed(); return true; } else if (evt.target == resetButton) { resetPressed(); return true; } else if (evt.target == configureButton) { configurePressed(); return true; } else { return super.action(evt, arg); } } private void runPressed() { if (runner==null) { runner = new Thread(exec); runner.setPriority(Thread.NORM_PRIORITY-2); runner.start(); runButton.setLabel("Stop"); } else { runner.stop(); runner=null; runButton.setLabel("Start"); } } private void configurePressed() { if (exec!=null) { GenerationsGeneratorDialog dialog = new GenerationsGeneratorDialog(this,exec); dialog.show(); refreshFields(); } else { System.out.println("Executor is null in RunInformationDialog.configurePressed()"); } } private void refreshFields() { if (exec!=null) { GenerationsGenerator gGen = exec.getGenerationsGenerator(); genSizeField.setText(String.valueOf(gGen.getGenSize())); mutationField.setText(String.valueOf(gGen.getMutationProbability())); crossoverField.setText(String.valueOf(gGen.getCrossoverProbability())); } } private void resetPressed() { GenerationsContainer gc=exec.getGenerationsContainer(); if (gc!=null) gc.deleteObserver(this); exec.reset(); exec.getGenerationsContainer().addObserver(this); grapher.removeGraphs(); GenerationsContainerGraph g=new GenerationsContainerGraph( exec.getGenerationsContainer()); grapher.addGraph(g); g=new GenerationsContainerGraph(exec.getGenerationsContainer()); g.setGraphStatistic(GenerationsContainerGraph.MAXIMUM); grapher.addGraph(g); } public void update(Observable o, Object arg) { GenerationsContainer gc = exec.getGenerationsContainer(); if (o == gc) { currGenField.setText(String.valueOf( gc.getLastGenerationNumber())); renderer.setTarget(gc.getFittestIndividual()); } } public void finalize() { if (runner!=null) { runner.stop(); runner=null; } } }