📄 dijpanel.java
字号:
package datas2;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import com.borland.jbcl.layout.XYLayout;
import com.borland.jbcl.layout.*;
import javax.swing.JToggleButton;
import javax.swing.JRadioButton;
import javax.swing.JButton;
import java.awt.Color;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.*;
import javax.swing.BorderFactory;
import javax.swing.border.TitledBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class DijPanel extends JPanel {
///////////////////////////////////////////////////
private final int MAX_VERTS = 20;
private final int INFINITY = 1000000;
private Vertex vertexList[]; // list of vertices
private int adjMat[][]; // adjacency matrix
private int nVerts; // current number of vertices
private int nTree; // number of verts in tree
private DistPar sPath[]; // array for shortest-path data
private int currentVert; // current vertex
private int startToCurrent; // distance to currentVert
private int tempAdjMat[][]= new int[MAX_VERTS][MAX_VERTS];
////////////////////////////////////////////////////
XYLayout xYLayout1 = new XYLayout();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JTextField jTextField1 = new JTextField();
JLabel jLabel3 = new JLabel();
JTextField jTextField2 = new JTextField();
JLabel jLabel4 = new JLabel();
JTextField jTextField3 = new JTextField();
JTextField jTextField4 = new JTextField();
JButton jButton1 = new JButton();
JScrollPane jScrollPane1 = new JScrollPane();
JPanel jPanel1 = new JPanel();
TitledBorder titledBorder1 = new TitledBorder("最短路径信息");
JLabel jLabel5 = new JLabel();
XYLayout xYLayout2 = new XYLayout();
JTextField jTextField5 = new JTextField();
JButton jButton2 = new JButton();
JScrollPane jScrollPane2 = new JScrollPane();
JTextArea jTextArea1 = new JTextArea();
JLabel jLabel6 = new JLabel();
JTextField jTextField6 = new JTextField();
JButton jButton3 = new JButton();
JScrollPane jScrollPane3 = new JScrollPane();
JTextArea jTextArea2 = new JTextArea();
JTextArea jTextArea3 = new JTextArea();
JScrollPane jScrollPane4 = new JScrollPane();
JTextArea jTextArea4 = new JTextArea();
public DijPanel() {
//////////////////////////////////////////////////////////
vertexList = new Vertex[MAX_VERTS];
// adjacency matrix
adjMat = new int[MAX_VERTS][MAX_VERTS];
nVerts = 0;
nTree = 0;
for (int j = 0; j < MAX_VERTS; j++) // set adjacency
for (int k = 0; k < MAX_VERTS; k++) // matrix
adjMat[j][k] = INFINITY; // to infinity
sPath = new DistPar[MAX_VERTS]; // shortest paths
//////////////////////////////////////////////////////////
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
this.setLayout(xYLayout1);
this.setBackground(Color.lightGray);
xYLayout1.setWidth(490);
xYLayout1.setHeight(536);
jLabel1.setBackground(Color.white);
jLabel1.setFont(new java.awt.Font("隶书", Font.BOLD, 20));
jLabel1.setForeground(Color.blue);
jLabel1.setBorder(BorderFactory.createRaisedBevelBorder());
jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
jLabel1.setHorizontalTextPosition(SwingConstants.CENTER);
jLabel1.setText("最短路径算法");
jLabel2.setFont(new java.awt.Font("宋体", Font.BOLD, 13));
jLabel2.setForeground(Color.blue);
jLabel2.setBorder(BorderFactory.createLineBorder(Color.black));
jLabel2.setHorizontalAlignment(SwingConstants.RIGHT);
jLabel2.setText("输入节点数:");
jLabel3.setFont(new java.awt.Font("宋体", Font.BOLD, 13));
jLabel3.setForeground(Color.blue);
jLabel3.setBorder(BorderFactory.createLineBorder(Color.black));
jLabel3.setHorizontalAlignment(SwingConstants.RIGHT);
jLabel3.setText("添加边和权值:");
jLabel4.setFont(new java.awt.Font("宋体", Font.BOLD, 15));
jLabel4.setForeground(Color.blue);
jLabel4.setToolTipText("");
jLabel4.setText("--->");
jButton1.setFont(new java.awt.Font("宋体", Font.BOLD, 13));
jButton1.setForeground(Color.blue);
jButton1.setText("查看矩阵信息");
jButton1.addActionListener(new DijPanel_jButton1_actionAdapter(this));
jScrollPane1.setHorizontalScrollBarPolicy(JScrollPane.
HORIZONTAL_SCROLLBAR_ALWAYS);
jScrollPane1.setVerticalScrollBarPolicy(JScrollPane.
VERTICAL_SCROLLBAR_ALWAYS);
jScrollPane1.setBorder(BorderFactory.createLineBorder(Color.black));
jPanel1.setBackground(Color.lightGray);
jPanel1.setFont(new java.awt.Font("宋体", Font.BOLD, 12));
jPanel1.setBorder(titledBorder1);
jPanel1.setLayout(xYLayout2);
jLabel5.setFont(new java.awt.Font("宋体", Font.BOLD, 13));
jLabel5.setForeground(Color.blue);
jLabel5.setBorder(BorderFactory.createLineBorder(Color.black));
jLabel5.setHorizontalAlignment(SwingConstants.RIGHT);
jLabel5.setText("结点:");
jButton2.setFont(new java.awt.Font("宋体", Font.BOLD, 13));
jButton2.setForeground(Color.blue);
jButton2.setText("查看信息");
jButton2.addActionListener(new DijPanel_jButton2_actionAdapter(this));
jScrollPane2.setHorizontalScrollBarPolicy(JScrollPane.
HORIZONTAL_SCROLLBAR_ALWAYS);
jScrollPane2.setVerticalScrollBarPolicy(JScrollPane.
VERTICAL_SCROLLBAR_ALWAYS);
jScrollPane2.setBorder(BorderFactory.createLineBorder(Color.black));
jLabel6.setFont(new java.awt.Font("宋体", Font.BOLD, 13));
jLabel6.setForeground(Color.blue);
jLabel6.setBorder(BorderFactory.createLineBorder(Color.black));
jLabel6.setHorizontalAlignment(SwingConstants.RIGHT);
jLabel6.setText("到结点:");
jButton3.setFont(new java.awt.Font("宋体", Font.BOLD, 13));
jButton3.setForeground(Color.blue);
jButton3.setText("路径");
jButton3.addActionListener(new DijPanel_jButton3_actionAdapter(this));
jScrollPane3.setHorizontalScrollBarPolicy(JScrollPane.
HORIZONTAL_SCROLLBAR_ALWAYS);
jScrollPane3.setVerticalScrollBarPolicy(JScrollPane.
VERTICAL_SCROLLBAR_ALWAYS);
jScrollPane3.setBorder(BorderFactory.createLineBorder(Color.black));
jTextField4.addActionListener(new DijPanel_jTextField4_actionAdapter(this));
jTextField5.addActionListener(new DijPanel_jTextField5_actionAdapter(this));
jTextArea3.setBackground(Color.lightGray);
jTextArea1.setBackground(Color.lightGray);
jTextArea2.setBackground(Color.lightGray);
jTextArea4.setBackground(Color.lightGray);
jScrollPane4.setHorizontalScrollBarPolicy(JScrollPane.
HORIZONTAL_SCROLLBAR_ALWAYS);
jScrollPane4.setVerticalScrollBarPolicy(JScrollPane.
VERTICAL_SCROLLBAR_NEVER);
jScrollPane4.setBorder(BorderFactory.createLineBorder(Color.black));
this.add(jLabel1, new XYConstraints(154, 0, 196, 43));
this.add(jLabel2, new XYConstraints(1, 52, 92, 29));
this.add(jTextField1, new XYConstraints(109, 52, 44, 29));
this.add(jLabel3, new XYConstraints(167, 52, 96, 29));
this.add(jLabel4, new XYConstraints(326, 53, -1, 29));
this.add(jTextField2, new XYConstraints(278, 53, 40, 29));
this.add(jTextField3, new XYConstraints(377, 53, 40, 29));
this.add(jTextField4, new XYConstraints(441, 53, 40, 29));
jScrollPane1.getViewport().add(jTextArea3);
jPanel1.add(jLabel5, new XYConstraints(0, 6, 49, 28));
jPanel1.add(jTextField5, new XYConstraints(62, 6, 39, 28));
jPanel1.add(jButton2, new XYConstraints(108, 6, 96, 28));
jPanel1.add(jLabel6, new XYConstraints(242, 6, 66, 28));
jPanel1.add(jButton3, new XYConstraints(386, 6, 63, 26));
jPanel1.add(jTextField6, new XYConstraints(322, 6, 47, 28));
jScrollPane3.getViewport().add(jTextArea2);
jScrollPane2.getViewport().add(jTextArea1);
jPanel1.add(jScrollPane2, new XYConstraints(0, 48, 204, 106));
jPanel1.add(jScrollPane3, new XYConstraints(242, 48, 207, 106));
this.add(jButton1, new XYConstraints(1, 151, 124, 29));
this.add(jPanel1, new XYConstraints(4, 338, 477, -1));
this.add(jScrollPane1, new XYConstraints(3, 185, 478, 146));
this.add(jScrollPane4, new XYConstraints(4, 91, 472, 51));
jScrollPane4.getViewport().add(jTextArea4);
}
//////////////////////////////////////////////////////////
public void addVertex(char lab)
{
vertexList[nVerts++] = new Vertex(lab);
}
//---------------------------------------------------------------
public void addEdge(int start, int end, int weight)
{
adjMat[start][end] = weight; // (directed)
adjMat[end][start] = weight;
}
// -------------------------------------------------------------
public void path(int startTree) // find all shortest paths
{
// int startTree = 0; // start at vertex 0
vertexList[startTree].isInTree = true;
nTree = 1; // put it in tree
// transfer row of distances from adjMat to sPath
for(int j=0; j<nVerts; j++)
{
int tempDist = adjMat[startTree][j];
sPath[j] = new DistPar(startTree, tempDist);
}
// until all vertices are in the tree
while(nTree < nVerts)
{
int indexMin = getMin(); // get minimum from sPath
int minDist = sPath[indexMin].distance;
if(minDist == INFINITY) // if all infinite
{ // or in tree,
System.out.println("There are unreachable vertices");
break; // sPath is complete
}
else
{ // reset currentVert
currentVert = indexMin; // to closest vert
startToCurrent = sPath[indexMin].distance;
// minimum distance from startTree is
// to currentVert, and is startToCurrent
}
// put current vertex in tree
vertexList[currentVert].isInTree = true;
nTree++;
adjust_sPath(); // update sPath[] array
} // end while(nTree<nVerts)
}
// -------------------------------------------------------------
public void adjust_sPath()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -