To load this file without formatting, visit http://whoyouknow.co.uk/ants/AntGraph/java/3/src/antgraph/gui/GraphJFrame.java. This is a spam-protection measure; sorry for the inconvenience.

· GraphJFrame.java ·

   1/*
   2 * GraphJFrame.java
   3 *
   4 * Created on 04 March 2007, 13:30
   5 *
   6 * To change this template, choose Tools | Template Manager
   7 * and open the template in the editor.
   8 */
   9
  10package antgraph.gui;
  11
  12import antgraph.Edge;
  13import antgraph.Graph;
  14import antgraph.NoSuchNodeException;
  15import antgraph.Node;
  16import java.awt.event.KeyAdapter;
  17import java.awt.event.KeyEvent;
  18import java.awt.print.PrinterJob;
  19
  20import java.util.ArrayList;
  21import java.util.HashMap;
  22import java.util.List;
  23import java.util.Map;
  24import java.util.PriorityQueue;
  25import java.util.Queue;
  26import javax.swing.JFrame;
  27
  28/**
  29 *
  30 * @author James
  31 */
  32public class GraphJFrame extends JFrame {
  33    
  34
  35    private GraphCanvas m = new GraphCanvas();
  36    
  37    /** Creates a new instance of GraphJFrame */
  38    public GraphJFrame() {
  39        
  40        setSize((int)m.getSize().getWidth() + 13, (int)m.getSize().getHeight() + 35);
  41        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  42        setResizable(false);
  43        setVisible(true); 
  44        add(m);
  45        
  46        m.addKeyListener( new KeyAdapter() { 
  47            public void keyPressed(KeyEvent e) {
  48                if(e.getKeyChar() == 'p') {
  49                    printGraphCanvas(m);
  50                }else if(e.getKeyChar() == 'w') {
  51                    for(Edge edge : m.getGraph().getEdges()) {
  52                        edge.toggleWeights();
  53                    }
  54                }
  55                
  56            }
  57        });
  58       m.setFocusable(true);
  59        initialise();
  60    }
  61    
  62    private void initialise() {
  63        Graph graph = getTestGraph();//new Graph();
  64        
  65        m.setGraph(graph);//.load(STANDARD_FILE)
  66        
  67        
  68        pfs(graph, graph.getNodes().get(0), graph.getNodes().get(graph.getNodes().size() - 1));
  69         
  70    }
  71    
  72    public void printGraphCanvas(GraphCanvas gc) {
  73        PrinterJob printJob = PrinterJob.getPrinterJob ();
  74        
  75        printJob.setPrintable(gc);
  76        
  77        if (printJob.printDialog()) {
  78            try {
  79                printJob.print();
  80            } catch (Exception PrintException) {
  81                PrintException.printStackTrace();
  82            }
  83        }
  84
  85    }
  86    
  87    public static Graph getTestGraph() {
  88        
  89            Graph graph = new Graph();    
  90            
  91            Node a = new Node("a");
  92            a.setLocation(81, 130);
  93            Node b = new Node("b");
  94            b.setLocation(90, 225);
  95            Node c = new Node("c");
  96            c.setLocation(165, 188);
  97            Node d = new Node("d");     
  98            d.setLocation(231, 126);
  99            Node e = new Node("e");
 100            e.setLocation(357, 156);
 101            Node f = new Node("f");
 102             f.setLocation(289, 233);
 103            Node g = new Node("g");
 104             g.setLocation(401, 207);
 105            Node h = new Node("h");
 106             h.setLocation(215, 269);
 107            Node i = new Node("i");
 108             i.setLocation(320, 349);
 109            Node j = new Node("j");   
 110             j.setLocation(381, 297);
 111            Node k = new Node("k");
 112             k.setLocation(219, 378);
 113            Node l = new Node("l");   
 114             l.setLocation(103, 347);
 115            
 116             graph.setCurrentChar('l');
 117             
 118            graph.addNode(a);
 119            graph.addNode(b);
 120            graph.addNode(c);
 121            graph.addNode(d);
 122            graph.addNode(e);
 123            graph.addNode(f);
 124            graph.addNode(g);
 125            graph.addNode(h);           
 126            graph.addNode(i);
 127            graph.addNode(j);
 128            graph.addNode(k);
 129            graph.addNode(l);     
 130            
 131            try {
 132                graph.addEdge(a, c);
 133                graph.addEdge(b, c);
 134                graph.addEdge(c, d);
 135                graph.addEdge(c, h);
 136                graph.addEdge(d, e);
 137                graph.addEdge(d, f);
 138                graph.addEdge(f, g);
 139                graph.addEdge(f, j);
 140                graph.addEdge(g, j);
 141                graph.addEdge(j, i);
 142                graph.addEdge(i, k);
 143                graph.addEdge(h, k);
 144                graph.addEdge(l, k);
 145            }catch (NoSuchNodeException ex) {
 146                System.out.println(ex);
 147            }
 148            
 149        return graph;
 150    }
 151
 152    public static void pfs(Graph g, Node start, Node finish) {
 153        Queue<Node> pq = new PriorityQueue<Node>();
 154        List<Node> visited = new ArrayList<Node>();
 155        
 156        Map<Node, Double> distance = new HashMap<Node, Double>();
 157        
 158        for(Node n : g.getNodes()) {
 159            distance.put(n, Double.POSITIVE_INFINITY);
 160        }
 161        
 162        distance.put(start, 0.0);
 163        
 164        Node currentNode = null;
 165        
 166        
 167        pq.offer(start);
 168        
 169        while(pq.size() != 0 && currentNode != finish) {
 170            currentNode = pq.poll();
 171            
 172            if(!visited.contains(currentNode)) {
 173                visited.add(currentNode);
 174                
 175                for(Node n : g.getAdjacencyList(currentNode)) {
 176                    if(visited.contains(n)) continue;
 177                    
 178                   double d = distance.get(n);
 179                   double d2 = distance.get(currentNode);
 180                   if(d < d2) distance.put(n, d2 + g.getEdge(currentNode, n).getWeight());
 181                    
 182                    visited.add(n);
 183                    pq.offer(n);
 184                }
 185                
 186            }
 187        }
 188        
 189    }
 190    
 191    public static void main(String[] args) {
 192        java.awt.EventQueue.invokeLater(new Runnable() {
 193            public void run() {
 194                new GraphJFrame();
 195            }
 196        });
 197          
 198        
 199        
 200    }       
 201    
 202}
 203

· GraphJFrame.java ends ·

Generated by CHIP: Code Highlighting in PHP, version 2.7.0.