选择某一类鱼群为例,根据该鱼群的活动路径,设计该鱼群的活动路径平面图,至少包括10个以上的地点,抽象成一个无向带权图(无向图),以图中顶点表示活动地点,边上的权值表示两地点之间的距离。要求程序使用GUI界面,最短路径算法或关键路径算法部分接收GUI部分传来的路径起始点与终止点,通过GUI返回源点到终止点的路径上各点的编号与各条边的权值。通过IO流读取地图数据。
时间: 2024-03-13 18:45:14 浏览: 18
以下是一个简单的Java程序,用于实现上述要求:
```java
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
import javax.swing.*;
public class FishGroupActivityPathGUI extends JFrame {
private JPanel panel;
private JComboBox<String> startLocation;
private JComboBox<String> endLocation;
private JTextArea resultArea;
private JButton findPathButton;
private Map<String, Map<String, Integer>> graph;
public FishGroupActivityPathGUI() {
setTitle("Fish Group Activity Path");
setSize(500, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
graph = new HashMap<>();
try {
Scanner scanner = new Scanner(new File("map.txt"));
while (scanner.hasNextLine()) {
String[] line = scanner.nextLine().split(",");
String location1 = line[0].trim();
String location2 = line[1].trim();
int distance = Integer.parseInt(line[2].trim());
if (!graph.containsKey(location1)) {
graph.put(location1, new HashMap<>());
}
if (!graph.containsKey(location2)) {
graph.put(location2, new HashMap<>());
}
graph.get(location1).put(location2, distance);
graph.get(location2).put(location1, distance);
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
panel = new JPanel();
panel.setLayout(new GridLayout(4, 1));
JPanel locationPanel = new JPanel();
locationPanel.setLayout(new FlowLayout());
JLabel startLabel = new JLabel("Start location:");
startLocation = new JComboBox<>(graph.keySet().toArray(new String[graph.size()]));
JLabel endLabel = new JLabel("End location:");
endLocation = new JComboBox<>(graph.keySet().toArray(new String[graph.size()]));
locationPanel.add(startLabel);
locationPanel.add(startLocation);
locationPanel.add(endLabel);
locationPanel.add(endLocation);
panel.add(locationPanel);
JPanel buttonPanel = new JPanel();
findPathButton = new JButton("Find Path");
findPathButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String start = (String) startLocation.getSelectedItem();
String end = (String) endLocation.getSelectedItem();
String result = findShortestPath(start, end);
resultArea.setText(result);
}
});
buttonPanel.add(findPathButton);
panel.add(buttonPanel);
JPanel resultPanel = new JPanel();
resultPanel.setLayout(new BorderLayout());
JLabel resultLabel = new JLabel("Shortest path:");
resultArea = new JTextArea();
resultArea.setEditable(false);
JScrollPane resultScrollPane = new JScrollPane(resultArea);
resultPanel.add(resultLabel, BorderLayout.NORTH);
resultPanel.add(resultScrollPane, BorderLayout.CENTER);
panel.add(resultPanel);
add(panel);
setVisible(true);
}
private String findShortestPath(String start, String end) {
Map<String, Integer> distances = new HashMap<>();
Map<String, String> predecessors = new HashMap<>();
PriorityQueue<String> queue = new PriorityQueue<>(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return distances.getOrDefault(o1, Integer.MAX_VALUE) - distances.getOrDefault(o2, Integer.MAX_VALUE);
}
});
for (String location : graph.keySet()) {
distances.put(location, Integer.MAX_VALUE);
predecessors.put(location, null);
}
distances.put(start, 0);
queue.add(start);
while (!queue.isEmpty()) {
String current = queue.poll();
if (current.equals(end)) {
break;
}
for (String neighbor : graph.get(current).keySet()) {
int distance = distances.get(current) + graph.get(current).get(neighbor);
if (distance < distances.get(neighbor)) {
distances.put(neighbor, distance);
predecessors.put(neighbor, current);
queue.remove(neighbor); // update the priority queue
queue.add(neighbor);
}
}
}
List<String> path = new ArrayList<>();
String current = end;
while (current != null) {
path.add(current);
current = predecessors.get(current);
}
Collections.reverse(path);
StringBuilder result = new StringBuilder();
for (int i = 0; i < path.size() - 1; i++) {
result.append(path.get(i)).append(" -> ").append(path.get(i + 1)).append(": ").append(graph.get(path.get(i)).get(path.get(i + 1))).append("\n");
}
result.append("Total Distance: ").append(distances.get(end));
return result.toString();
}
public static void main(String[] args) {
new FishGroupActivityPathGUI();
}
}
```
这个程序使用了Swing库来实现GUI界面。程序首先从文件map.txt中读取地图数据,并将它们存储在一个Map类型的graph变量中。然后,程序使用JPanel、JComboBox、JTextArea和JButton等组件来实现GUI界面。用户可以选择起始点和终止点,然后点击“Find Path”按钮,程序会使用Dijkstra最短路径算法来计算最短路径,并将结果显示在文本框中。
在Dijkstra算法中,程序使用了一个Map类型的distances变量来存储每个节点到起始点的距离,使用一个Map类型的predecessors变量来存储每个节点的前一个节点,使用一个PriorityQueue类型的queue变量来存储待处理的节点。程序在每次处理一个节点时,会遍历它的所有邻居节点,并计算它们到起始点的距离。如果计算出的距离比原来存储的距离小,则更新距离和前驱节点,并将邻居节点加入到priority queue中。最终,程序根据predecessors变量构造出最短路径,并将路径上各点的编号和各条边的权值显示在文本框中。