package clientGUI;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import map.Node;
import search_algorithm.HeuristicSearch;
public class ClientGUI extends JFrame implements ActionListener{
private Node[][] map;
private Node start;
private Node end;
private NodeView[][] mapView;
private JButton buttonNext;
private HeuristicSearch heuristicSearch;
public ClientGUI() {
initMap();
System.out.println(" "+start);
heuristicSearch = new HeuristicSearch(map, start, end);
buttonNext = new JButton("Next");
buttonNext.addActionListener(this);
int x = map.length;
int y = map[0].length;
setLayout(new BorderLayout());
setTitle("+7nb");
JPanel mapPanel = new JPanel(new GridLayout(x,y,2,2));
add(mapPanel,BorderLayout.NORTH);
add(buttonNext,BorderLayout.SOUTH);
mapView = new NodeView[x][y];
for(int i=0; i<x; i++) {
for(int j=0; j<y; j++) {
NodeView nodeView = new NodeView();
mapView[i][j] = nodeView;
mapPanel.add(nodeView);
}
}
update();
// 设置窗口
int windowLength = y*50+(y-1)*2;
int windowWidth = x*50+(y-1)*2+100;
setSize(windowLength,windowWidth);
setVisible(true);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/**
* 初始化地图
*/
public void initMap() {
// 地图
map = new Node[9][9];
for(int i = 0;i<9;i++) {
for(int j = 0;j<9;j++) {
Node node = new Node(i,j);
map[i][j] = node;
}
}
Node node1 = new Node(Node.WALL,4,1);
map[4][1] = node1;
Node node2 = new Node(Node.WALL,2,2);
map[2][2] = node2;
Node node3 = new Node(Node.WALL,3,2);
map[3][2] = node3;
Node node4 = new Node(Node.WALL,4,2);
map[4][2] = node4;
Node node5 = new Node(Node.WALL,5,2);
map[5][2] = node5;
Node node6 = new Node(Node.WALL,2,3);
map[2][3] = node6;
Node node7 = new Node(Node.WALL,5,3);
map[5][3] = node7;
Node node8 = new Node(Node.WALL,2,4);
map[2][4] = node8;
Node node9 = new Node(Node.WALL,2,5);
map[2][5] = node9;
Node node0 = new Node(Node.WALL,2,6);
map[2][6] = node0;
start = new Node(Node.START,3,1,0,0);
map[3][1] = start;
end = new Node(Node.END,3,3);
map[3][3] = end;
}
/**
* 刷新用户图形界面
* @param map
*/
public void update() {
int x = map.length;
int y = map[0].length;
for(int i=0; i<x; i++) {
for(int j=0; j<y; j++) {
Node node = map[i][j];
NodeView nodeView = mapView[i][j];
JLabel textG = nodeView.getTextG();
JLabel textH = nodeView.getTextH();
JLabel textF = nodeView.getTextF();
int type = node.getType();
switch (type) {
case Node.EMPTY:
nodeView.setBackground(Color.WHITE);
textG.setText(" ");
textH.setText(" ");
textF.setText(" ");
break;
case Node.START:
nodeView.setBackground(Color.BLUE);
textG.setText(" ");
textH.setText(" ");
textF.setText("A");
break;
case Node.END:
nodeView.setBackground(Color.BLUE);
textG.setText(" ");
textH.setText(" ");
textF.setText("B");
break;
case Node.PATH:
nodeView.setBackground(Color.BLUE);
textG.setText(" "+node.getG());
textH.setText(node.getH()+" ");
textF.setText(node.getF()+"");
break;
case Node.WALL:
nodeView.setBackground(Color.BLACK);
textG.setText(" ");
textH.setText(" ");
textF.setText(" ");
break;
case Node.OPEN:
nodeView.setBackground(Color.GREEN);
textG.setText(" "+node.getG());
textH.setText(node.getH()+" ");
textF.setText(node.getF()+"");
break;
case Node.CLOSED:
nodeView.setBackground(Color.RED);
textG.setText(" "+node.getG());
textH.setText(node.getH()+" ");
textF.setText(node.getF()+"");
break;
default:
break;
}
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
int state = heuristicSearch.execute();
switch (state) {
case HeuristicSearch.CONTINUE:
update();
break;
case HeuristicSearch.SUCESS:
int steps = heuristicSearch.getShortestPath();
buttonNext.setText("Success! Steps:"+steps);
update();
break;
case HeuristicSearch.FAILURE:
update();
buttonNext.setText("failure!");
default:
break;
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
基于A*算法的最优路径搜索
共15个文件
class:6个
java:5个
docx:1个
需积分: 50 18 下载量 13 浏览量
2020-12-14
22:15:14
上传
评论 2
收藏 114KB ZIP 举报
温馨提示
基于A*算法的最优路径搜索实例及程序说明文档。包含基于A*算法的最优路径搜索实例;程序说明文档包含算法设计说明、程序设计说明、类图、流程图
资源推荐
资源详情
资源评论
收起资源包目录
AStarAlgorithm.zip (15个子文件)
程序说明文档.docx 99KB
AStarAlgorithm
src
clientGUI
ClientGUI.java 5KB
NodeView.java 952B
search_algorithm
HeuristicSearch.java 4KB
main
Main.java 225B
map
Node.java 2KB
bin
clientGUI
NodeView.class 1KB
ClientGUI.class 5KB
search_algorithm
HeuristicSearch.class 4KB
HeuristicSearch$1.class 1KB
main
Main.class 453B
map
Node.class 2KB
.classpath 301B
.settings
org.eclipse.jdt.core.prefs 598B
.project 390B
共 15 条
- 1
资源评论
hit光翟
- 粉丝: 2
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功