|
@@ -0,0 +1,148 @@
|
|
|
|
+package algorithms.geneticAlgorithm.holegGA;
|
|
|
|
+
|
|
|
|
+import java.awt.EventQueue;
|
|
|
|
+
|
|
|
|
+import javax.swing.JFrame;
|
|
|
|
+import javax.swing.JSplitPane;
|
|
|
|
+
|
|
|
|
+import java.awt.BorderLayout;
|
|
|
|
+
|
|
|
|
+import javax.swing.GroupLayout;
|
|
|
|
+import javax.swing.GroupLayout.Alignment;
|
|
|
|
+import javax.swing.BoxLayout;
|
|
|
|
+import javax.swing.JScrollPane;
|
|
|
|
+import javax.swing.JTree;
|
|
|
|
+import javax.swing.JPanel;
|
|
|
|
+import javax.swing.tree.DefaultMutableTreeNode;
|
|
|
|
+import javax.swing.tree.DefaultTreeModel;
|
|
|
|
+import javax.swing.tree.TreePath;
|
|
|
|
+
|
|
|
|
+import ui.controller.Control;
|
|
|
|
+import ui.model.Model;
|
|
|
|
+import ui.view.MyCanvas;
|
|
|
|
+import ui.view.UnitGraph;
|
|
|
|
+
|
|
|
|
+import javax.swing.JButton;
|
|
|
|
+
|
|
|
|
+import algorithms.geneticAlgorithm.Components.GAResultListener;
|
|
|
|
+
|
|
|
|
+import java.awt.event.ActionListener;
|
|
|
|
+import java.awt.event.ActionEvent;
|
|
|
|
+import java.awt.event.MouseAdapter;
|
|
|
|
+import java.awt.event.MouseEvent;
|
|
|
|
+import java.awt.event.MouseListener;
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import javax.swing.JTextField;
|
|
|
|
+
|
|
|
|
+public class GenAlgoWindow implements GAResultListener<HolegIndividual> {
|
|
|
|
+
|
|
|
|
+ JFrame frame;
|
|
|
|
+ Control controller;
|
|
|
|
+ Model model;
|
|
|
|
+ JTree genTree;
|
|
|
|
+ DefaultTreeModel treeModel;
|
|
|
|
+ DefaultMutableTreeNode treeRoot;
|
|
|
|
+ HashMap<DefaultMutableTreeNode, HolegIndividual> treeIndiHashmap;
|
|
|
|
+
|
|
|
|
+ GenAlgoHandler algoHandler;
|
|
|
|
+ private JTextField textField;
|
|
|
|
+ /**
|
|
|
|
+ * Create the application.
|
|
|
|
+ */
|
|
|
|
+ public GenAlgoWindow(Control controller, Model model) {
|
|
|
|
+ this.model = model;
|
|
|
|
+ this.controller = controller;
|
|
|
|
+ algoHandler = new GenAlgoHandler(controller, model);
|
|
|
|
+ algoHandler.addListener(this);
|
|
|
|
+ treeIndiHashmap = new HashMap<DefaultMutableTreeNode, HolegIndividual>();
|
|
|
|
+ initialize();
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Initialize the contents of the frame.
|
|
|
|
+ */
|
|
|
|
+ private void initialize() {
|
|
|
|
+ frame = new JFrame();
|
|
|
|
+ frame.setBounds(100, 100, 450, 409);
|
|
|
|
+ frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
|
|
|
+ frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.X_AXIS));
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ JSplitPane mainSplitPane = new JSplitPane();
|
|
|
|
+ mainSplitPane.setResizeWeight(0.3);
|
|
|
|
+ frame.getContentPane().add(mainSplitPane);
|
|
|
|
+
|
|
|
|
+ JScrollPane treeScrollPane = new JScrollPane();
|
|
|
|
+ mainSplitPane.setLeftComponent(treeScrollPane);
|
|
|
|
+
|
|
|
|
+ genTree = new JTree();
|
|
|
|
+ treeScrollPane.setViewportView(genTree);
|
|
|
|
+
|
|
|
|
+ treeModel = (DefaultTreeModel) genTree.getModel();
|
|
|
|
+ treeRoot = new DefaultMutableTreeNode("Generations");
|
|
|
|
+ treeModel.setRoot(treeRoot);
|
|
|
|
+ genTree.setModel(treeModel);
|
|
|
|
+
|
|
|
|
+ JSplitPane rightSplitPane = new JSplitPane();
|
|
|
|
+ rightSplitPane.setResizeWeight(0.5);
|
|
|
|
+ rightSplitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
|
|
|
|
+ mainSplitPane.setRightComponent(rightSplitPane);
|
|
|
|
+
|
|
|
|
+ JPanel panel = new JPanel();
|
|
|
|
+ rightSplitPane.setLeftComponent(panel);
|
|
|
|
+
|
|
|
|
+ JButton btnCreategeneration = new JButton("createGeneration");
|
|
|
|
+ btnCreategeneration.addActionListener(new ActionListener() {
|
|
|
|
+ public void actionPerformed(ActionEvent arg0) {
|
|
|
|
+ algoHandler.createGeneration();
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ textField = new JTextField();
|
|
|
|
+ panel.add(textField);
|
|
|
|
+ textField.setColumns(10);
|
|
|
|
+ panel.add(btnCreategeneration);
|
|
|
|
+
|
|
|
|
+ MouseListener ml = new MouseAdapter() {
|
|
|
|
+ public void mousePressed(MouseEvent e) {
|
|
|
|
+ int selRow = genTree.getRowForLocation(e.getX(), e.getY());
|
|
|
|
+ TreePath selPath = genTree.getPathForLocation(e.getX(), e.getY());
|
|
|
|
+ if(selRow != -1) {
|
|
|
|
+ if(e.getClickCount() == 1) {
|
|
|
|
+ //mySingleClick(selRow, selPath);
|
|
|
|
+ }
|
|
|
|
+ else if(e.getClickCount() == 2) {
|
|
|
|
+ nodeDoubleClick(selRow, selPath);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ };
|
|
|
|
+ genTree.addMouseListener(ml);
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void nodeDoubleClick(int selRow, TreePath selPath){
|
|
|
|
+ DefaultMutableTreeNode node = (DefaultMutableTreeNode)selPath.getLastPathComponent();
|
|
|
|
+ if(treeIndiHashmap.get(node) != null){
|
|
|
|
+ algoHandler.drawIndividual(treeIndiHashmap.get(node));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void populationCreated(int generation, ArrayList<HolegIndividual> population) {
|
|
|
|
+ DefaultMutableTreeNode genNode = new DefaultMutableTreeNode("Generation " + generation);
|
|
|
|
+
|
|
|
|
+ for(int i = 0; i < population.size(); i++){
|
|
|
|
+ DefaultMutableTreeNode child = new DefaultMutableTreeNode("Individual " + i);
|
|
|
|
+ treeIndiHashmap.put(child, population.get(i));
|
|
|
|
+ genNode.add(child);
|
|
|
|
+ }
|
|
|
|
+ treeRoot.insert(genNode, 0);
|
|
|
|
+ treeModel.reload();
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+}
|