⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 graphinput.java

📁 用Java开发的实用数学建模程序 简单易懂 初学者可以用来学习java知识
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 *@(#)GraphInput.java 2.0 2005/05/02
 *
 *清华大学 精密仪器与机械学系
 *范灿升 fancansheng@163.com
 */

package input;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.util.Vector;
//导入系统类

import lib.Library;
import algorithm.GraphTheory;
import ADT.BinaryTree;
//导入自定义的类

/**
 *该类所提供的方法是供给类{@link Modeling}调用的,用以产生图论算法的输入界面。
 *<p>为提高计算精确度,图中各矩阵元素必须是整型数,而不能是浮点数,
 *用户在输入前必须对待处理的图进行一下加工。
 *@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是结点数
	private int m=0;//m是边数
	private int[][] weightMatrix;//权矩阵
	private int[] leafVertex;//哈夫曼树树叶结点的权
	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;
	
	/**
	 *数据在该面板上输入,主要是邻接矩阵、关联矩阵等。
	 */
	public JPanel inputPanel;
	
	/**
	 *用来放置计算按钮和帮助按钮等。
	 */
	public JPanel computePanel;
	
	/**
	 *用来放置inputPanel的容器。
	 */
	public JPanel mainPanel;

	/**
	 *产生包含图的矩阵相关参数的类。
	 *@param parentComponent	输入面板的上一层组件
	 *@param inputPanel			数据在该面板上输入
	 *@param computePanel		面板上的按钮为计算用按钮
	 *@param mainPanel			mainPanel是用来放置inputPanel的容器
	 */
	public GraphInput(Component parentComponent,JPanel inputPanel,JPanel computePanel,JPanel mainPanel)
	{
		this.parentComponent=parentComponent;
		this.inputPanel=inputPanel;
		this.computePanel=computePanel;
		this.mainPanel=mainPanel;
	}
	
	/**
	 *显示输入待计算最短路的图的矩阵的界面。
	 *<p>用户从该界面输入图的权矩阵。
	 */
	public void showDijkstra()
	{
		boolean pass=false;//用于标记是否通过合法性检验
		boolean secondTime=false;
		String tmp;
		
		while(!pass)
		{
			if(secondTime==true)
				tmp=JOptionPane.showInputDialog(parentComponent,"请输入一个正整数!\n请重新输入:","输入结点数",JOptionPane.WARNING_MESSAGE);
			else
				tmp=JOptionPane.showInputDialog(parentComponent,"请输入图中结点的数目:","输入结点数",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];
		//输入结点数并形成权矩阵
		
		GridLayout weightGridLayout=new GridLayout(n,n);
		inputPanel.setLayout(weightGridLayout);
		TitledBorder border=new TitledBorder("输入有向正权图的权矩阵");
		border.setTitleFont(Library.font);
		inputPanel.setBorder(border);
		//设置权矩阵布局
		
		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);
		//不能含有自环
		
		JScrollPane scrollPane=new JScrollPane(inputPanel);
		BoxLayout computePanelLayout=new BoxLayout(computePanel,BoxLayout.X_AXIS);
		computePanel.setLayout(computePanelLayout);
		
		dijkstraButton=new JButton("计算两点间的最短路",new ImageIcon(Library.polyhedronIcont_Scaled));
		dijkstraButton.setFont(Library.font);
		computePanel.removeAll();
		computePanel.add(dijkstraButton);
		fillSymmetric=new JButton("自动填充为对称矩阵",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);
	}
	
	/**
	 *显示输入待计算求最短哈密顿回路的图的权矩阵界面(即旅行商问题,又称推销员问题)。
	 *<p>用户从该界面输入图的权矩阵。该图必须是无向完全图。
	 */
	public void showHamilton()
	{
		boolean pass=false;//用于标记是否通过合法性检验
		boolean secondTime=false;
		String tmp;
		
		while(!pass)
		{
			if(secondTime==true)
				tmp=JOptionPane.showInputDialog(parentComponent,"请输入一个不小于3的正整数!\n请重新输入:","输入结点数",JOptionPane.WARNING_MESSAGE);
			else
				tmp=JOptionPane.showInputDialog(parentComponent,"请输入图中结点的数目:","输入结点数",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];
		//输入结点数并形成权矩阵
		
		GridLayout weightGridLayout=new GridLayout(n,n);
		inputPanel.setLayout(weightGridLayout);
		TitledBorder border=new TitledBorder("输入无向完全图的权矩阵");
		border.setTitleFont(Library.font);
		inputPanel.setBorder(border);
		//设置权矩阵布局
		
		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);
		}
		//保证输入为无向图
		for(i=0;i<n;i++)
			weightTextField[i][i].setText("0");
		//不能含有自环
		
		JScrollPane scrollPane=new JScrollPane(inputPanel);
		FlowLayout computePanelLayout=new FlowLayout();
		computePanel.setLayout(computePanelLayout);
		
		hamiltonButton=new JButton("计算最短哈密顿回路",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());
					}
				}
			}
		);//自动填充成对称矩阵
		
		parentComponent.setSize(parentComponent.getPreferredSize());
		parentComponent.setVisible(true);
	}
	
	/**
	 *显示输入待构造哈夫曼树的树叶结点的权的输入界面。
	 *<p>用户从该界面输入树叶结点的权。
	 */
	public void showHuffman()
	{
		boolean pass=false;//用于标记是否通过合法性检验
		boolean secondTime=false;
		String tmp;
		
		while(!pass)
		{
			if(secondTime==true)
				tmp=JOptionPane.showInputDialog(parentComponent,"请输入一个不小于2的正整数!\n请重新输入:","输入树叶结点数",JOptionPane.WARNING_MESSAGE);
			else
				tmp=JOptionPane.showInputDialog(parentComponent,"请输入哈夫曼树的树叶结点的数目:","输入树叶结点数",JOptionPane.QUESTION_MESSAGE);
			try
			{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -