package myhoangthanh.yahoo.com;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Map;
import java.util.Properties;
import java.util.StringTokenizer;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Timer;
import org.jgraph.JGraph;
import org.jgraph.graph.DefaultCellViewFactory;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.DefaultGraphModel;
import org.jgraph.graph.DefaultPort;
import org.jgraph.graph.GraphConstants;
import org.jgraph.graph.GraphLayoutCache;
import org.jgraph.graph.GraphModel;
import org.jgrapht.alg.DijkstraShortestPath;
import org.jgrapht.graph.DefaultDirectedWeightedGraph;
import org.jgrapht.graph.DefaultWeightedEdge;
public class FindShortestPathOfDirectedWeightedGraph extends JFrame{
private static final long serialVersionUID = 1L;
/*
* graph is instance of JGraph Object
* grapht is instance of JGraphT Object
*/
DefaultDirectedWeightedGraph
ArrayList
ArrayList
Properties savelist = new Properties();
JGraph graph;
int delay = 0;
//For Jframe
JButton btAddv = new JButton("Add Vertex");
JTextField txtVertex = new JTextField();
JButton btAdde = new JButton("Add Edge");
JButton btDj = new JButton("Dijkstra");
JPanel canvas = new JPanel();
JPanel toolbar = new JPanel();
String[] items = {"HaNoi", "Beijing", "NewYork", "Tokyo", "Paris"};
JComboBox startvertex = new JComboBox(items);
JComboBox endvertex = new JComboBox(items);
JLabel lblen = new JLabel("Length of ShortestPath: ");
Timer time;
public void initJGraph(){
GraphModel model = new DefaultGraphModel();
GraphLayoutCache view = new GraphLayoutCache(model, new DefaultCellViewFactory());
graph = new JGraph(model, view);
cell.add(new DefaultGraphCell(new String("HaNoi")));
savelist.setProperty("HaNoi", "0");
cell.add(new DefaultGraphCell(new String("Beijing")));
savelist.setProperty("Beijing", "1");
cell.add(new DefaultGraphCell(new String("NewYork")));
savelist.setProperty("NewYork", "2");
cell.add(new DefaultGraphCell(new String("Tokyo")));
savelist.setProperty("Tokyo", "3");
cell.add(new DefaultGraphCell(new String("Paris")));
savelist.setProperty("Paris", "4");
for (int i=0; i < cell.size(); i++ ){ addVertex(cell.get(i)); }
}
public void addVertex(DefaultGraphCell c){
GraphConstants.setBounds(c.getAttributes(), new Rectangle2D.Double(500*Math.random(), 500*Math.random(), 150, 40));
GraphConstants.setBackground(c.getAttributes(), Color.RED);
GraphConstants.setOpaque(c.getAttributes(), true);
GraphConstants.setFont(c.getAttributes(), new Font(Font.SANS_SERIF, Font.BOLD, 25));
DefaultPort port = new DefaultPort();
c.add(port);
port.setParent(c);
graph.getGraphLayoutCache().insert(c);
}
public void initJGraphT(){
grapht = new DefaultDirectedWeightedGraph
grapht.addVertex("HaNoi");
grapht.addVertex("Beijing");
grapht.addVertex("NewYork");
grapht.addVertex("Tokyo");
grapht.addVertex("Paris");
}
public void initSwing(){
canvas.setSize(500, 500);
canvas.setBackground(Color.white);
toolbar.setSize(600, 50);
txtVertex.setColumns(15);
setLayout(new BorderLayout());
add(toolbar, BorderLayout.NORTH);
add(canvas, BorderLayout.CENTER);
add(btDj, BorderLayout.SOUTH);
canvas.add(graph);
toolbar.setLayout(new FlowLayout());
toolbar.add(txtVertex);
toolbar.add(btAddv);
toolbar.add(startvertex);
toolbar.add(endvertex);
toolbar.add(btAdde);
toolbar.add(lblen);
pack();
btAddv.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
startvertex.addItem(txtVertex.getText());
endvertex.addItem(txtVertex.getText());
grapht.addVertex(txtVertex.getText());
cell.add(new DefaultGraphCell(txtVertex.getText()));
savelist.setProperty(txtVertex.getText(), ((Integer)(cell.size()-1)).toString());
addVertex(cell.get(savelist.size()-1));
txtVertex.setText("");
}
});
btAdde.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int weight = Integer.parseInt(JOptionPane.showInputDialog(null, "Weight of this Vertex:"));
grapht.addEdge(startvertex.getSelectedItem().toString(), endvertex.getSelectedItem().toString());
grapht.setEdgeWeight(grapht.getEdge(startvertex.getSelectedItem().toString(), endvertex.getSelectedItem().toString()), weight);
org.jgraph.graph.DefaultEdge edge = new org.jgraph.graph.DefaultEdge();
int arrow = GraphConstants.ARROW_CLASSIC;
GraphConstants.setLineEnd(edge.getAttributes(), arrow);
GraphConstants.setEndFill(edge.getAttributes(), true);
GraphConstants.setForeground(edge.getAttributes(), Color.blue);
GraphConstants.setFont(edge.getAttributes(), new Font(Font.SANS_SERIF, Font.BOLD, 30));
GraphConstants.setValue(edge.getAttributes(), weight);
edge.setSource(cell.get(Integer.parseInt(savelist.getProperty(startvertex.getSelectedItem().toString()))).getChildAt(0));
edge.setTarget(cell.get(Integer.parseInt(savelist.getProperty(endvertex.getSelectedItem().toString()))).getChildAt(0));
graph.getGraphLayoutCache().insert(edge);
}
});
btDj.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String start = startvertex.getSelectedItem().toString();
String end = endvertex.getSelectedItem().toString();
DijkstraShortestPath
StringTokenizer token = new StringTokenizer(dj.getPath().toString().replaceAll(" ", ""), "():,[]");
if (token.hasMoreTokens())
listedge.add(token.nextToken());
int numtoken = (token.countTokens()%2 == 0)?(token.countTokens()):(token.countTokens()+1);
for (int i=0; i< numtoken; i++){
if (token.hasMoreTokens())
listedge.add(token.nextToken());
if (token.hasMoreTokens())
token.nextToken();
}
if (token.hasMoreTokens())
listedge.add(token.nextToken());
time = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
setBackGround(cell.get(Integer.parseInt(savelist.get(listedge.get(delay)).toString())));
if(delay
delay++;
else
time.stop();
}
});
time.start();
setBorderColor(cell.get(Integer.parseInt(savelist.get(listedge.get(0)).toString())));
setBorderColor(cell.get(Integer.parseInt(savelist.get(listedge.get(listedge.size()-1)).toString())));
lblen.setText("Length of ShortestPath: "+dj.getPathLength());
}
});
}
@SuppressWarnings("unchecked")
public void setBackGround(DefaultGraphCell c){
Map
Map attributeMap1 = new Hashtable();
GraphConstants.setBackground(attributeMap1, Color.green);
nested.put(c, attributeMap1);
graph.getGraphLayoutCache().edit(nested);
}
@SuppressWarnings("unchecked")
public void setBorderColor(DefaultGraphCell c){
Map
Map attributeMap1 = new Hashtable();
GraphConstants.setBorderColor(attributeMap1, Color.red);
nested.put(c, attributeMap1);
graph.getGraphLayoutCache().edit(nested);
}
public FindShortestPathOfDirectedWeightedGraph(String s){
super(s);
initJGraph();
initJGraphT();
initSwing();
}
}
2. Lớp MainClass.java
package myhoangthanh.yahoo.com;
import javax.swing.JFrame;
public class MainClass {
public static void main(String[] args) {
FindShortestPathOfDirectedWeightedGraph myform = new FindShortestPathOfDirectedWeightedGraph("Find Shortest Path of Directed Weighted Graph.");
myform.setBounds(8, 30, 720, 870);
myform.setVisible(true);
myform.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
No comments:
Post a Comment