import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* 用于显示两点间所有最短路径以及最短路径子图的Dialog。
* 继承自GraphShow。
* @author Fe
*/
public class GraphShowAdv extends GraphShow {
JPanel checkPanel = new JPanel();
static JLabel pathNumLabel = new JLabel("");
CheckboxGroup cbg = new CheckboxGroup();
Checkbox showAllPath = new Checkbox("显示最短路径路由子图", cbg, true);
Checkbox showOnePath = new Checkbox("显示一条最短路由路径", cbg, false);
JLabel currentPathLabel = new JLabel("");
JButton nextPath = new JButton("下一条最短路由路径");
JFrame fram = null;
static int path[][] = null;
/**
* @param owner
* Dialog所依赖的主窗口
* @param g
* 所操作的图的信息
*/
public GraphShowAdv(JFrame owner, GraphFromFile g) {
graph = g;
fram = owner;
mapCanvas = new MapCanvas(graph);
graphContainer = new JDialog(owner, "RouterNodesShow", true);
graphContainer.getContentPane().setLayout(new BorderLayout());
graphContainer.getContentPane().add(mapCanvas, BorderLayout.CENTER);
JPanel temp = new JPanel();
temp.add(showLength);
temp.add(ok);
//用匿名内类实现使地图窗口不可见。
ok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
graphContainer.setVisible(false);
}
});
graphContainer.getContentPane().add(temp, BorderLayout.SOUTH);
JPanel temp1 = new JPanel();
temp1.add(mapInfo);
graphContainer.getContentPane().add(temp1, BorderLayout.NORTH);
//为显示路径长度按钮添加监听器。
AddAction action = new AddAction();
//将显示路径长度按钮设置为默认按钮。
graphContainer.getRootPane().setDefaultButton(showLength);
setCheckPanel();
graphContainer.getContentPane().add(checkPanel, BorderLayout.EAST);
act();
GraphShow.setLengthButtonEnable(false);
graphContainer.setSize(735, 710);
//得到屏幕的大小,即屏幕的分辨率。
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
graphContainer.setLocation(d.width/2-340, d.height/2-370);
graphContainer.setVisible(true);
//此处我也没有搞清楚到底怎么回事。
//反正如果没有这一句,显示路径长度的信息框的显示次数将随着对checkbox的点击次数的增加而增加。
showLength.removeActionListener(action);
}
/**
* 设置面板用。
*/
private void setCheckPanel() {
checkPanel.setLayout(new GridLayout(0, 1));
JPanel temp1 = new JPanel();
temp1.add(pathNumLabel);
JPanel temp2 = new JPanel();
temp2.add(showAllPath);
JPanel temp3 = new JPanel();
temp3.add(showOnePath);
JPanel temp4 = new JPanel();
temp4.add(currentPathLabel);
JPanel temp5 = new JPanel();
temp5.add(nextPath);
nextPath.setEnabled(false);
checkPanel.add(temp1);
checkPanel.add(temp2);
checkPanel.add(temp3);
checkPanel.add(temp4);
checkPanel.add(temp5);
}
/**
* 设置最短路径子图。
* @param pa
* 最短路径子图(矩阵形式)
*/
public static void setSubGraph(int pa[][]) {
path = pa;
}
/**
* 动态的设置共有几条最短路径。
* @param s
* 显示的字符串。
*/
public static void setPathNumLabel(String s) {
pathNumLabel.setText(s);
}
/**
* 为各个组件添加事件监听器。
*/
private void act() {
showAllPath.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent ie) {
if (ie.getStateChange() == ie.SELECTED) {
nextPath.setEnabled(false);
GraphShow.setLengthButtonEnable(false);
currentPathLabel.setText("");
MapCanvas.setDrawSpecifiedPath(null, false);
MapCanvas.setDrawSubGraph(path, true);
mapCanvas.repaint();
}
}
});
showOnePath.addItemListener(new ItemListener() {
int currentPath = -1;
public void itemStateChanged(ItemEvent ie) {
if (ie.getStateChange() == ie.SELECTED) {
nextPath.setEnabled(true);
GraphShow.setLengthButtonEnable(true);
int pathNum = path.length;
if (currentPath == pathNum-1) {
currentPath = -1;
}
currentPath++;
currentPathLabel.setText("第" + (currentPath+1) + "条最短路由路径");
MapCanvas.setDrawSubGraph(null, false);
MapCanvas.setDrawSpecifiedPath(path[currentPath], true);
mapCanvas.repaint();
}
}
});
nextPath.addActionListener(new ActionListener() {
int currentPath = 0;
public void actionPerformed(ActionEvent e) {
int pathNum = path.length;
if (currentPath == pathNum-1) {
currentPath = -1;
}
currentPath++;
currentPathLabel.setText("第" + (currentPath+1) + "条最短路由路径");
MapCanvas.setDrawSpecifiedPath(path[currentPath], true);
mapCanvas.repaint();
}
});
}
}