📄 face.java
字号:
package shortestpath;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Rectangle;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class Face
extends JFrame implements ActionListener, Runnable {
private static int xPos = 0, yPos = 0, NumberOfCities = 66, len = 0, temp = 0; //取最短路径上两城市间的距离
private static double a = 0, b = 0; //临时变量
private String sta = "", fin = "", through = ""; //sta 代表起点,fin 代表终点,through 表示最短路径
private String[] path = new String[NumberOfCities]; //存储最短路径上的各个城市
private Node node1, node2, nodeTemp, one, two; //node1,node2 分别表示起点和终点城市的信息
private boolean isStart = false, isEnd = false, isTemp = false, done = false;
Container content = getContentPane();
JTextArea jTextArea1 = new JTextArea();
JComboBox jComboBox1 = new JComboBox();
JComboBox jComboBox2 = new JComboBox();
JButton start = new JButton();
JButton end = new JButton();
JLabel jLabel = new JLabel("");
JButton search = new JButton();
JButton select = new JButton();
JRadioButton jRadioButton1 = new JRadioButton(); //第一阶段暂时没用
JRadioButton jRadioButton2 = new JRadioButton();
ButtonGroup bg = new ButtonGroup();
Node[] cities = {
new Node("沈阳", 789, 192), new Node("a", 737, 193),
new Node("秦皇岛", 651, 282),
new Node("b", 634, 265), new Node("北京", 561, 266),
new Node("唐山", 632, 304),
new Node("天津", 597, 301),
new Node("c", 517, 216), new Node("d", 490, 217),
new Node("e", 448, 256),
new Node("f", 448, 207), new Node("呼和浩特", 389, 207),
new Node("临河", 267, 207),
new Node("g", 267, 265),
new Node("银川", 236, 297), new Node("h", 170, 370),
new Node("干唐", 140, 370),
new Node("i", 140, 405), new Node("兰州", 111, 406),
new Node("j", 155, 446),
new Node("天水", 200, 446),
new Node("宝鸡", 232, 482), new Node("阳平关", 232, 541),
new Node("广元", 200, 541),
new Node("成都", 151, 592), new Node("k", 150, 630),
new Node("内江", 173, 651),
new Node("重庆", 220, 651),
new Node("l", 250, 620), new Node("达县", 250, 580),
new Node("安康", 286, 541),
new Node("西安", 305, 482), new Node("洛阳", 418, 482),
new Node("m", 350, 540),
new Node("n", 390, 575),
new Node("襄樊", 418, 575), new Node("郑州", 470, 482),
new Node("o", 500, 445),
new Node("石家庄", 501, 346), new Node("p", 501, 326),
new Node("霸州", 560, 300),
new Node("q", 552, 308),
new Node("商丘", 552, 482), new Node("德州", 597, 360),
new Node("淄博", 651, 390),
new Node("济南", 625, 390), new Node("东营", 651, 347),
new Node("潍坊", 715, 390),
new Node("青岛", 735, 405),
new Node("泰山", 626, 412), new Node("r", 594, 445),
new Node("徐州", 594, 482),
new Node("s", 594, 510), new Node("t", 634, 552),
new Node("u", 650, 608),
new Node("南京", 651, 553),
new Node("镇江", 670, 552), new Node("丹阳", 681, 566),
new Node("常州", 700, 565),
new Node("无锡", 710, 580), new Node("苏州", 728, 580),
new Node("上海", 745, 600),
new Node("杭州", 714, 627),
new Node("芜湖", 675, 608), new Node("黄山", 635, 651),
new Node("武汉", 472, 635),
};
public Face() {
setSize(1024, 738);
setTitle("The ShortestFace Path");
addMouseListener(new MouseClickHandler());
content.setLayout(null);
try {
jbInit();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.red);
//g.drawString("Clicked @ [" + xPos + ", " + yPos + "]", xPos, yPos);
if (isStart) {
g.fillOval(node1.x - 7, node1.y - 5, 14, 14);
}
if (isEnd) {
g.fillOval(node2.x - 7, node2.y - 5, 14, 14);
}
if (isTemp) {
g.setColor(Color.yellow);
g.fillOval(nodeTemp.x - 7, nodeTemp.y - 5, 14, 14);
isTemp = false;
}
g.setColor(Color.red);
if (done) {
for (int i = 0; i < path.length && path[i] != null; i++) {
two = one;
one = getNode(path[i], cities);
if (one != null && two != null) {
a = Math.pow(two.x - one.x, 2);
b = Math.pow(two.y - one.y, 2);
double lenOneToTwo = Math.pow(a + b, 0.5);
double lenToTwo = 0;
while (lenToTwo < lenOneToTwo) {
g.drawOval( (int) (two.x - (two.x - one.x) * lenToTwo / lenOneToTwo),
(int) (two.y - (two.y - one.y) * lenToTwo / lenOneToTwo), 6,
6);
lenToTwo += 5;
try {
Thread.sleep(30);
}
catch (InterruptedException e) {
}
}
len += lenOneToTwo;
}
if (one.city.length() != 1) {
through += " —" + len + "— " + one.city;
len = 0;
}
}
two = one = null;
through = through.substring(5, through.length());
if (through.length() > 80) {
through = through.substring(0, 80) + "\n\t\t " +
through.substring(80, through.length());
}
jTextArea1.setText(" 您选择的起点城市是 " + sta +
"\n 您选择的终点城市是 " + fin +
"\n 最短路径上的城市及相对距离依次为: " + through);
through = "";
}
}
// 处理鼠标点击
private class MouseClickHandler
extends MouseAdapter {
public void mouseClicked(MouseEvent event) {
xPos = event.getX();
yPos = event.getY();
nodeTemp = getNode2(xPos, yPos, cities);
if (nodeTemp != null) {
jTextArea1.setText(" 您选择的起点城市是 " + sta +
"\n 您选择的终点城市是 " + fin +
"\n 您选择的当前城市是:" + nodeTemp.city);
isTemp = true;
}
else {
jTextArea1.setText(" 您选择的起点城市是 " + sta +
"\n 您选择的终点城市是 " + fin +
"\n 您选择的当前城市是 ");
}
repaint();
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jRadioButton1) {
}
else if (e.getSource() == jRadioButton2) {
}
//按下右侧两个按钮后的操作
else if (e.getSource() == start) {
nodeTemp = getNode2(xPos, yPos, cities);
if (nodeTemp != null && nodeTemp.city.length() != 1) {
node1 = nodeTemp;
sta = node1.city;
jComboBox1.setSelectedItem(sta);
jTextArea1.setText(" 您选择的起点城市是 " + sta +
"\n 您选择的终点城市是 " + fin);
isStart = true;
isTemp = false;
repaint();
}
else {
jTextArea1.setText(" 您所选择的城市位置不准确,请重新选择");
}
}
else if (e.getSource() == end) {
nodeTemp = getNode2(xPos, yPos, cities);
if (nodeTemp != null && nodeTemp.city.length() != 1) {
node2 = nodeTemp;
fin = node2.city;
jComboBox2.setSelectedItem(fin);
jTextArea1.setText(" 您选择的起点城市是 " + sta +
"\n 您选择的终点城市是 " + fin);
isEnd = true;
isTemp = false;
repaint();
}
else {
jTextArea1.setText(" 您所选择的城市位置不准确,请重新选择");
}
}
//按下 “查找、重选”按钮后的操作
else if (e.getSource() == search) {
isTemp = false;
if (sta.equals("") || fin.equals("")) {
jTextArea1.setText(" 您未选择起点或终点,请选择");
}
if (sta.equals(fin)) {
jTextArea1.setText(" 起点城市与终点城市相同,距离为零");
}
if (!sta.equals(fin)) {
path = Graph.RoadFinder(sta, fin);
done = true;
repaint();
}
}
else if (e.getSource() == select) {
isStart = isEnd = done = false;
sta = fin = "";
xPos = yPos = 0;
node1 = node2 = one = two = null;
jTextArea1.setText(" Information goes here...");
repaint();
}
// 下拉菜单中选择后的操作
else if (e.getSource() == jComboBox1) { //获取起点城市
sta = ( (JComboBox) e.getSource()).getSelectedItem() + "";
jTextArea1.setText(" 您选择的起点城市是 " + sta +
"\n 您选择的终点城市是 " + fin);
node1 = getNode(sta, cities);
isStart = true;
repaint();
}
else if (e.getSource() == jComboBox2) { //获取终点城市
fin = ( (JComboBox) e.getSource()).getSelectedItem() + "";
jTextArea1.setText(" 您选择的起点城市是 " + sta +
"\n 您选择的终点城市是 " + fin);
node2 = getNode(fin, cities);
isEnd = true;
repaint();
}
}
public void run() {
}
private void jbInit() throws Exception {
jTextArea1.setEditable(false);
jTextArea1.setText(" Information goes here...");
jTextArea1.setBounds(new Rectangle(45, 24, 920, 80));
jComboBox1.setBounds(new Rectangle(873, 293, 92, 33));
for (int i = 0; i < NumberOfCities; i++) {
String s = cities[i].city;
if (s.length() > 1) {
jComboBox1.addItem(cities[i].city);
}
}
jComboBox1.addActionListener(this);
jComboBox2.setBounds(new Rectangle(873, 489, 92, 33));
for (int i = 0; i < NumberOfCities; i++) {
String s = cities[i].city;
if (s.length() > 1) {
jComboBox2.addItem(cities[i].city);
}
}
jComboBox2.addActionListener(this);
start.setBounds(new Rectangle(873, 211, 92, 35));
start.setActionCommand("start");
start.setText("设为起点");
start.addActionListener(this);
end.setBounds(new Rectangle(873, 407, 92, 33));
end.setActionCommand("end");
end.setText("设为终点");
end.addActionListener(this);
jLabel.setBounds(new Rectangle(45, 138, 782, 510));
jLabel.setIcon(new ImageIcon("map.gif"));
search.setBounds(new Rectangle(375, 658, 81, 33));
search.setActionCommand("search");
search.setText("查找");
search.addActionListener(this);
select.setBounds(new Rectangle(537, 658, 81, 33));
select.setActionCommand("select");
select.setText("重选");
select.addActionListener(this);
jRadioButton1.setText("直接从地图上选择城市");
jRadioButton1.setBounds(new Rectangle(298, 113, 155, 23));
jRadioButton1.addActionListener(this);
jRadioButton2.setText("从下拉菜单中选择城市");
jRadioButton2.setBounds(new Rectangle(557, 113, 155, 23));
jRadioButton2.addActionListener(this);
bg.add(jRadioButton1);
bg.add(jRadioButton2);
content.add(search);
content.add(select);
content.add(jRadioButton1);
content.add(jRadioButton2);
content.add(start);
content.add(end);
content.add(jComboBox1);
content.add(jComboBox2);
content.add(jTextArea1);
content.add(jLabel);
}
public static Node getNode(String s, Node[] nod) {
int i;
for (i = 0; i < nod.length; i++) {
if (s.equals(nod[i].city)) {
return nod[i];
}
}
return null;
}
public static Node getNode2(int x0, int y0, Node[] nod) {
int i, lx, ly;
for (i = 0; i < nod.length; i++) {
lx = nod[i].x - 10;
ly = nod[i].y - 10;
if (x0 > lx && (x0 < lx + 20) && y0 > ly && (y0 < ly + 20)) {
return nod[i];
}
}
return null;
}
public static void main(String[] args) {
Face gui = new Face();
gui.setVisible(true);
gui.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
); //end call to addWindowListener
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -