📄 graphinput.java
字号:
if(tmp==null)
return;
n=-1;
n=Integer.parseInt(tmp);
}
catch(NumberFormatException e)
{
secondTime=true;
}
if(n>=2)
pass=true;
else
secondTime=true;
}
leafVertex=new int[n];
//输入树叶结点数并形成树叶结点权矩阵
GridLayout leafGridLayout;
if(n>10)
leafGridLayout=new GridLayout(0,10);
else
leafGridLayout=new GridLayout(0,n);
inputPanel.setLayout(leafGridLayout);
TitledBorder border=new TitledBorder("输入哈夫曼树树叶结点的权");
border.setTitleFont(Library.font);
inputPanel.setBorder(border);
//设置树叶结点权矩阵布局
leafTextField=new JTextField[n];
inputPanel.removeAll();
for(i=0;i<n;i++)
{
leafTextField[i]=new JTextField(Library.TEXTFIELD_LENGTH);
leafTextField[i].setToolTipText("第"+(i+1)+"结点");
inputPanel.add(leafTextField[i]);
}
//向inputPanel中添加输入域
JScrollPane scrollPane=new JScrollPane(inputPanel);
BoxLayout computePanelLayout=new BoxLayout(computePanel,BoxLayout.X_AXIS);
computePanel.setLayout(computePanelLayout);
huffmanButton=new JButton("生成哈夫曼树",new ImageIcon(Library.polyhedronIcont_Scaled));
huffmanButton.setFont(Library.font);
computePanel.removeAll();
computePanel.add(huffmanButton);
huffmanHelpButton=new JButton("帮助",new ImageIcon(Library.helpIcon_Scaled));
huffmanHelpButton.setFont(Library.font);
computePanel.add(huffmanHelpButton);
mainPanel.removeAll();
mainPanel.add(scrollPane,BorderLayout.CENTER);
mainPanel.add(computePanel,BorderLayout.SOUTH);
//布局
huffmanButton.addActionListener(this);
huffmanHelpButton.addActionListener(this);
parentComponent.setSize(parentComponent.getPreferredSize());
parentComponent.setVisible(true);
}
/**
*由ActionListener所指定的方法,响应用户单击按钮时的动作
*@param e 单击按钮所产生的事件
*/
public void actionPerformed(ActionEvent e)
{
boolean secondTime;
boolean pass;
//用于进行输入的合法性检查
String str;
if(e.getSource()==dijkstraButton)
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
try
{
weightMatrix[i][j]=Integer.parseInt(weightTextField[i][j].getText());
if((weightMatrix[i][j]<0)||(weightMatrix[i][j]==Integer.MAX_VALUE))
throw new NumberFormatException();
}
catch(NumberFormatException nException)
{
JOptionPane.showMessageDialog(parentComponent,
"权矩阵中第"+(i+1)+"行,第"+(j+1)+
"列的输入不是非负整数\n(或者该整数超过2147483647)。\n请重新输入","输入错误",JOptionPane.WARNING_MESSAGE);
return;
}
}
}
//完成值的传递
String temp;
int start=0;
int end=0;
secondTime=false;
pass=false;
while(!pass)
{
if(secondTime==true)
temp=JOptionPane.showInputDialog(parentComponent,
"请输入一个大于或等于1,小于或等于"+n+"的整数!\n请重新输入开始结点","开始结点",JOptionPane.WARNING_MESSAGE);
else
temp=JOptionPane.showInputDialog(parentComponent,
"请输入开始结点","开始结点",JOptionPane.QUESTION_MESSAGE);
try
{
if(temp==null)
return;
start=Integer.parseInt(temp);
}
catch(NumberFormatException nException)
{
secondTime=true;
}
if((start>=1)&&(start<=n))
pass=true;
else
secondTime=true;
}
secondTime=false;
pass=false;
while(!pass)
{
if(secondTime==true)
temp=JOptionPane.showInputDialog(parentComponent,
"请输入一个大于或等于1,小于或等于"+n+"的整数!\n请重新输入结束结点","结束结点",JOptionPane.WARNING_MESSAGE);
else
temp=JOptionPane.showInputDialog(parentComponent,
"请输入结束结点","结束结点",JOptionPane.QUESTION_MESSAGE);
try
{
end=Integer.parseInt(temp);
}
catch(NumberFormatException nException)
{
secondTime=true;
}
if((end>=1)&&(end<=n))
pass=true;
else
secondTime=true;
}
GraphTheory graph=new GraphTheory(weightMatrix,GraphTheory.WEIGHT);
Vector vector=graph.dijkstra(start-1,end-1);
int[] path=(int[])vector.get(0);
Integer len=(Integer)vector.get(1);
StringBuffer buffer;
if(len.intValue()==-1)
buffer=new StringBuffer("不存在从结点"+start+"到结点"+end+"的路径。");
else
{
buffer=new StringBuffer("从结点"+start+"到结点"+end+"的最短路为"+len.intValue()+"\n");
buffer.append("所走路径为:\n");
StringBuffer bufferTemp=new StringBuffer();
int i=end-1;
bufferTemp.append("\n"+end);
while(i!=(start-1))
{
i=path[i];
bufferTemp.append("\n"+(i+1));
}
buffer.append(bufferTemp.reverse());
//生成路径
}
JOptionPane.showMessageDialog(parentComponent,buffer,
"最短路",JOptionPane.INFORMATION_MESSAGE,new ImageIcon(Library.polyhedronIcont_Scaled));
//显示求解结果
}
//dijkstraButton的动作
if(e.getSource()==fillSymmetric)
{
for(i=0;i<n;i++)
{
for(j=0;j<i;j++)
{
str=weightTextField[j][i].getText();
if((str.intern()!="0".intern())&&(str.intern()!="".intern()))
weightTextField[i][j].setText(str);
else
weightTextField[j][i].setText(weightTextField[i][j].getText());
}
}
}
//自动填充成对称矩阵
if(e.getSource()==dijkstraHelpButton)
{
ImageIcon helpMessage=new ImageIcon("resource\\DijkstraHelp.GIF");
JOptionPane.showMessageDialog(parentComponent,helpMessage,"输入帮助",JOptionPane.INFORMATION_MESSAGE,new ImageIcon(Library.helpIcon_Scaled));
}
if(e.getSource()==hamiltonButton)
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
try
{
weightMatrix[i][j]=Integer.parseInt(weightTextField[i][j].getText());
if((weightMatrix[i][j]==0)&&(i!=j))
throw new NumberFormatException();
}
catch(NumberFormatException nException)
{
JOptionPane.showMessageDialog(parentComponent,
"权矩阵中第"+(i+1)+"行,第"+(j+1)+
"列的输入不是非零整数\n(或者该整数绝对值超过2147483647)。\n请重新输入","输入错误",JOptionPane.WARNING_MESSAGE);
return;
}
}
}
//完成值的传递
GraphTheory graph=new GraphTheory(weightMatrix,GraphTheory.WEIGHT);
int[] path=graph.hamilton();
StringBuffer buffer;
buffer=new StringBuffer("最短哈密顿圈的长度为"+path[0]+"\n");
buffer.append("该哈密顿圈结点的顺序依次为:\n");
for(i=1;i<path.length;i++)
buffer.append((path[i]+1)+"\n");
JOptionPane.showMessageDialog(parentComponent,buffer,
"最短路",JOptionPane.INFORMATION_MESSAGE,new ImageIcon(Library.polyhedronIcont_Scaled));
//显示求解结果
}
//hamiltonButton的动作
if(e.getSource()==hamiltonHelpButton)
{
ImageIcon helpMessage=new ImageIcon("resource\\HamiltonHelp.GIF");
JOptionPane.showMessageDialog(parentComponent,helpMessage,"输入帮助",JOptionPane.INFORMATION_MESSAGE,new ImageIcon(Library.helpIcon_Scaled));
}
if(e.getSource()==huffmanButton)
{
for(i=0;i<n;i++)
{
try
{
leafVertex[i]=Integer.parseInt(leafTextField[i].getText());
if(leafVertex[i]<=0)
throw new NumberFormatException();
}
catch(NumberFormatException nException)
{
JOptionPane.showMessageDialog(parentComponent,"第"+(i+1)+
"个树叶结点的权不是正整数\n(或者该整数绝对值超过2147483647)。\n请重新输入",
"输入错误",JOptionPane.WARNING_MESSAGE);
return;
}
}
//完成值的传递
BinaryTree[] binTree=GraphTheory.huffman(leafVertex);
StringBuffer buffer;
int length=0;
buffer=new StringBuffer("哈夫曼树的各树叶结点的路径为:\n");
buffer.append("(0表示从当前结点起经过左分支到达下一结点,1表示经过右分支)\n");
for(i=0;i<binTree.length;i++)
{
buffer.append("第"+(i+1)+"个结点:"+binTree[i].info+"\n");
length+=binTree[i].info.length()*binTree[i].weight;//计算哈夫曼树带权的路径总长度
}
buffer.append("所得最优二叉树(哈夫曼树)的带权路径总长度为:"+length);
JOptionPane.showMessageDialog(parentComponent,buffer,
"哈夫曼树",JOptionPane.INFORMATION_MESSAGE,new ImageIcon(Library.polyhedronIcont_Scaled));
//显示求解结果
}
//huffmanButton的动作
if(e.getSource()==huffmanHelpButton)
{
ImageIcon helpMessage=new ImageIcon("resource\\HuffmanHelp.GIF");
JOptionPane.showMessageDialog(parentComponent,helpMessage,"输入帮助",JOptionPane.INFORMATION_MESSAGE,new ImageIcon(Library.helpIcon_Scaled));
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -