Java实现最短路径查找器:在两点之间寻找最短路径并避免遮挡
以下是一个实现该功能的Java程序示例:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class ShortestPathFinder {
private JFrame frame;
private JPanel panel;
private JButton generateButton;
private JButton findButton;
private List<Line> lines;
private List<Line> shortestPath;
private int shortestPathLength;
public ShortestPathFinder() {
lines = new ArrayList<>();
shortestPath = new ArrayList<>();
frame = new JFrame('Shortest Path Finder');
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
panel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (Line line : lines) {
if (shortestPath.contains(line)) {
g.setColor(Color.RED);
Graphics2D g2d = (Graphics2D) g;
g2d.setStroke(new BasicStroke(2));
} else {
g.setColor(Color.BLACK);
Graphics2D g2d = (Graphics2D) g;
g2d.setStroke(new BasicStroke(1));
}
g.drawLine(line.getStartX(), line.getStartY(), line.getEndX(), line.getEndY());
}
}
};
generateButton = new JButton('Generate Lines');
generateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
generateLines();
panel.repaint();
}
});
findButton = new JButton('Find Shortest Path');
findButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
findShortestPath();
panel.repaint();
}
});
frame.setLayout(new BorderLayout());
frame.add(panel, BorderLayout.CENTER);
frame.add(generateButton, BorderLayout.NORTH);
frame.add(findButton, BorderLayout.SOUTH);
frame.setVisible(true);
}
private void generateLines() {
lines.clear();
Random random = new Random();
int n = random.nextInt(31) + 10;
for (int i = 0; i < n; i++) {
int startX = random.nextInt(700) + 50;
int startY = random.nextInt(500) + 50;
int endX = random.nextInt(700) + 50;
int endY = random.nextInt(500) + 50;
Line line = new Line(startX, startY, endX, endY);
lines.add(line);
}
shortestPath.clear();
shortestPathLength = Integer.MAX_VALUE;
}
private void findShortestPath() {
for (Line line : lines) {
boolean isShortest = true;
for (Line otherLine : lines) {
if (otherLine != line && isLineCovered(line, otherLine)) {
isShortest = false;
break;
}
}
if (isShortest) {
int length = line.getLength();
if (length < shortestPathLength) {
shortestPathLength = length;
shortestPath.clear();
shortestPath.add(line);
}
}
}
}
private boolean isLineCovered(Line line, Line otherLine) {
return line.getStartX() >= otherLine.getStartX() && line.getStartY() >= otherLine.getStartY()
&& line.getEndX() <= otherLine.getEndX() && line.getEndY() <= otherLine.getEndY();
}
private class Line {
private int startX;
private int startY;
private int endX;
private int endY;
public Line(int startX, int startY, int endX, int endY) {
this.startX = startX;
this.startY = startY;
this.endX = endX;
this.endY = endY;
}
public int getStartX() {
return startX;
}
public int getStartY() {
return startY;
}
public int getEndX() {
return endX;
}
public int getEndY() {
return endY;
}
public int getLength() {
return (int) Math.sqrt(Math.pow(endX - startX, 2) + Math.pow(endY - startY, 2));
}
}
public static void main(String[] args) {
new ShortestPathFinder();
}
}
这个程序使用Swing库创建了一个包含一个绘制面板和两个按钮的窗口。点击“Generate Lines”按钮将生成随机数量的线段,并在绘制面板中显示出来。点击“Find Shortest Path”按钮将查找最短的一条线段,并将其加粗显示在绘制面板中。
为了找到最短路径,程序使用了两个嵌套的循环,首先遍历每条线段,然后对于每条线段,再遍历所有其他线段,检查是否有其他线段遮挡了该线段。如果某条线段没有被其他线段遮挡,那么它就是最短路径的一部分。在遍历过程中,记录下最短路径的长度和线段,并在绘制面板中将最短路径的线段加粗显示出来。
请注意,该程序中没有实现弹框和重新生成线段的功能,你可以根据需要自行添加。
原文地址: https://www.cveoy.top/t/topic/o2nt 著作权归作者所有。请勿转载和采集!