java求最短路径问题源程序_最短路径问题 java实现 源代码

这是一个使用Java编程实现最短路径问题的源代码程序,包括读取资源文件、创建图形界面、绘制结点和线条、计算最短路径等功能。程序通过`ShortPathFrame`类创建GUI,并使用`ShortPathALG`类进行最短路径计算,采用Floyd算法。用户可以选择始发地和目的地,程序将显示最短路径。
摘要由CSDN通过智能技术生成

0_1290924152by6H.gif0_1290924156N2Ph.gif

用到的资源文件  文件名 shortPath.properties

begin=/u59CB/u53D1/u5730/uFF1A

clear=/u6E05 /u9664

clearString=/u6E05/u695A/u7ED8/u56FE/u533A/u548C/u6240/u6709/u7684/u6587/u672C

drawLine=/u7ED8/u5236/u8DEF/u5F84

end=/u76EE/u7684/u5730/uFF1A

mouseLocation=/u9F20/u6807/u4F4D/u7F6E/uFF1A

nodeLine=/u7ED3/u70B9/u8FDE/u7EBF

nodeLineString=/u7ED8/u5236/u7ED3/u70B9/u5230/u7ED3/u70B9/u7684/u8DEF/u5F84/uFF08/u8FDE/u7EBF/uFF09

nodeName=/u7ED3/u70B9/u540D/u79F0

nodeNameString=/u7528/u4E8E/u8F93/u5165/u65B0/u7ED3/u70B9/u7684/u540D/u79F0

nodeNew=/u65B0/u7ED3/u70B9

nodeSpace=/u7ED3/u70B9/u8DDD/u79BB

nodeSpaceString=/u7528/u4E8E/u8F93/u5165/u7ED3/u70B9/u5230/u7ED3/u70B9/u7684/u8DEF/u5F84

nodeString=/u7ED8/u5236/u65B0/u7684/u5706/u5F62/u7ED3/u70B9

ok=/u786E /u5B9A

okText=/u5355/u51FB /u201C/u786E/u5B9A/u201D /u6309/u94AE/u5C06/u663E/u793A/u51FA/u53D1/u5730/u5230/u76EE/u7684/u5730/u7684/u6700/u77ED/u8DEF/u5F84/uFF01

shortPath=/u6700/u77ED/u8DEF/u5F84

import javax.swing.UIManager;

/**

* 类介绍:主类

*

* @author 作者:liujunguang

* @version 创建时间:2010-5-25 下午01:45:23

*/

public class ShortPath {

public static void main(String[] args) {

try {// 将界面设置为当前windows界面风格

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

} catch (Exception e) {

}

new ShortPathFrame();

}

}

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.FlowLayout;

import java.awt.Toolkit;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.InputStream;

import java.util.Properties;

import javax.swing.Icon;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JToolBar;

/**

* 类说明:ShortPath 界面类

*

* @author 作者: LiuJunGuang

* @version 创建时间:2010-5-29 下午02:15:05

*/

