Skip to content

Commit

Permalink
auto-generated GUI code embedded into ASCIIMazeVisualizer.java
Browse files Browse the repository at this point in the history
  • Loading branch information
pablo.rodriguez.mier committed Jul 30, 2014
1 parent e44e543 commit 2e5bd16
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 29 deletions.
7 changes: 7 additions & 0 deletions hipster-examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,12 @@
<artifactId>hipster-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.intellij</groupId>
<artifactId>forms_rt</artifactId>
<version>7.0.3</version>
</dependency>
</dependencies>


</project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import es.usc.citius.hipster.algorithm.Hipster;
import es.usc.citius.hipster.model.Node;
import es.usc.citius.hipster.model.Transition;
Expand Down Expand Up @@ -45,17 +47,89 @@ public class ASCIIMazeVisualizer {
private JFrame mainFrame;
private String lastMaze;

{
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
$$$setupUI$$$();
}

/**
* Method generated by IntelliJ IDEA GUI Designer
* >>> IMPORTANT!! <<<
* DO NOT edit this method OR call it in your code!
*
* @noinspection ALL
*/
private void $$$setupUI$$$() {
mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout(0, 0));
optionPanel = new JPanel();
optionPanel.setLayout(new GridLayoutManager(4, 1, new Insets(0, 0, 0, 0), -1, -1));
optionPanel.setEnabled(true);
mainPanel.add(optionPanel, BorderLayout.NORTH);
loadMazeLabel = new JLabel();
loadMazeLabel.setText("Load ASCII Maze:");
optionPanel.add(loadMazeLabel, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
comboMazes = new JComboBox();
optionPanel.add(comboMazes, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
algoContainerPanel = new JPanel();
algoContainerPanel.setLayout(new GridLayoutManager(2, 1, new Insets(0, 0, 0, 0), -1, -1));
optionPanel.add(algoContainerPanel, new GridConstraints(3, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
comboAlgorithm = new JComboBox();
algoContainerPanel.add(comboAlgorithm, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
configAlgorithmPanel = new JPanel();
configAlgorithmPanel.setLayout(new BorderLayout(0, 0));
algoContainerPanel.add(configAlgorithmPanel, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
refreshPanel = new JPanel();
refreshPanel.setLayout(new GridLayoutManager(1, 2, new Insets(0, 0, 0, 0), -1, -1));
configAlgorithmPanel.add(refreshPanel, BorderLayout.WEST);
refreshSpinner = new JSpinner();
refreshPanel.add(refreshSpinner, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(50, -1), null, 0, false));
refreshLabel = new JLabel();
refreshLabel.setText("Refresh interval (ms):");
refreshPanel.add(refreshLabel, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayoutManager(1, 2, new Insets(0, 0, 0, 0), -1, -1));
configAlgorithmPanel.add(buttonPanel, BorderLayout.EAST);
runButton = new JButton();
runButton.setHorizontalAlignment(4);
runButton.setText("Run");
buttonPanel.add(runButton, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
resetButton = new JButton();
resetButton.setHorizontalAlignment(4);
resetButton.setText("Reset");
buttonPanel.add(resetButton, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
final JLabel label1 = new JLabel();
label1.setText("Select algorithm:");
optionPanel.add(label1, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
textContainerPanel = new JPanel();
textContainerPanel.setLayout(new GridLayoutManager(1, 1, new Insets(10, 1, 1, 1), -1, -1));
mainPanel.add(textContainerPanel, BorderLayout.CENTER);
mazeTextArea = new JTextArea();
mazeTextArea.setFont(new Font("Monospaced", mazeTextArea.getFont().getStyle(), 20));
textContainerPanel.add(mazeTextArea, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
}

/**
* @noinspection ALL
*/
public JComponent $$$getRootComponent$$$() {
return mainPanel;
}

private enum State {STOPPED, STARTED, PAUSED}

// Execution state
private State state = State.STOPPED;
private AlgorithmListener algorithmListener;

public static void main(String[] args){
public static void main(String[] args) {

try {
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception ex){}
} catch (Exception ex) {
}

JFrame frame = new JFrame("Hipster Maze Shortest Path Visualizer");
frame.setContentPane(new ASCIIMazeVisualizer(frame).mainPanel);
Expand All @@ -70,16 +144,36 @@ public ASCIIMazeVisualizer(final JFrame frame) {
// Use double buffer for smooth updates
mazeTextArea.setDoubleBuffered(true);
refreshSpinner.setValue(50);

// Default values
comboMazes.addItem("Maze example 1");
comboMazes.addItem("Maze example 2");
comboMazes.addItem("Maze example 3");
comboMazes.addItem("Maze example 4");
comboMazes.addItem("Maze example 5");

comboAlgorithm.addItem("Breadth First Search (BFS, non-optimal) ");
comboAlgorithm.addItem("Bellman Ford");
comboAlgorithm.addItem("Dijkstra");
comboAlgorithm.addItem("A*");
comboAlgorithm.addItem("IDA*");

// Listener to process run/pause button
runButton.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
// Fill with spaces
switch (state){
case STOPPED: start(); break;
case STARTED: pause(); break;
case PAUSED: start(); break;
switch (state) {
case STOPPED:
start();
break;
case STARTED:
pause();
break;
case PAUSED:
start();
break;
}
}
});
Expand All @@ -103,13 +197,13 @@ public void actionPerformed(ActionEvent e) {
loadSelectedMaze();
}

private void fillTextAreaWithSpaces(){
private void fillTextAreaWithSpaces() {

}

private void start(){
private void start() {

if (state.equals(State.PAUSED)){
if (state.equals(State.PAUSED)) {
algorithmListener.startTimer();
runButton.setText("Pause");
state = State.STARTED;
Expand All @@ -131,23 +225,23 @@ private void start(){
state = State.STARTED;
}

private void stop(){
if (state.equals(State.STARTED) || state.equals(State.PAUSED)){
private void stop() {
if (state.equals(State.STARTED) || state.equals(State.PAUSED)) {
algorithmListener.stopTimer();
}
runButton.setText("Run");
state = State.STOPPED;
}

private void pause(){
if (state.equals(State.STARTED)){
private void pause() {
if (state.equals(State.STARTED)) {
algorithmListener.stopTimer();
}
runButton.setText("Run");
state = State.PAUSED;
}

private void reset(){
private void reset() {
stop();
loadSelectedMaze();
}
Expand All @@ -164,13 +258,13 @@ private class AlgorithmListener implements ActionListener {
private AlgorithmListener(Iterator<? extends Node<?, Point, ?>> algorithmIterator, Maze2D maze) {
this.algorithmIterator = algorithmIterator;
this.maze = maze;
this.timer = new Timer((Integer)refreshSpinner.getValue(), this);
this.timer = new Timer((Integer) refreshSpinner.getValue(), this);
}

@Override
public void actionPerformed(ActionEvent e) {
if (!algorithmIterator.hasNext()) return;
Node<?,Point,?> currentNode = algorithmIterator.next();
Node<?, Point, ?> currentNode = algorithmIterator.next();
// Record all visited states to mark as visited in the string
explored.add(currentNode.state());
List<Point> statePath = Lists.transform(currentNode.path(), new Function<Node<?, Point, ?>, Point>() {
Expand All @@ -189,14 +283,14 @@ public void startTimer() {
timer.start();
}

public void stopTimer(){
public void stopTimer() {
timer.stop();
}
}

private Iterator<? extends Node<?, Point, ?>> createAlgorithm(Maze2D maze){
private Iterator<? extends Node<?, Point, ?>> createAlgorithm(Maze2D maze) {
final Iterator<? extends Node<?, Point, ?>> iterator;
switch(comboAlgorithm.getSelectedIndex()){
switch (comboAlgorithm.getSelectedIndex()) {

case 0:
iterator = Hipster.createBreadthFirstSearch(buildProblem(maze, false)).iterator();
Expand All @@ -219,13 +313,23 @@ public void stopTimer(){
return iterator;
}

private void loadSelectedMaze(){
switch(comboMazes.getSelectedIndex()){
case 0: mazeTextArea.setText(Joiner.on('\n').join(Mazes.exampleMaze1)); break;
case 1: mazeTextArea.setText(Joiner.on('\n').join(Mazes.testMaze4)); break;
case 2: mazeTextArea.setText(Joiner.on('\n').join(Mazes.testMaze3)); break;
case 3: mazeTextArea.setText(Joiner.on('\n').join(Mazes.testMaze2)); break;
case 4: mazeTextArea.setText(Joiner.on('\n').join(Mazes.testMaze5)); break;
private void loadSelectedMaze() {
switch (comboMazes.getSelectedIndex()) {
case 0:
mazeTextArea.setText(Joiner.on('\n').join(Mazes.exampleMaze1));
break;
case 1:
mazeTextArea.setText(Joiner.on('\n').join(Mazes.testMaze4));
break;
case 2:
mazeTextArea.setText(Joiner.on('\n').join(Mazes.testMaze3));
break;
case 3:
mazeTextArea.setText(Joiner.on('\n').join(Mazes.testMaze2));
break;
case 4:
mazeTextArea.setText(Joiner.on('\n').join(Mazes.testMaze5));
break;
}
}

Expand All @@ -244,7 +348,7 @@ private String getMazeStringSolution(Maze2D maze, Collection<Point> explored, Co
return maze.getReplacedMazeString(replacements);
}

private SearchProblem<Void, Point, WeightedNode<Void, Point, Double>> buildProblem(final Maze2D maze, final boolean heuristic){
private SearchProblem<Void, Point, WeightedNode<Void, Point, Double>> buildProblem(final Maze2D maze, final boolean heuristic) {
return ProblemBuilder.create()
.initialState(maze.getInitialLoc())
.defineProblemWithoutActions()
Expand All @@ -260,7 +364,7 @@ public Double evaluate(Transition<Void, Point> transition) {
Point source = transition.getFromState();
Point destination = transition.getState();
double distance = source.distance(destination);
double roundedDistance = (double) Math.round(distance*1e5)/1e5;
double roundedDistance = (double) Math.round(distance * 1e5) / 1e5;
return roundedDistance;
}
})
Expand All @@ -269,7 +373,7 @@ public Double evaluate(Transition<Void, Point> transition) {
public Double estimate(Point state) {
if (heuristic) {
double distance = state.distance(maze.getGoalLoc());
double roundedDistance = (double) Math.round(distance*1e5)/1e5;
double roundedDistance = (double) Math.round(distance * 1e5) / 1e5;
return roundedDistance;
}
return 0d;
Expand Down

0 comments on commit 2e5bd16

Please sign in to comment.