!
"
#
$%"
#
&
#
&'
(
(
$'
#
!
(
)
!"#$%&
!"#$&
'()*+,,
()*+,,,
--.
/'()*0/'()*0.
1$2/'()*0.
3$/'()*0.
/'()*0.
24%5678
-".
349:;%.
%<%6-&$&7.
6%-&===&->->->7.
$6<+.<.??78
$6"<+."<."??78
6%-&=&->/0/"07.
6/0/"0<<,7/0/"0<()*.
@
@
6%7.
@
24678
2.
$62<+.2<.2??78
/20</0/20.AABC
D
E
$
E
FG
%
F
2
2</0/20
AAH
G
1$2/20<.
AAIG
D
FJ
D
F
C
D
$
E
E
3$/20<$.
@
AA
F
E
1$2/0<,.
/0<,.
3$/0<.
@
2!"#$678
-2-%.
AA
E
E
$#
E
K63$/078
%<()*.
AAIJ
$G
E
%I$FJ
D
/0
L
I
G
E
%C
E
%
FJ
D
AA
FJ
$62<+.2<.2??78
63$/20>>6%/20778
%</20.
<2.
@
@
AAM#J
FC
E
FJ
D
J
F
E
FJ
D
3$/0<.
AA!
FJ
D
F
D
FJ
D
$I
63$/078
$62<+.2<.2??78
63$/20>>6/0?/0/20/20778
AAM
D
E
D
2
/20</0?/0/20.
AA92
1$2/20<.
@
@
@
@
@
N%5678
4.
349:;%.
%<%6-&O&7.
%$6%-&?!==P&--7.
%$6%-&=<&-7.
<1$2/0.
O6Q<78
%$6%-&=<&-7.
<1$2/0.
@
%$6%-&=&-7.
%$6%-&P?!=&-/07.
6%7.
@
*+,-. )
MRSTS:%U%$V2WX2
*/0 1, )
N%YZ[F\]^2^2_ZF`-Za6%b7-
R-cd-aL]UZ!"#$
package Dijkstra3;
import java.awt.Button;
import java.awt.Event;
import java.awt.GridLayout;
import java.awt.Panel;
class Options extends Panel {
/**
*
*/
private static final long serialVersionUID = 1L;
// Dat cac Options ben trai man hinh ung dung
Button b1 = new Button("Clear");
Button b2 = new Button("Run");
Button b3 = new Button("Step");
Button b4 = new Button("Reset");
Button b5 = new Button("Example");
Button b6 = new Button("Exit");
Button b7 = new Button("About");
GraphAlgorithm parent;
boolean Locked=false;
Options(GraphAlgorithm myparent) {
parent = myparent;
setLayout(new GridLayout(7, 1, 0, 10));
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b7);
add(b6);
}
public boolean action(Event evt, Object arg) {
if (evt.target instanceof Button) {
//Neu la JButton "Step" va Unlock thi thuc hien buoc dau tien va
hien thi "Nextstep" len JButton,
//nguoc lai thi khong thuc hien
if (((String)arg).equals("Step")) {
if (!Locked) {
b3.setLabel("Next step");
parent.graphcanvas.stepalg();
}
else parent.documentation.doctext.showline("locked");
}
//Neu la JButton "Nextstep" thi thuc hien buoc tiep thep
if (((String)arg).equals("Next step")){
parent.graphcanvas.nextstep();
}
//Neu la JButton "Reset" thi thuc hien reset
if (((String)arg).equals("Reset")) {
parent.graphcanvas.reset();
b3.setLabel("Step");
parent.documentation.doctext.showline("all items");
}
//Neu la JButton "Clear" thi xoa graphics
if (((String)arg).equals("Clear")) {
parent.graphcanvas.clear();
b3.setLabel("Step");
parent.documentation.doctext.showline("all items");
}
//Neu la JButton "Run" va Unlock thi thuc hien tung buoc mac dinh
//nguoc lai thi khong thuc hien
if (((String)arg).equals("Run")) {
if (!Locked)
parent.graphcanvas.runalg();
else parent.documentation.doctext.showline("locked");
}
//Neu la JButton "Example" va Unlock thi thuc hien default graphics
//nguoc lai thi khong thuc hien
if (((String)arg).equals("Example")) {
if (!Locked)
parent.graphcanvas.showexample();
else parent.documentation.doctext.showline("locked");
}
//Neu la JButton "About" thi hien thi dialog About
if(((String)arg).equals("About")){
dialog dig = new dialog();
}
//Neu la JButton "Exit" thi thoat khoi chuong trinh
if (((String)arg).equals("Exit")) {
System.exit(0);
}
}
return true;
}
public void lock() {
Locked=true;
}
public void unlock() {
Locked=false;
b3.setLabel("Step");
}
}
*$/0 2)
!YWeRSC$V2fZ
`eg2W2^h%biZF`$jZ$VaL]^k
U!"#$
package Dijkstra3;
import java.awt.BorderLayout;
import java.awt.Choice;
import java.awt.Event;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
class Documentation extends Panel {
// Documentaion huong dan o phia tren man hinh ung dung
DocOptions docopt = new DocOptions(this);
DocText doctext = new DocText();
Documentation() {
setLayout(new BorderLayout(10, 10));
add("West", docopt);
add("Center", doctext);
}
}
class DocOptions extends Panel {
//Tao Choice huong dan ben trai man hinh ung dung
Choice doc = new Choice();
Documentation parent;
DocOptions(Documentation myparent) {
setLayout(new GridLayout(2, 1, 5, 0));
parent = myparent;
add(new Label("DOCUMENTATION:"));
doc.addItem("draw nodes");
doc.addItem("remove nodes");
doc.addItem("move nodes");
doc.addItem("the startnode");
doc.addItem("draw arrows");
doc.addItem("change weights");
doc.addItem("remove arrows");
doc.addItem("clear / reset");
doc.addItem("run algorithm");
doc.addItem("step through");
doc.addItem("example");
doc.addItem("about");
doc.addItem("exit");
doc.addItem("all items");
add(doc);
}
public boolean action(Event evt, Object arg) {
//Ham xu ly Choice
if (evt.target instanceof Choice) {
String str=new String(doc.getSelectedItem());
parent.doctext.showline(str);
}
return true;
}
}
class DocText extends TextArea {
//TextDocumentation huong dan cho cac Choice va JButton
final String drawnodes = new String("DRAWING NODES:\n"+
"Draw a node by clicking the mouse on the screen application.\n\n");
final String rmvnodes = new String("REMOVE NODES:\n"+
"To remove a node press <ctrl> and click on the node.\n"+
"You can not remove the startnode.\n"+
"Select another startnode, then you can remove the node.\n\n");
final String mvnodes = new String("MOVING NODES\n"+
"To move a node press <Shift>, click on the node,\nand drag it to"+
" its new position.\n\n");
final String startnode = new String("STARTNODE:\n"+
"The startnode is blue, other nodes are grey.\n"+
"The first node you draw on the screen will be the startnode.\n"+
"To select another startnode press <ctrl>, click on the startnode,\n"+
"and drag the mouse to another node.\n"+
"To delete the startnode, first select another startnode, and then"+
"\nremove the node the usual way.\n\n");
final String drawarrows = new String("DRAWING ARROWS:\n"+
"To draw an arrow click mouse in a node,"+
"and drag it to another node.\n\n");
final String weight = new String("CHANGING WEIGHTS:\n"+
"To change the weight of an arrow, click on the arrowhead and drag\n"+
"it along the arrow.\n\n");
final String rmvarrows = new String("REMOVE ARROWS:\n"+
"To remove an arrow, change its weight to 0.\n\n");
final String clrreset = new String("<CLEAR> BUTTON: "+
"Remove the current graph from the screen.\n"+
"<RESET> BUTTON: "+
"Remove the results of the algorithm from the graph,\n"+
" and unlock screen.\n\n");
final String runalg = new String("<RUN> BUTTON: "+
"Run the algorithm on the graph, there will be a time\n" +
"delay of +/- 1 second between steps.\n"+
"To run the algorithm slower, use <STEP>.\n");
final String step = new String("<STEP> BUTTON: " +
"An opportunity to step through the algorithm.\n");
final String example = new String("<EXAMPLE> BUTTON: "+
"Displays a default graph on the screen for you.\n"+
"You can then use <STEP> or <RUN>\n");
final String exitbutton = new String("<EXIT> BUTTON: " +
"Only works if applet is run with appletviewer.\n");
final String aboutbutton = new String("<ABOUT> BUTTON: " +
"Show about Dijkstra program information.\n");
final String toclose = new String("ERROR: "+
"This position is to close to another node/arrow.\n");
final String done = new String("Algorithm has finished, " +
"follow green arrows from startnode to any node "+
"to get\nthe shortest path to " +
"the node. The length of the path is written in the node.\n" +
"press <RESET> to reset the graph, and unlock the screen.");
final String some = new String("Algorithm has finished, " +
"follow green arrows from startnode to any node "+
"to get\nthe shortest path to " +
"the node. The length of the path is written in the node.\n" +
"There are no paths from the startnode to any gray node.\n" +
"press <RESET> to reset the graph, and unlock the screen.");
final String none = new String("Algorithm has finished, " +
"there are no nodes reachable from the start node.\n"+
"press <RESET> to reset the graph, and unlock the screen.");
final String maxnodes = new String("ERROR: "+
"Maximum number of nodes reached!\n\n");
final String info = new String("DOCUMENTATION:\n"+
"You can scroll through the documentation or get documentation\n"+
"on a specific "+
"item by selecting the item on the left.\nSelecting <All items> "+
"brings you back "+
" to the scrolling text.\n\n");
final String locked = new String("ERROR: "+
"Keyboard/mouse locked for this action.\n"+
"Either press <NEXT STEP> or <RESET>.\n");
final String doc = info + drawnodes + rmvnodes + mvnodes +
startnode + drawarrows + weight + rmvarrows +
clrreset + runalg + step + example + aboutbutton +
exitbutton;
DocText() {
super(5, 2);
setEditable(false);
setText(doc);
}
public void showline(String str) {
if (str.equals("draw nodes")) setText(drawnodes);
else if (str.equals("remove nodes")) setText(rmvnodes);
else if (str.equals("move nodes")) setText(mvnodes);
else if (str.equals("the startnode")) setText(startnode);
else if (str.equals("draw arrows")) setText(drawarrows);
else if (str.equals("change weights")) setText(weight);
else if (str.equals("remove arrows")) setText(rmvarrows);
else if (str.equals("clear / reset")) setText(clrreset);
else if (str.equals("run algorithm")) setText(runalg);
else if (str.equals("step through")) setText(step);
else if (str.equals("example")) setText(example);
else if (str.equals("exit")) setText(exitbutton);
else if (str.equals("about")) setText(aboutbutton);
else if (str.equals("all items")) setText(doc);
else if (str.equals("toclose")) setText(toclose);
else if (str.equals("done")) setText(done);
else if (str.equals("locked")) setText(locked);
else if (str.equals("maxnodes")) setText(maxnodes);
else if (str.equals("none")) setText(none);
else if (str.equals("some")) setText(some);
else setText(str);
}
}
*/0 0)
2l%ml-n2o
package Dijkstra3;
import java.awt.*;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
class dialog extends JDialog implements ActionListener{
//tao Button OK
JButton jb=new JButton("OK");
//tao JPanel
JPanel jp1=new JPanel();
JPanel jp2=new JPanel(new GridLayout(3,1));
JPanel jp3=new JPanel();
ImageIcon img=new
ImageIcon(this.getClass().getResource("/nhanntv/nhanntv1.png"));
//tao JLabel
JLabel jlb1=new JLabel(img);
JLabel jlb2=new JLabel("************Program Java to************");
JLabel jlb3=new JLabel("****Dijkstra Algorithm Program Demo****");
JLabel jlb4=new JLabel("***CopyWrite @2011 Edit by Nhanntv***");
//Tao Layout
GridBagLayout gb=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
private static final long serialVersionUID = 1L;
public dialog(){
setSize(300,200);
setLayout(gb);
jp1.add(jlb1);
jp2.add(jlb2);
jp2.add(jlb3);
jp2.add(jlb4);
jb.addActionListener(this);
jp3.add(jb);
gbc.fill=GridBagConstraints.EAST;
addComponent(jp1,0,0,4,10);
gbc.fill=GridBagConstraints.NORTHWEST;
addComponent(jp2,0,10,3,15);
gbc.fill=GridBagConstraints.NORTHWEST;
addComponent(jp3,3,15,1,5);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==jb){
this.dispose();
}
}
public void addComponent(Component c,int row,int col,int nrow,int ncol){
gbc.gridy=row;// toa do y
gbc.gridx=col;// toa do x
gbc.gridheight=nrow;// so dong chiem
gbc.gridwidth=ncol;// so cot chiem
gb.setConstraints(c,gbc);
add(c);
}
}
*/0 3,/ )
p$%2fFqhrC$V-YZUZk
jLo2^VFnFst$a6M$1u7-Z%C
YF\2_Ta-cGL]2fVFn-F$#ojkWaFqhk
U!"#$
package Dijkstra3;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
class GraphCanvas extends Canvas implements Runnable {
// drawing area for the graph
final int MAXNODES = 20;
final int MAX = MAXNODES+1;
final int NODESIZE = 26;
final int NODERADIX = 13;
final int DIJKSTRA = 1;
// basic graph information
Point node[] = new Point[MAX]; // node
int weight[][] = new int[MAX][MAX]; // weight of arrow
Point arrow[][] = new Point[MAX][MAX]; // current position of arrowhead
Point startp[][] = new Point[MAX][MAX]; // start and
Point endp[][] = new Point[MAX][MAX]; // endpoint of arrow
float dir_x[][] = new float[MAX][MAX]; // direction of arrow
float dir_y[][] = new float[MAX][MAX]; // direction of arrow
// graph information while running algorithm
boolean algedge[][] = new boolean[MAX][MAX];
int dist[] = new int[MAX];
int finaldist[] = new int[MAX];
Color colornode[] = new Color[MAX];
boolean changed[] = new boolean[MAX]; // indicates distance change during
algorithm
int numchanged =0;
int neighbours=0;
int step=0;
// information used by the algorithm to find the next