public class ShortPathFrame extends JFrame implements ActionListener {

private static final long serialVersionUID = -1877582687108858783L;

private Properties prop = null;

private String begin = "Begin: ";// 始发地

private String end = "End: ";// 目的地

private String ok = "OK";// 确定键

private String shortPath = "ShortPath";// 标题

private String mouseLocation = "MouseLocation:";// 鼠标的位置

private String okText = "Show Path when OK is click.";

private JPanel nothJP = null, shouthJP = null, centerJP = null;

private JLabel beginJLabel = null, endJLabel = null;

private JLabel AtoBPathJLabel = null, mouseJLabel = null;

private JComboBox beginJComboBox = null, endJComboBox = null;// 两个下拉选择框

private JButton jButton = null;

private JToolBar jToolBar = null;// 定义工具条

private JButton newNodeJButton = null, nodeLineJButton = null,

nodeNameJButton = null, nodeSpaceJButton = null,

clearJButton = null;// 定义工具条按钮

private DrawJPanel drawJPanel = null;// 定义中央绘制区对象

public ShortPathFrame() {

// TODO Auto-generated constructor stub

properties();

if (prop != null) {

begin = prop.getProperty("begin");

end = prop.getProperty("end");

ok = prop.getProperty("ok");

shortPath = prop.getProperty("shortPath");

mouseLocation = prop.getProperty("mouseLocation");

okText = prop.getProperty("okText");

}

this.setTitle(shortPath);

// 始发地

nothJP = new JPanel();

beginJLabel = new JLabel(begin);

nothJP.add(beginJLabel);

beginJComboBox = new JComboBox();

beginJComboBox.setPreferredSize(new Dimension(150, 25));

beginJComboBox.addActionListener(this);

nothJP.add(beginJComboBox);

nothJP.add(new JLabel(" "));// 添加空标签使得beginJComboBox与endJLabel之间有一定的间隙

// 目的地

endJLabel = new JLabel(end);

nothJP.add(endJLabel);

endJComboBox = new JComboBox();

endJComboBox.setPreferredSize(new Dimension(150, 25));

endJComboBox.addActionListener(this);

nothJP.add(endJComboBox);

jButton = new JButton(ok);

jButton.setToolTipText(okText);// 确定 按钮添加提示文本

jButton.addActionListener(this);

nothJP.add(jButton);

// 中间绘图区

centerJP = new JPanel();

toolBar();

centerJP.setLayout(new BorderLayout());

centerJP.add(jToolBar, BorderLayout.NORTH);

drawJPanel = new DrawJPanel(this);

centerJP.add(drawJPanel, BorderLayout.CENTER);

// 状态栏

shouthJP = new JPanel();

shouthJP.setBackground(Color.LIGHT_GRAY);

AtoBPathJLabel = new JLabel();

mouseJLabel = new JLabel(mouseLocation);

shouthJP.add(mouseJLabel);

shouthJP.add(new JLabel(" "));

shouthJP.add(AtoBPathJLabel);

this.add(nothJP, BorderLayout.NORTH);

this.add(shouthJP, BorderLayout.SOUTH);

this.add(centerJP, BorderLayout.CENTER);

// 设置流布局的流向

FlowLayout flowLayout = new FlowLayout();

flowLayout.setAlignment(FlowLayout.LEFT);

flowLayout.setHgap(5);

shouthJP.setLayout(flowLayout);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setVisible(true);

Toolkit toolkit = getToolkit();

Dimension dim = toolkit.getScreenSize();

this.setLocation((dim.width - 500) / 2, (dim.height - 500) / 2);

pack();

}

private void setDrawType(int drawType) {// 设置要绘制的内容

drawJPanel.setCurrentChoice(drawType);// 设置绘制内容

drawJPanel.createNewitem();// 新建要绘制的图形

drawJPanel.repaint();// 重新绘制绘图区

}

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

if (e.getSource() == newNodeJButton) {// 新建结点

setAtoBPathJLabel("你选择了新建圆结点!");

int c = drawJPanel.getCircleCount();

drawJPanel.setCircleCount(--c);

setDrawType(1);

} else if (e.getSource() == nodeNameJButton) {// 结点名称

setAtoBPathJLabel("你选择了输入圆结点的名称!");

JOptionPane.showMessageDialog(null, "请单击结点(圆圈)以确定要输入名字的结点!", "提示",

JOptionPane.INFORMATION_MESSAGE);

setDrawType(2);

} else if (e.getSource() == nodeLineJButton) {// 结点连线

setAtoBPathJLabel("你选择了新建圆结点之间的连线!");

int l = drawJPanel.getLineCount();

drawJPanel.setLineCount(--l);

setDrawType(3);

} else if (e.getSource() == nodeSpaceJButton) {// 结点距离

setAtoBPathJLabel("你选择了输入结点之间的距离!");

JOptionPane.showMessageDialog(null, "请单击线段上方以确定要输入距离的线段!", "提示",

JOptionPane.INFORMATION_MESSAGE);

setDrawType(4);

} else if (e.getSource() == clearJ

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值