// ImgCanvas Class
class ImgCanvas extends Canvas implements Runnable
{ final static int MAXNODES = 20;
final static int MAX = MAXNODES+1;
final int NODESIZE = 20;
final int NODERADIX = 10; Buttons btObject = new Buttons();
// 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 dirX[][] = new float[MAX][MAX]; // direction of arrow
float dirY[][] = new float[MAX][MAX]; // direction of arrow Color nodeColor [] = new Color[MAX]; Point thispoint=new Point(0,0); // current mouseposition
Point oldpoint=new Point(0, 0); // previous position of node being moved // current action
boolean newarrow = false;
boolean movearrow = false;
boolean movestart = false;
boolean deletenode = false;
boolean movenode = false;
boolean performalg = false;
boolean clicked = false; int stringPointX;
int stringPointY; int numOfNodes = 0;
int deleteNodes = 0;
int startGraph = 0;
int hitNode; // mouse clicked on or close to this node
int hitLine; // mouse clicked on or close to this line
int node1, node2; // numbers of nodes involved in current action
int nodeI,nodeJ;
// fonts
Font roman= new Font("TimesRoman", Font.BOLD, 12);
Font helvetica= new Font("Helvetica", Font.BOLD, 15);
FontMetrics fmetrics = getFontMetrics(roman);
int h = (int)fmetrics.getHeight()/3; // for double buffering
private Image offScreenImage;
private Graphics offScreenGraphics;
private Dimension offScreenSize; int mindis, minnode,minstart, minend; int algIndex; NewAlgorithm parent; Thread runThread; Dijkstra dijkstra;
static boolean traEdge[][]=new boolean[MAX][MAX];
static int traDistance[]=new int[MAX];
static int traLastDistance[]=new int[MAX];
static boolean traChanged[]=new boolean[MAX];
static int traMinDistance = 0, traMinStart = 0, traMinEnd = 0;
static int traNumChanged = 0, traStep = 0;
static boolean lock = false; public void init()
{
for (int i=0;i<MAXNODES;i++)
{
nodeColor[i]=Color.lightGray;
}
nodeColor[startGraph]=Color.green;
performalg = false; }
public boolean nodehit(double x, double y, int dist)
{
// checks if you hit a node with your mouseclick
for (int i=0; i<numOfNodes; i++)
if ( (x-node[i].x)*(x-node[i].x) + (y-node[i].y)*(y-node[i].y) < dist*dist )
{
hitNode = i;
return true;
}
return false;
}
ImgCanvas(NewAlgorithm parentIn)
{
parent = parentIn;
init();
algIndex=btObject.algSelect.getSelectedIndex(); setBackground(Color.white);
} public void run()
{ } public boolean mouseDown(Event evt, int x, int y)
{
if(lock)
{
btObject.comment.setText("The panel is locked!!");
//JOptionPane.showMessageDialog(null,"The panel is locked", JOptionPane.ERROR_MESSAGE);
} else
{
if (evt.shiftDown())
{
// move a node
if (nodehit(x, y, NODESIZE))
{
oldpoint = node[hitNode];
node1 = hitNode;
movenode=true;
}
}
else if (evt.controlDown())
{
// delete a node
if (nodehit(x, y, NODESIZE))
{
node1 = hitNode;
if (startGraph==node1)
{
movestart=true;
thispoint = new Point(x,y);
nodeColor[startGraph]=Color.gray;
}
else
deletenode= true;
}
}
else if (lineHit(x, y))
{
System.out.println("Line, I am clicked!!");
if(evt.clickCount==2)
{
String ret = JOptionPane.showInputDialog("How many demention of your algorithm");
try
{
int numPoints = Integer.parseInt( ret );
weight[nodeI][nodeJ]=numPoints;
System.out.println("the weight of line between node :"+nodeI+" and node : "+nodeJ+" is "+numPoints);
repaint();
}
catch(NumberFormatException m)
{
JOptionPane.showMessageDialog(null,"Please Enter an Integer number for the weight of this line!","ERROR MESSAGE",JOptionPane.ERROR_MESSAGE);
}
}
}
else if (nodehit(x, y, NODESIZE))
{
// draw a new arrow
if (!newarrow)
{
newarrow = true;
thispoint = new Point(x, y);
node1 = hitNode;
}
}
else if ( !nodehit(x, y, 50) && !lineHit(x, y) )
{
// draw new node
if (deleteNodes==0)
{
// take the next available spot in the array
if (numOfNodes < MAXNODES)
node[numOfNodes++]=new Point(x, y);
//else parent.documentation.doctext.showline("maxnodes");
}
else
{
// take an empty spot in the array (from previously deleted node)
int i;
for (i=0;i<numOfNodes;i++)
if (node[i].x==-100) break;
node[i]=new Point(x, y);
deleteNodes--;
}
}
else
// mouseclick to close to a point or arrowhead, so probably an error
// parent.documentation.doctext.showline("toclose");
repaint();
} return true;
}
public boolean mouseDrag(Event evt, int x, int y)
{
if(lock)
{
btObject.comment.setText("The panel is locked!!");
//JOptionPane.showMessageDialog(null,"The panel is locked", JOptionPane.ERROR_MESSAGE);
} else
{
if (movenode)
{
// move node and adjust arrows coming into/outof the node
node[node1]=new Point(x, y);
for (int i=0;i<numOfNodes;i++)
{
if (weight[i][node1]>0)
{
arrowupdate(i, node1, weight[i][node1]);
}
if (weight[node1][i]>0)
{
arrowupdate(node1, i, weight[node1][i]);
}
}
repaint();
}
else if (movestart || newarrow)
{
thispoint = new Point(x, y);
repaint();
}
}
return true;
}
class ImgCanvas extends Canvas implements Runnable
{ final static int MAXNODES = 20;
final static int MAX = MAXNODES+1;
final int NODESIZE = 20;
final int NODERADIX = 10; Buttons btObject = new Buttons();
// 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 dirX[][] = new float[MAX][MAX]; // direction of arrow
float dirY[][] = new float[MAX][MAX]; // direction of arrow Color nodeColor [] = new Color[MAX]; Point thispoint=new Point(0,0); // current mouseposition
Point oldpoint=new Point(0, 0); // previous position of node being moved // current action
boolean newarrow = false;
boolean movearrow = false;
boolean movestart = false;
boolean deletenode = false;
boolean movenode = false;
boolean performalg = false;
boolean clicked = false; int stringPointX;
int stringPointY; int numOfNodes = 0;
int deleteNodes = 0;
int startGraph = 0;
int hitNode; // mouse clicked on or close to this node
int hitLine; // mouse clicked on or close to this line
int node1, node2; // numbers of nodes involved in current action
int nodeI,nodeJ;
// fonts
Font roman= new Font("TimesRoman", Font.BOLD, 12);
Font helvetica= new Font("Helvetica", Font.BOLD, 15);
FontMetrics fmetrics = getFontMetrics(roman);
int h = (int)fmetrics.getHeight()/3; // for double buffering
private Image offScreenImage;
private Graphics offScreenGraphics;
private Dimension offScreenSize; int mindis, minnode,minstart, minend; int algIndex; NewAlgorithm parent; Thread runThread; Dijkstra dijkstra;
static boolean traEdge[][]=new boolean[MAX][MAX];
static int traDistance[]=new int[MAX];
static int traLastDistance[]=new int[MAX];
static boolean traChanged[]=new boolean[MAX];
static int traMinDistance = 0, traMinStart = 0, traMinEnd = 0;
static int traNumChanged = 0, traStep = 0;
static boolean lock = false; public void init()
{
for (int i=0;i<MAXNODES;i++)
{
nodeColor[i]=Color.lightGray;
}
nodeColor[startGraph]=Color.green;
performalg = false; }
public boolean nodehit(double x, double y, int dist)
{
// checks if you hit a node with your mouseclick
for (int i=0; i<numOfNodes; i++)
if ( (x-node[i].x)*(x-node[i].x) + (y-node[i].y)*(y-node[i].y) < dist*dist )
{
hitNode = i;
return true;
}
return false;
}
ImgCanvas(NewAlgorithm parentIn)
{
parent = parentIn;
init();
algIndex=btObject.algSelect.getSelectedIndex(); setBackground(Color.white);
} public void run()
{ } public boolean mouseDown(Event evt, int x, int y)
{
if(lock)
{
btObject.comment.setText("The panel is locked!!");
//JOptionPane.showMessageDialog(null,"The panel is locked", JOptionPane.ERROR_MESSAGE);
} else
{
if (evt.shiftDown())
{
// move a node
if (nodehit(x, y, NODESIZE))
{
oldpoint = node[hitNode];
node1 = hitNode;
movenode=true;
}
}
else if (evt.controlDown())
{
// delete a node
if (nodehit(x, y, NODESIZE))
{
node1 = hitNode;
if (startGraph==node1)
{
movestart=true;
thispoint = new Point(x,y);
nodeColor[startGraph]=Color.gray;
}
else
deletenode= true;
}
}
else if (lineHit(x, y))
{
System.out.println("Line, I am clicked!!");
if(evt.clickCount==2)
{
String ret = JOptionPane.showInputDialog("How many demention of your algorithm");
try
{
int numPoints = Integer.parseInt( ret );
weight[nodeI][nodeJ]=numPoints;
System.out.println("the weight of line between node :"+nodeI+" and node : "+nodeJ+" is "+numPoints);
repaint();
}
catch(NumberFormatException m)
{
JOptionPane.showMessageDialog(null,"Please Enter an Integer number for the weight of this line!","ERROR MESSAGE",JOptionPane.ERROR_MESSAGE);
}
}
}
else if (nodehit(x, y, NODESIZE))
{
// draw a new arrow
if (!newarrow)
{
newarrow = true;
thispoint = new Point(x, y);
node1 = hitNode;
}
}
else if ( !nodehit(x, y, 50) && !lineHit(x, y) )
{
// draw new node
if (deleteNodes==0)
{
// take the next available spot in the array
if (numOfNodes < MAXNODES)
node[numOfNodes++]=new Point(x, y);
//else parent.documentation.doctext.showline("maxnodes");
}
else
{
// take an empty spot in the array (from previously deleted node)
int i;
for (i=0;i<numOfNodes;i++)
if (node[i].x==-100) break;
node[i]=new Point(x, y);
deleteNodes--;
}
}
else
// mouseclick to close to a point or arrowhead, so probably an error
// parent.documentation.doctext.showline("toclose");
repaint();
} return true;
}
public boolean mouseDrag(Event evt, int x, int y)
{
if(lock)
{
btObject.comment.setText("The panel is locked!!");
//JOptionPane.showMessageDialog(null,"The panel is locked", JOptionPane.ERROR_MESSAGE);
} else
{
if (movenode)
{
// move node and adjust arrows coming into/outof the node
node[node1]=new Point(x, y);
for (int i=0;i<numOfNodes;i++)
{
if (weight[i][node1]>0)
{
arrowupdate(i, node1, weight[i][node1]);
}
if (weight[node1][i]>0)
{
arrowupdate(node1, i, weight[node1][i]);
}
}
repaint();
}
else if (movestart || newarrow)
{
thispoint = new Point(x, y);
repaint();
}
}
return true;
}
解决方案 »
- java的wait 吊起线程的时候 会不会释放synchronized锁呢
- 用java转换xmlt结果跟用Microsoft.XMLDOM结果不同。而后者是我预期的。不知何故?
- 还是错误!
- 想用JB写一个mysql的连接池,在线等
- bean怎么就这么难么?望指点 100分求助!
- 请大家帮我找错。。。
- applet之间的通讯问题???
- 困惑很久的问题,一个实体BEAN只能对应一个TABLE吗?如果有几百个TABLE,难道要写几百个实体BEAN?
- 简单问题在线给分:怎样将一个byte[]的值拷贝给另一个byte[]
- 今天问题特别多,弱啊~~~向大家讨教~
- 文件通过编译,但无法运行!各位老大帮我!!!!万分感谢,
- Java Examples in a Nutshell(java实例技术手册)谁有Ebook吗?
{
if(lock)
{
btObject.comment.setText("The panel is locked!!");
//JOptionPane.showMessageDialog(null,"The panel is locked", JOptionPane.ERROR_MESSAGE);
} else
{ if (movenode)
{
// move the node if the new position is not to close to
// another node or outside of the panel
node[node1]=new Point(0, 0);
if ( nodehit(x, y, 50) || (x<0) || (x>this.size().width) ||
(y<0) || (y>this.size().height) )
{
node[node1]=oldpoint;
// parent.documentation.doctext.showline("toclose");
}
else node[node1]=new Point(x, y);
for (int i=0;i<numOfNodes;i++)
{
if (weight[i][node1]>0)
arrowupdate(i, node1, weight[i][node1]);
if (weight[node1][i]>0)
arrowupdate(node1, i, weight[node1][i]);
}
movenode=false;
}
else if (deletenode)
{
nodedelete();
deletenode=false;
}
else if (newarrow)
{
newarrow = false;
if (nodehit(x, y, NODESIZE))
{
node2=hitNode;
if (node1!=node2)
{
arrowupdate(node1, node2, 50);
System.out.println("I set the weight of this line (node "+node1+" and node "+node2+") to 50");
if (weight[node2][node1]>0)
{
arrowupdate(node2, node1, weight[node2][node1]);
}
//parent.documentation.doctext.showline("change weights");
}
else System.out.println("zelfde");
}
}
else if (movearrow)
{
movearrow = false;
/* if (weight[node1][node2]>0)
changeweight(x, y);*/
}
else if (movestart)
{
// if new position is a node, this node becomes the startnode
if (nodehit(x, y, NODESIZE))
startGraph=hitNode;
nodeColor[startGraph]=Color.blue;
movestart=false;
}
repaint();
} return true;
}
public void nodedelete()
{
// delete a node and the arrows coming into/outof the node
node[node1]=new Point(-100, -100);
for (int j=0;j<numOfNodes;j++)
{
weight[node1][j]=0;
weight[j][node1]=0;
}
deleteNodes++;
}
public void arrowupdate(int p1, int p2, int w)
{
// make a new arrow from node p1 to p2 with weight w, or change
// the weight of the existing arrow to w, calculate the resulting
// position of the arrowhead
int dx, dy;
float l;
weight[p1][p2]=w; // direction line between p1 and p2
dx = node[p2].x-node[p1].x;
dy = node[p2].y-node[p1].y; // distance between p1 and p2
l = (float)( Math.sqrt((float)(dx*dx + dy*dy)));
dirX[p1][p2]=dx/l;
dirY[p1][p2]=dy/l; // calculate the start and endpoints of the arrow,
// adjust startpoints if there also is an arrow from p2 to p1
if (weight[p2][p1]>0)
{
startp[p1][p2] = new Point((int)(node[p1].x-5*dirY[p1][p2]),
(int)(node[p1].y+5*dirX[p1][p2]));
endp[p1][p2] = new Point((int)(node[p2].x-5*dirY[p1][p2]),
(int)(node[p2].y+5*dirX[p1][p2]));
}
else
{
startp[p1][p2] = new Point(node[p1].x, node[p1].y);
endp[p1][p2] = new Point(node[p2].x, node[p2].y);
} // range for arrowhead is not all the way to the start/endpoints
int diff_x = (int)(Math.abs(20*dirX[p1][p2]));
int diff_y = (int)(Math.abs(20*dirY[p1][p2])); // calculate new x-position arrowhead
if (startp[p1][p2].x>endp[p1][p2].x)
{
arrow[p1][p2] = new Point(endp[p1][p2].x + diff_x +
(Math.abs(endp[p1][p2].x-startp[p1][p2].x) - 2*diff_x )*
(100-w)/100 , 0); }
else
{
arrow[p1][p2] = new Point(startp[p1][p2].x + diff_x +
(Math.abs(endp[p1][p2].x-startp[p1][p2].x) - 2*diff_x )*
w/100, 0);
} // calculate new y-position arrowhead
if (startp[p1][p2].y>endp[p1][p2].y)
{
arrow[p1][p2].y=endp[p1][p2].y + diff_y +
(Math.abs(endp[p1][p2].y-startp[p1][p2].y) -
2*diff_y )*
(100-w)/100;
}
else
{
arrow[p1][p2].y=startp[p1][p2].y + diff_y +
(Math.abs(endp[p1][p2].y-startp[p1][p2].y) -
2*diff_y )*
w/100;
}
}
{
for(int i=0;i<numOfNodes;i++)
for(int j=0;j<numOfNodes;j++)
{
double x1,x2,y1,y2,k;
x1=node[i].x;
x2=node[j].x;
y1=node[i].y;
y2=node[j].y; double a=Math.sqrt((x1-x2)*(x1-x2)+
(y1-y2)*(y1-y2));
double c=Math.abs((y2-y1)*x-(x2-x1)*y+(x2-x1)*y1-(y2-y1)*x1); double b=c/a; System.out.println("b is :"+b);
if((weight[i][j]>0)&&(b<=3))
{
if((x>Math.min(x1,x2))&&(x<Math.max(x1,x2))&&((y>Math.min(y1,y2))&&(y<Math.max(y1,y2))))
{
nodeI=i;
nodeJ=j;
return true;
} }
} return false;
}
public String intToString(int i)
{
char c=(char)((int)'a'+i);
return ""+c;
} public String intToInt(int i)
{
String s = ""+i;
return s;
}
public final synchronized void update(Graphics g)
{
// prepare new image offscreen
Dimension d=size();
if ((offScreenImage == null) || (d.width != offScreenSize.width)
||
(d.height != offScreenSize.height))
{
offScreenImage = createImage(d.width, d.height);
offScreenSize = d;
offScreenGraphics = offScreenImage.getGraphics();
}
offScreenGraphics.setColor(Color.white);
offScreenGraphics.fillRect(0, 0, d.width, d.height);
paint(offScreenGraphics);
g.drawImage(offScreenImage, 0, 0, null);
} public void paint(Graphics g)
{
/* mindist=0;
minnode=MAXNODES;
minstart=MAXNODES;
minend=MAXNODES;
for(int i=0; i<MAXNODES; i++)
changed[i]=false;
numchanged=0;
neighbours=0; */
g.setFont(roman);
g.setColor(Color.blue);
/* if (step==1)
showstring="Algorithm running: red arrows point to nodes
reachable from " +
" the startnode.\nThe distance to: ";
else
showstring="Step " + step + ": Red arrows point to nodes
reachable from " +
"nodes that already have a final distance." +
"\nThe distance to: "; */ // draw a new arrow upto current mouse position
if (newarrow)
g.drawLine(node[node1].x, node[node1].y, thispoint.x, thispoint.y); // draw all arrows
for (int i=0; i<numOfNodes; i++)
{
for (int j=0; j<numOfNodes; j++)
{
if (weight [i][j]>0)
{
// if algorithm is running then perform next step for this arrow
if (performalg)
{
detailsalg(g, i, j);
if((dijkstra.changed[i])&&(Dijkstra.jColorRed))
nodeColor[j] = Color.red;
}
drawarrow(g, i, j); int x1,x2,y1,y2,k;
x1=node[i].x;
x2=node[j].x;
y1=node[i].y;
y2=node[j].y; int t=x1+x2;
int m=y1+y2; if(x1==x2)
{
stringPointX=x1+10;
stringPointY=m/2;
}
else if(y1==y2)
{
stringPointX=t/2;
stringPointY=y1+10;
}
else
{ stringPointX = (int)(14/(Math.sqrt(1+(t*t)/(m*m)))+t/2);
stringPointY = (int)(14/(Math.sqrt(1+(m*m)/(t*t)))+m/2); }
g.setColor(Color.blue);
g.drawString(intToInt(weight[i][j]), stringPointX, stringPointY);
} }
}
// if arrowhead has been dragged to 0, draw it anyway, so the user
// will have the option to make it positive again
if (movearrow && weight[node1][node2]==0)
{
drawarrow(g, node1, node2);
g.drawLine(startp[node1][node2].x, startp[node1][node2].y,
endp[node1][node2].x, endp[node1][node2].y);
} // draw the nodes
for (int i=0; i<numOfNodes; i++)
if (node[i].x>0)
{
g.setColor(nodeColor[i]);
g.fillOval(node[i].x-NODERADIX, node[i].y-NODERADIX,NODESIZE, NODESIZE);
} // reflect the startnode being moved
g.setColor(Color.green);
if (movestart)
g.fillOval(thispoint.x-NODERADIX, thispoint.y-NODERADIX,
NODESIZE, NODESIZE);
g.setColor(Color.black);
// finish this step of the algorithm
if (performalg) endstepalg(g); // draw black circles around nodes, write their names to the screen
g.setFont(helvetica);
for (int i=0; i<numOfNodes; i++)
if (node[i].x>0)
{
g.setColor(Color.white);
g.drawOval(node[i].x-NODERADIX, node[i].y-NODERADIX,
NODESIZE, NODESIZE);
g.setColor(Color.blue);
g.drawString(intToString(i), node[i].x-14, node[i].y-14);
}
}
public void clear()
{
// removes graph from screen
startGraph=0;
numOfNodes=0;
deleteNodes=0;
init();
for(int i=0; i<MAXNODES; i++)
{
node[i]=new Point(0, 0);
for (int j=0; j<MAXNODES;j++)
weight[i][j]=0;
}
// if (algrthm != null) algrthm.stop();
// parent.unlock();
repaint();
lock = false;
} public void drawarrow(Graphics g,int i, int j)
{
int x1 = startp[i][j].x;
int x2 = endp[i][j].x;
int y1 = startp[i][j].y;
int y2 = endp[i][j].y;
int x3 = 0,x4 = 0,y3 = 0,y4 = 0;
((Graphics2D) g).setStroke(new BasicStroke(3.0f));
if (traEdge[i][j])
{
g.setColor(Color.yellow);
nodeColor[traMinEnd]=Color.yellow;
}
g.drawLine(x1,y1,x2,y2);
double s = 10.0;
double h = 10.0;
double a = 5.0;
double diffX = x2-x1;
double diffY = y2-y1;
double distance = Math.sqrt(diffY*diffY+diffX*diffX);
x2 = (int)(x2-(diffX*s)/distance);
y2 = (int)(y2-(diffY*s)/distance);
double midX = x2-(diffX*h)/distance;
double midY = y2-(diffY*h)/distance;
x3 = (int)(midX+((Math.abs(diffY))*a)/distance);
x4 = (int)((midX-((Math.abs(diffY))*a)/distance));
if(y2>=y1)
{
y3 = (int)(midY-(diffX*a)/distance);
y4 = (int)(midY+(diffX*a)/distance);
}
if(y2<y1)
{
y3 = (int)(midY+(diffX*a)/distance);
y4 = (int)(midY-(diffX*a)/distance);
}
g.drawLine(x2,y2,x3,y3);
g.drawLine(x2,y2,x4,y4);
} private double distance(double x1,double y1,double x2, double y2)
{
return Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); }
{
initAlgorithm();
performalg=true;
runThread=new Thread(this);
runThread.start();
} public void stepOfAlgorithm()
{
System.out.println("traMinEnd = " + traMinEnd);
lock = true;
initAlgorithm();
performalg=true;
dijkstra.step();
repaint();
//nodeColor[traMinEnd]=Color.yellow;
} public void initAlgorithm()
{
// SecondClass second = new SecondClass(testArray.weight, testArray.MAX); dijkstra = new Dijkstra(weight,numOfNodes);
init();
if(traStep==0)
dijkstra.init();
else
dijkstra.init2();
}
public void detailsalg(Graphics g, int i, int j)
{
algIndex=parent.buttons.algSelect.getSelectedIndex();
// System.out.println("Algorithm Index is :"+algIndex);
// more algorithms can be added later
if (algIndex==0)
{
dijkstra.processDijkstra(g, i, j);
dijkstra.saveData();
//if(Dijkstra.jColorRed==true)
// nodeColor[j]=Color.red;
}
}
public void endstepalg(Graphics g)
{
// more algorithms can be added later
if (algIndex==0)
// dijkstra.endstepDijkstra(g); for (int i=0; i<numOfNodes; i++)
if ( (node[i].x>0) && (dijkstra.distance[i]!=-1) )
{
String str = new String(""+dijkstra.distance[i]);
g.drawString(str, node[i].x - (int)fmetrics.stringWidth(str)/2 -1,
node[i].y + h);
// string to distance to nodes on the documentation panel
if (dijkstra.lastDistance[i]==-1)
{
dijkstra.neighbours++;
if (dijkstra.neighbours!=1)
{
}// showstring = showstring + ", ";
//showstring = showstring + intToString(i) +"=" + dist[i];
}
}
}}class Dijkstra
{ final int MAXNODES = 20;
final int MAX = MAXNODES+1;
int numNodesDijkstra=0;
boolean edge[][]=new boolean[MAX][MAX];
int distance[]=new int[MAX];
int lastDistance[]=new int[MAX];
boolean changed[]=new boolean[MAX];
static boolean jColorRed=false; int weight[][]; int numChanged =0;
int neighbours=0; int step=0; int minDistance, minNode, minStart, minEnd; ImgCanvas img;
Dijkstra()
{ } Dijkstra(int weightIn[][], int numOfNodesIn)
{
numNodesDijkstra = numOfNodesIn;
weight= new int[numNodesDijkstra][numNodesDijkstra];
System.out.println("I recieve the numOfNodes is :"+ numNodesDijkstra);
for(int i=0; i<numNodesDijkstra; i++)
for(int j=0; j<numNodesDijkstra; j++)
{
weight[i][j]=weightIn[i][j];
}
changed[0] = true;
} public void init()
{
for(int i=0;i<MAXNODES;i++)
{
distance[i]=-1;
lastDistance[i]=-1;
for(int j=0;j<MAXNODES;j++)
{
edge[i][j]=false;
}
distance[0]=0;
lastDistance[0]=0;
step=ImgCanvas.traStep;
minDistance=ImgCanvas.traMinDistance;
minStart=ImgCanvas.traMinStart;
minEnd=ImgCanvas.traMinEnd ;
}
}
public void init2()
{
for(int i=0;i<MAXNODES;i++)
{
distance[i]=ImgCanvas.traDistance[i];
lastDistance[i]=ImgCanvas.traLastDistance[i];
changed[i]=ImgCanvas.traChanged[i];
for(int j=0;j<MAXNODES;j++)
{
edge[i][j]=ImgCanvas.traEdge[i][j];
}
}
step=ImgCanvas.traStep;
minDistance=ImgCanvas.traMinDistance;
minStart=ImgCanvas.traMinStart;
minEnd=ImgCanvas.traMinEnd;
numChanged = ImgCanvas.traNumChanged;
}
public void saveData()
{
for(int i=0;i<MAXNODES;i++)
{
ImgCanvas.traDistance[i] = distance[i];
ImgCanvas.traLastDistance[i] = lastDistance[i];
ImgCanvas.traChanged[i] = changed[i];
for(int j=0;j<MAXNODES;j++)
{
ImgCanvas.traEdge[i][j] = edge[i][j];
}
}
ImgCanvas.traStep = step;
ImgCanvas.traMinDistance = minDistance;
ImgCanvas.traMinStart = minStart;
ImgCanvas.traMinEnd = minEnd;
ImgCanvas.traNumChanged = numChanged;
} public void step()
{
lastDistance[minEnd]=minDistance;
edge[minStart][minEnd]=true;
//img.nodeColor[minEnd]=Color.pink;
step = ImgCanvas.traStep;
step++;
ImgCanvas.traStep = step;
System.out.println("This step is :"+step);
// img.repaint();
}
public void stop()
{
if(img.runThread!=null)
img.runThread.suspend();
} public void run()
{
//for(int i=0;i<(img.numOfNodes-img.deleteNodes);i++)
for(int i=0;i<numNodesDijkstra;i++)
{
step();
try
{
img.runThread.sleep(3000); }
catch(InterruptedException ire)
{ }
}
img.runThread=null; } public void processDijkstra(Graphics g, int i, int j)
{
if((lastDistance[i]!=-1)&&(lastDistance[j])==-1)
{
g.setColor(Color.red);
if((distance[j]==-1)||(distance[j]>=(distance[i]+weight[i][j])))
{
if((distance[i]+weight[i][j])<distance[j])
{
changed[j]=true;
numChanged = ImgCanvas.traNumChanged;
numChanged++;
ImgCanvas.traNumChanged = numChanged;
}
distance[j]=distance[i]+weight[i][j];
// img.nodeColor[j]=Color.red;
jColorRed = true;
if((minDistance==0)||(distance[j]<minDistance))
{
minDistance=distance[j];
minStart=i;
minEnd=j;
System.out.println("The minDistance is "+ minDistance+
"The minStart is "+ minStart+
"The minEnd is "+ minEnd);
ImgCanvas.traMinDistance = minDistance;
ImgCanvas.traMinStart = minStart;
ImgCanvas.traMinEnd = minEnd;
}
}
}
else
g.setColor(Color.gray);
}
}