startTime = System.nanoTime(); // 获得新的路线
newList = annealing.newLoop(cityName); endTime = System.nanoTime();
this.showTime(endTime - startTime); // 画线
usOP.setLine(g, cityName, list, newList, startCityName); this.showWayLength(g); line = 0; } }
if (draw == 2) {
cityName = \蚌埠市\
list = usOP.showCity(cityName); usOP.show(g, list, cityName); if (line == 1) {
startTime = System.nanoTime();
newList = annealing.newLoop(cityName); endTime = System.nanoTime();
this.showTime(endTime - startTime);
usOP.setLine(g, cityName, list, newList, startCityName); this.showWayLength(g); line = 0; } }
if (draw == 3) {
cityName = \安庆市\
list = usOP.showCity(cityName); usOP.show(g, list, cityName); if (line == 1) {
startTime = System.nanoTime();
newList = annealing.newLoop(cityName); endTime = System.nanoTime();
this.showTime(endTime - startTime);
usOP.setLine(g, cityName, list, newList, startCityName); this.showWayLength(g); line = 0; } }
if (draw == 4) {
cityName = \芜湖市\
list = usOP.showCity(cityName); usOP.show(g, list, cityName); if (line == 1) {
startTime = System.nanoTime();
newList = annealing.newLoop(cityName); endTime = System.nanoTime();
this.showTime(endTime - startTime);
usOP.setLine(g, cityName, list, newList, startCityName); this.showWayLength(g); line = 0; } }
if (draw == 5) {
cityName = \淮北市\
list = usOP.showCity(cityName); usOP.show(g, list, cityName); if (line == 1) {
startTime = System.nanoTime();
newList = annealing.newLoop(cityName); endTime = System.nanoTime();
this.showTime(endTime - startTime);
usOP.setLine(g, cityName, list, newList, startCityName); this.showWayLength(g); line = 0; } }
if (draw == 6) {
cityName = \阜阳市\
list = usOP.showCity(cityName); usOP.show(g, list, cityName); if (line == 1) {
startTime = System.nanoTime();
newList = annealing.newLoop(cityName); endTime = System.nanoTime();
this.showTime(endTime - startTime);
usOP.setLine(g, cityName, list, newList, startCityName); this.showWayLength(g); line = 0; }} }
// 计算和设置查询所用时间
public void showTime(long a) { float f = a / 1000;
float b = new Float(f / 1000000).floatValue();
time.setText(\查询用时:\秒\}
// 用于显示行使的总路程
}
@SuppressWarnings(\
public void showWayLength(Graphics g) { Info.setText(\查询结束,欢迎使用!\ g.setColor(Color.white);
g.drawString(\总路程: \米\}
3、方法操作
import java.awt.Color; import java.awt.Graphics; import java.util.Iterator; import java.util.List; public class userOP {
private pathInfo paInfo = new pathInfo(); public static int lineOK = 0; // 判断输入的城市是否有效
public boolean testCityName(String cityName, String name) { List
String strName = li.next();
if (name.trim().equals(strName)) { return true;} }
return false; }
// 返回城市列表
public List
return (List
// 显示城市name的名称
public void show(Graphics g, List
Iterator
String city = li.next(); x = a[i][0]; y = a[i][1];
g.setColor(Color.blue);
g.fillArc(x, y, 10, 10, 0, 360); g.setColor(Color.black);
}
}
g.drawString(city, x, y); i++;
if (i == a.length) { break; }
// 获得城市的坐标
public int[][] getZB(String name) { int a[][] = null;
if (name.trim().equals(\合肥市\ a = paInfo.getHeFeiZB(); }
if (name.trim().equals(\蚌埠市\ a = paInfo.getBengBuZB(); }
if (name.trim().equals(\安庆市\ a = paInfo.getAnQingZB(); }
if (name.trim().equals(\芜湖市\ a = paInfo.getWuHuZB(); }
if (name.trim().equals(\淮北市\ a = paInfo.getHuaiBeiZB(); }
if (name.trim().equals(\阜阳市\ a = paInfo.getFuYangZB(); }
return a; }
// 画出序列list的路线
@SuppressWarnings(\
public void setLine(Graphics g, String city, List
int zb[][] = null,i=0, x = 0, y = 0, w = 0, h = 0, stop = 0; int startCityIndex = 0,newListSize=newList.size(); //获得该城市的路径长度
int way[][]=this.getCityWay(city); // 获得原始序列坐标 zb = this.getZB(city);
// 判断起始位置在最佳序列中位置
int indexOfNewList = newList.indexOf(startCity);
// 起始位置在原始序列中的坐标
startCityIndex = S.indexOf(startCity); i = startCityIndex; while (true) { x = zb[i][0]; y = zb[i][1]; startCityIndex=i;
if (indexOfNewList == (newListSize- 1)) { indexOfNewList = 0; } else {
indexOfNewList++; }
String str = newList.get(indexOfNewList); i = S.indexOf(str); w = zb[i][0]; h = zb[i][1];
g.setColor(Color.red); try {
Thread.sleep(1000);
} catch (InterruptedException e) { e.printStackTrace(); }
g.drawLine(x, y, w, h); stop++;
g.setColor(new Color(83, 205, 74));
g.drawString(\ g.setColor(Color.pink);
g.drawString(\相距 \米\(y + h) / 2);
if (stop == newList.size()) { break; } }
lineOK = 1; }
//获得城市间的路径长度
public int[][] getCityWay(String cityName) { int a[][] = null;
if (cityName.trim().equals(\合肥市\ a = paInfo.getHeFeiWay(); }
if (cityName.trim().equals(\蚌埠市\