1    /**
2     * @author Nick Peters
3     * COMP 469 - Heuristic Search
4     * Creates a randomly generated graph and finds the shortest path between two 
5     * random vertices using the A* search algorithm.
6     */
7    
8    package search;
9    
10   import java.awt.*;
11   import javax.swing.*;
12   import org.apache.commons.collections15.*;
13   import edu.uci.ics.jung.algorithms.layout.*;
14   import edu.uci.ics.jung.visualization.*;
15   import edu.uci.ics.jung.visualization.decorators.*;
16   import edu.uci.ics.jung.visualization.renderers.Renderer.VertexLabel.*;
17   import edu.uci.ics.jung.graph.*;
18   
19   public class SearchGUI extends JPanel {
20       static int SIZE = 600;
21       
22       public SearchGUI(UndirectedSparseGraph<MyVertex, WeightedEdge> g, Integer source, Integer dest) {
23               // Setup the visualization layout
24               Layout<MyVertex, WeightedEdge> layout = new CircleLayout(g);
25               layout.setSize(new Dimension(SIZE,SIZE));
26               BasicVisualizationServer<MyVertex,WeightedEdge> vv = new BasicVisualizationServer<MyVertex, WeightedEdge>(layout);
27               
28               // Used to color the vertices
29               Transformer<MyVertex,Paint> vertexPaint = new Transformer<MyVertex,Paint>() {
30                   public Paint transform(MyVertex i) {
31                       // If the vertex is on the shortest path, then color it red,
32                       // otherwise color it yellow.
33                       if(i.getPath())
34                           return Color.RED;
35                       else
36                           return Color.YELLOW;
37                   }
38               };
39               
40               // Used to determine the edge thickness
41               Transformer<WeightedEdge, Stroke> edgeStrokeTransformer = new Transformer<WeightedEdge, Stroke>() {
42                 public Stroke transform(WeightedEdge s) {
43                     // If the edge is on the shorest path, then give it a larger thickness.
44                     if(s.getPath())
45                         return new BasicStroke(2.0f);
46                     else
47                         return new BasicStroke(1.0f);
48                 }
49               };
50   
51               // Set the viewing area size
52               vv.setPreferredSize(new Dimension(SIZE + 50, SIZE + 50));
53               vv.getRenderContext().setVertexFillPaintTransformer(vertexPaint);
54               vv.getRenderContext().setEdgeStrokeTransformer(edgeStrokeTransformer);
55               vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
56               vv.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller());
57               vv.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR);
58               
59               vv.add(new JLabel("Shortest path from vertex " + source + " to vertex " + dest));
60             
61               JFrame frame = new JFrame("COMP 469 - Search");
62               frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
63               frame.getContentPane().add(vv);
64   
65               frame.pack();
66               frame.setVisible(true);       
67       }
68   }
69