-
Urgent! Java applet.
hi every one, i've been working on a java applet which display a map, with land mark and tourist spots. i got all the layout done. however, i got one more problem....
i got a method which compute the shortest path out of ten cities and return the best solution. i test the algorthm in dos at it give perfect result, however, when i tried to implement in the applet, it won't display the out put, can one of u guys tell me why?
here's the complete source code, i hope is not too messy.
Code:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.awt.geom.*;
import java.lang.*;
import java.util.Stack;
import java.util.ArrayList;
import java.util.Iterator;
import java.lang.String;
/*
<applet code="Maps" width=900 height=900>
</applet>
*/
// Applet window.
public class Maps extends Applet
implements MouseListener, MouseMotionListener {
final Font label= new Font("SansSerif",Font.BOLD,13);
final Font pic= new Font("SanSerif",Font.BOLD,18);
int mouseX=0, mouseY=10;
int movX=0, movY=0;
int spotX[]={360,520,600,460,565,565,620,340,180,150,440};
int spotY[]={420,40,440,370,40,75,120,195,100,160,270};
int lmX[]={495,480,540,600,590,560};
int lmY[]={40,40,75,40,70,380};
int sC=20;
int startingCity;
Image img;
Image imgNew;
DistanceTable dt = DistanceTable.getDefault();
public void init() {
setBackground(Color.cyan);
setForeground(Color.red);
// register this object to receive its own mouse events
addMouseListener(this);
addMouseMotionListener(this);
}
public void mouseClicked(MouseEvent me) {
mouseX=me.getX();
mouseY=me.getY();
}
public void mouseEntered(MouseEvent me) {
}
public void mouseExited(MouseEvent me) {
}
public void mousePressed(MouseEvent me) {
}
public void mouseReleased(MouseEvent me) {
}
public void mouseDragged(MouseEvent me) {
// save coordinates
}
// Handle mouse moved.
public void mouseMoved(MouseEvent me) {
// save coordinates
movX = me.getX();
movY = me.getY();
repaint(0, 0, 200, 20);
}
//TSP algorithm
class DfsTSPSolver implements TSPSolver{
public Solution[] solutions = new Solution[3]; // 3 best path solutions
double[] solutionsLength=new double[3];// 3 best path solutions length
Stack<Integer> indexStack=new Stack<Integer>(); // Stack to store city indices
private int parent,startingCity;
// Default city level in tree hierarchy
ArrayList<DfsSolution> topThree=new ArrayList<DfsSolution>();
public DfsTSPSolver(int startingCity){ //constructor
this.startingCity=startingCity;
parent=startingCity;
}
public DfsTSPSolver(){ //constructor
this.startingCity=0;
parent=0;
}
public void solve(DistanceTable dt){
int legendSize=dt.size();
// Start tracing the path tree???1
trace(dt,legendSize,1);
// Copy all the topThree Arraylist elements to solutions array
for(int i=0;i<legendSize;i++){
solutions[i]=topThree.get(i);
}
}
public Solution getBestSolution(){
return solutions[0];
}
public Solution[] getTopThreeSolutions(){
return solutions;
}
// Trace the path tree to get all the possible path
private boolean trace(DistanceTable dt, int legendSize,int level){
// If the end of city is visited, add the specific path solution to an arraylist for analysis
if(level==legendSize){
if(!indexStack.empty()){
// Get all the stack values and produce a complete path
ArrayList<Integer> sol=new ArrayList<Integer>();
for(int i=0;i<indexStack.size();i++){
sol.add(indexStack.elementAt(i));
}
sol.add(parent);
sol.add(startingCity);
// Start - Calculate the path total length and sort with the top three solutions.
DfsSolution path=new DfsSolution(dt,sol);// ARRAYLIST
double tempLength=path.getLength();
int size=topThree.size();
int i=0;
Iterator it=topThree.iterator();
while(true){
if(it.hasNext()){
Solution a=(Solution)it.next();
if(tempLength<a.getLength()){
topThree.add(i,path);
if(size==legendSize)
topThree.remove(legendSize);
break;
}
i++;
}
else if(size<legendSize){
topThree.add(path);
break;
}
else
break;
}
// End - Calculation and Sorting
}
return true;
}
// Push the parent level legend index when moving to the child level
indexStack.push(parent);
for(int i=0;i<legendSize;i++){
if(i==startingCity)
continue;
if(indexStack.search(i)<0 && parent!=i ){
// If the legend index is not inside the stack
parent=i;
trace(dt,legendSize,level+1);
}
}
// pop out the top stack value after each level
parent =indexStack.pop();
return true;
}
}
class DfsSolution implements Solution{
private double length = 0.0;
private String solution = "";
DfsSolution(DistanceTable dt, ArrayList sol){
// Get the specific path solution string and length
Integer legendIndexFrom,legendIndexTo;
for(int i=0;i<sol.size();i++){
legendIndexTo=(Integer)sol.get(i);
if(i!=0){
legendIndexFrom=(Integer)sol.get(i-1);
this.length+=dt.getDistance(legendIndexFrom,legendIndexTo);
this.solution+="->";
}
this.solution+=dt.getLegend(legendIndexTo.intValue());
}
}
public String toString(){
return solution;
}
public double getLength(){
return length;
}
}
// Display msg in applet window.
public void paint(Graphics g) {
Graphics solution=(Graphics)g;
solution.setFont(label);
solution.drawString("sC: "+ sC,0,30);
String msg="";
if((sC<10)==true){
startingCity=sC;
solution.drawString("starting: "+ startingCity,0,40);
DistanceTable dt = DistanceTable.getDefault();
DfsTSPSolver solver = new DfsTSPSolver(startingCity);
solver.solve(dt);
solution.drawString("Best Solution: "+ solver.getBestSolution(),0,50);
}
i excluded the 2D graphic code and the event code. in the code which i highlighted, there actually a algorthm that set the value sC from 0-9 when user clikc on the tourist spots. please tell why why the" solution.drawString("Best Solution: "+ solver.getBestSolution(),0,50)" won't work?
Similar Threads
-
By SvenLittkowski in forum Java
Replies: 3
Last Post: 09-22-2006, 02:19 PM
-
By crash75uk in forum Java
Replies: 1
Last Post: 04-20-2005, 03:32 PM
-
Replies: 1
Last Post: 02-20-2001, 01:47 AM
-
By NAGARAJA UPADYAYA in forum Java
Replies: 1
Last Post: 06-19-2000, 01:22 PM
-
By Mike Buchanan in forum Java
Replies: 0
Last Post: 04-12-2000, 09:53 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks