?? graphinput.java
字號(hào):
/*
*@(#)GraphInput.java 2.0 2005/05/02
*
*清華大學(xué) 精密儀器與機(jī)械學(xué)系
*范燦升 fancansheng@163.com
*/
package input;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.util.Vector;
//導(dǎo)入系統(tǒng)類
import lib.Library;
import algorithm.GraphTheory;
import ADT.BinaryTree;
//導(dǎo)入自定義的類
/**
*該類所提供的方法是供給類{@link Modeling}調(diào)用的,用以產(chǎn)生圖論算法的輸入界面。
*<p>為提高計(jì)算精確度,圖中各矩陣元素必須是整型數(shù),而不能是浮點(diǎn)數(shù),
*用戶在輸入前必須對(duì)待處理的圖進(jìn)行一下加工。
*@version 2.0, 2005/05/02
*@author 范燦升
*@see Modeling
*@see algorithm.GraphTheory
*/
public class GraphInput implements ActionListener
{
private int i,j;
private int n=0;//n是結(jié)點(diǎn)數(shù)
private int m=0;//m是邊數(shù)
private int[][] weightMatrix;//權(quán)矩陣
private int[] leafVertex;//哈夫曼樹樹葉結(jié)點(diǎn)的權(quán)
private JTextField[][] weightTextField;
private JTextField[] leafTextField;
private JButton dijkstraButton;
private JButton dijkstraHelpButton;
private JButton hamiltonButton;
private JButton hamiltonHelpButton;
private JButton huffmanButton;
private JButton huffmanHelpButton;
private JButton fillSymmetric;
/**
*輸入面板的父組件。
*/
public Component parentComponent;
/**
*數(shù)據(jù)在該面板上輸入,主要是鄰接矩陣、關(guān)聯(lián)矩陣等。
*/
public JPanel inputPanel;
/**
*用來(lái)放置計(jì)算按鈕和幫助按鈕等。
*/
public JPanel computePanel;
/**
*用來(lái)放置inputPanel的容器。
*/
public JPanel mainPanel;
/**
*產(chǎn)生包含圖的矩陣相關(guān)參數(shù)的類。
*@param parentComponent 輸入面板的上一層組件
*@param inputPanel 數(shù)據(jù)在該面板上輸入
*@param computePanel 面板上的按鈕為計(jì)算用按鈕
*@param mainPanel mainPanel是用來(lái)放置inputPanel的容器
*/
public GraphInput(Component parentComponent,JPanel inputPanel,JPanel computePanel,JPanel mainPanel)
{
this.parentComponent=parentComponent;
this.inputPanel=inputPanel;
this.computePanel=computePanel;
this.mainPanel=mainPanel;
}
/**
*顯示輸入待計(jì)算最短路的圖的矩陣的界面。
*<p>用戶從該界面輸入圖的權(quán)矩陣。
*/
public void showDijkstra()
{
boolean pass=false;//用于標(biāo)記是否通過(guò)合法性檢驗(yàn)
boolean secondTime=false;
String tmp;
while(!pass)
{
if(secondTime==true)
tmp=JOptionPane.showInputDialog(parentComponent,"請(qǐng)輸入一個(gè)正整數(shù)!\n請(qǐng)重新輸入:","輸入結(jié)點(diǎn)數(shù)",JOptionPane.WARNING_MESSAGE);
else
tmp=JOptionPane.showInputDialog(parentComponent,"請(qǐng)輸入圖中結(jié)點(diǎn)的數(shù)目:","輸入結(jié)點(diǎn)數(shù)",JOptionPane.QUESTION_MESSAGE);
try
{
if(tmp==null)
return;
n=-1;
n=Integer.parseInt(tmp);
}
catch(NumberFormatException e)
{
secondTime=true;
}
if(n>=1)
pass=true;
else
secondTime=true;
}
weightMatrix=new int[n][n];
//輸入結(jié)點(diǎn)數(shù)并形成權(quán)矩陣
GridLayout weightGridLayout=new GridLayout(n,n);
inputPanel.setLayout(weightGridLayout);
TitledBorder border=new TitledBorder("輸入有向正權(quán)圖的權(quán)矩陣");
border.setTitleFont(Library.font);
inputPanel.setBorder(border);
//設(shè)置權(quán)矩陣布局
weightTextField=new JTextField[n][n];
inputPanel.removeAll();
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
weightTextField[i][j]=new JTextField("0",Library.TEXTFIELD_LENGTH);
weightTextField[i][j].setToolTipText("第"+(i+1)+"行,第"+(j+1)+"列");
inputPanel.add(weightTextField[i][j]);
}
}
//向inputPanel中添加輸入域
for(i=0;i<n;i++)
weightTextField[i][i].setEditable(false);
//不能含有自環(huán)
JScrollPane scrollPane=new JScrollPane(inputPanel);
BoxLayout computePanelLayout=new BoxLayout(computePanel,BoxLayout.X_AXIS);
computePanel.setLayout(computePanelLayout);
dijkstraButton=new JButton("計(jì)算兩點(diǎn)間的最短路",new ImageIcon(Library.polyhedronIcont_Scaled));
dijkstraButton.setFont(Library.font);
computePanel.removeAll();
computePanel.add(dijkstraButton);
fillSymmetric=new JButton("自動(dòng)填充為對(duì)稱矩陣",new ImageIcon(Library.aidIcon_Scaled));
fillSymmetric.setFont(Library.font);
computePanel.add(fillSymmetric);
dijkstraHelpButton=new JButton("幫助",new ImageIcon(Library.helpIcon_Scaled));
dijkstraHelpButton.setFont(Library.font);
computePanel.add(dijkstraHelpButton);
mainPanel.removeAll();
mainPanel.add(scrollPane,BorderLayout.CENTER);
mainPanel.add(computePanel,BorderLayout.SOUTH);
//布局
dijkstraButton.addActionListener(this);
fillSymmetric.addActionListener(this);
dijkstraHelpButton.addActionListener(this);
parentComponent.setSize(parentComponent.getPreferredSize());
parentComponent.setVisible(true);
}
/**
*顯示輸入待計(jì)算求最短哈密頓回路的圖的權(quán)矩陣界面(即旅行商問(wèn)題,又稱推銷員問(wèn)題)。
*<p>用戶從該界面輸入圖的權(quán)矩陣。該圖必須是無(wú)向完全圖。
*/
public void showHamilton()
{
boolean pass=false;//用于標(biāo)記是否通過(guò)合法性檢驗(yàn)
boolean secondTime=false;
String tmp;
while(!pass)
{
if(secondTime==true)
tmp=JOptionPane.showInputDialog(parentComponent,"請(qǐng)輸入一個(gè)不小于3的正整數(shù)!\n請(qǐng)重新輸入:","輸入結(jié)點(diǎn)數(shù)",JOptionPane.WARNING_MESSAGE);
else
tmp=JOptionPane.showInputDialog(parentComponent,"請(qǐng)輸入圖中結(jié)點(diǎn)的數(shù)目:","輸入結(jié)點(diǎn)數(shù)",JOptionPane.QUESTION_MESSAGE);
try
{
if(tmp==null)
return;
n=-1;
n=Integer.parseInt(tmp);
}
catch(NumberFormatException e)
{
secondTime=true;
}
if(n>=3)
pass=true;
else
secondTime=true;
}
weightMatrix=new int[n][n];
//輸入結(jié)點(diǎn)數(shù)并形成權(quán)矩陣
GridLayout weightGridLayout=new GridLayout(n,n);
inputPanel.setLayout(weightGridLayout);
TitledBorder border=new TitledBorder("輸入無(wú)向完全圖的權(quán)矩陣");
border.setTitleFont(Library.font);
inputPanel.setBorder(border);
//設(shè)置權(quán)矩陣布局
weightTextField=new JTextField[n][n];
inputPanel.removeAll();
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
weightTextField[i][j]=new JTextField(Library.TEXTFIELD_LENGTH);
weightTextField[i][j].setToolTipText("第"+(i+1)+"行,第"+(j+1)+"列");
inputPanel.add(weightTextField[i][j]);
}
}
//向inputPanel中添加輸入域
for(i=0;i<n;i++)
{
for(j=0;j<=i;j++)
weightTextField[i][j].setEditable(false);
}
//保證輸入為無(wú)向圖
for(i=0;i<n;i++)
weightTextField[i][i].setText("0");
//不能含有自環(huán)
JScrollPane scrollPane=new JScrollPane(inputPanel);
FlowLayout computePanelLayout=new FlowLayout();
computePanel.setLayout(computePanelLayout);
hamiltonButton=new JButton("計(jì)算最短哈密頓回路",new ImageIcon(Library.polyhedronIcont_Scaled));
hamiltonButton.setFont(Library.font);
computePanel.removeAll();
computePanel.add(hamiltonButton);
hamiltonHelpButton=new JButton("幫助",new ImageIcon(Library.helpIcon_Scaled));
hamiltonHelpButton.setFont(Library.font);
computePanel.add(hamiltonHelpButton);
mainPanel.removeAll();
mainPanel.add(scrollPane,BorderLayout.CENTER);
mainPanel.add(computePanel,BorderLayout.SOUTH);
//布局
hamiltonButton.addActionListener(this);
hamiltonHelpButton.addActionListener(this);
computePanel.addMouseListener(new MouseAdapter()
{
public void mouseEntered(MouseEvent e)
{
for(i=0;i<n;i++)
{
for(j=0;j<i;j++)
weightTextField[i][j].setText(weightTextField[j][i].getText());
}
}
}
);//自動(dòng)填充成對(duì)稱矩陣
parentComponent.setSize(parentComponent.getPreferredSize());
parentComponent.setVisible(true);
}
/**
*顯示輸入待構(gòu)造哈夫曼樹的樹葉結(jié)點(diǎn)的權(quán)的輸入界面。
*<p>用戶從該界面輸入樹葉結(jié)點(diǎn)的權(quán)。
*/
public void showHuffman()
{
boolean pass=false;//用于標(biāo)記是否通過(guò)合法性檢驗(yàn)
boolean secondTime=false;
String tmp;
while(!pass)
{
if(secondTime==true)
tmp=JOptionPane.showInputDialog(parentComponent,"請(qǐng)輸入一個(gè)不小于2的正整數(shù)!\n請(qǐng)重新輸入:","輸入樹葉結(jié)點(diǎn)數(shù)",JOptionPane.WARNING_MESSAGE);
else
tmp=JOptionPane.showInputDialog(parentComponent,"請(qǐng)輸入哈夫曼樹的樹葉結(jié)點(diǎn)的數(shù)目:","輸入樹葉結(jié)點(diǎn)數(shù)",JOptionPane.QUESTION_MESSAGE);
try
{
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -