!
"
#
$%"
#
&
#
&'
(
(
$'
#
!
(
)
$
E
FG
%
F
2
2</0/20
AAH
G
1$2/20<.
AAIG
D
FJ
D
F
C
K63$/078
%<()*.
AAIJ
$G
E
%I$FJ
D
/0
L
I
G
E
%C
E
%
FJ
D
AA
FJ
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<.
@
@
@
@
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) {
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");
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 {
"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, " +
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);
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){
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