0

I am trying to approximate a solution of the Traveling Salesman Problem with a Hilbert Curve program. I have to do it by using an applet. How would I add the Matrix in my code, and how would I display the coordinates in the Applet. I don't need to have more than one frame.

The code is below:

import java.awt.*;
import java.applet.*;   

// Background images (put a map in the back of the applet)

public class HilbertCurve extends Applet{
private SimpleGraphics sg = null;
private final int dist0=512;
private int dist=dist0;


@Override
public void init(){
    resize(dist0, dist0 +300);
    sg = new SimpleGraphics(getGraphics());
}

@Override
public void paint(Graphics g){
    int level = 5;
    dist = dist0;
    for(int i=level; i>0; i--) dist/=2;
    sg.goToXY (dist/2, dist/2);
    HilbertU(level);
}

private void HilbertU(int level){
    if(level>0){
        HilbertD(level-1); sg.lineRel(0,dist);
        HilbertU(level-1); sg.lineRel(dist,0);
        HilbertU(level-1); sg.lineRel(0,-dist);
        HilbertC(level-1);
    }
}

private void HilbertD(int level){
    if(level>0){
        HilbertU(level-1); sg.lineRel(dist,0);
        HilbertD(level-1); sg.lineRel(0,dist);
        HilbertD(level-1); sg.lineRel(-dist,0);
        HilbertA(level-1);
    }
}
private void HilbertC(int level){
    if(level>0){
        HilbertA(level-1); sg.lineRel(-dist,0);
        HilbertC(level-1); sg.lineRel(0,-dist);
        HilbertC(level-1); sg.lineRel(dist,0);
        HilbertU(level-1);
    }
}
private void HilbertA(int level){
    if(level>0){
        HilbertC(level-1); sg.lineRel(0,-dist);
        HilbertA(level-1); sg.lineRel(-dist,0);
        HilbertA(level-1); sg.lineRel(0,dist);
        HilbertD(level-1);
    }
}
}

I need to set cities in the matrix, and then when I reach a city to draw a line between the cities, in order to find the shortest path to reach it.

  • 1
    *I have to do it by using an applet.* Applets are **dead**. Ask for your tuition money back. In the last [five years](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). it has [accelerated](https://blogs.oracle.com/java-platform-group/moving-to-a-plugin-free-web) and now I think it's safe to declare them dead. – Elliott Frisch Feb 26 '18 at 00:04
  • 1
    Seriously - Ask for your tuition money back. Anyone teaching Applets should be seriously questioned about their experience. – Scary Wombat Feb 26 '18 at 00:13
  • Yeah, I know applets are terrible, but I have to do it this way :/ – Ian Tupiara Feb 26 '18 at 00:56
  • That's some terrible comments. – Micromega Sep 20 '18 at 09:04

0 Answers0