Hilbert Curve in Applet with Matrix of coordinates

2019-06-09 22:51发布

问题:

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.