写一个关于关键路径的小程序,功能基本上都差不多了,但是想画图的时候总是出错,一是画图的时候图无法显示,不知道现在在哪,二就是一写上Graphics的函数的时候程序虽然运行正常但是总是会抛出异常,也不知道为什么。
各位大牛能不能给点建议,举个例子我该怎么弄。我贴的程序是未写上Graphics的程序
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.PrintWriter;
import java.security.SecureRandom;
import java.util.StringTokenizer;import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.filechooser.FileFilter;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
public class Aoe {
public static int vertex, edge;// 点和边的数目
public static int[][] array;// 存放耗费
public static JTextArea relation, out;// ///显示关系和结果的文本框
public static JScrollPane jsp1, jsp2;//滚动条
static Graphics g;
////////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////// AOE主窗口
public static void aoe() {
final JFrame f = new JFrame("AOE查询系统");
f.setLocation(200, 120);
f.setSize(400, 640);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(null);
// //菜单栏
JMenuBar mb=new JMenuBar();
JMenu memoMenu1=new JMenu("文件");
JMenu memoMenu2=new JMenu("编辑");
mb.add(memoMenu1);
mb.add(memoMenu2);
f.add(mb);
JMenuItem Open,New,Save,Add,Delete;
Open=new JMenuItem("打开");
Open.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
open(relation);
}
});
memoMenu1.add(Open);
New=new JMenuItem("新建");
New.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
vertex = 0;
newfile(relation);
}
});
memoMenu1.add(New);
Save=new JMenuItem("保存");
Save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
relation.setEditable(false);
save(relation);
}
});
memoMenu1.add(Save);
Add=new JMenuItem("添加");
Add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
editadd(relation);
}
});
memoMenu2.add(Add);
Delete=new JMenuItem("删除");
Delete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
editdelete(relation);
}
});
memoMenu2.add(Delete);
f.setJMenuBar(mb);
////////////////////////////
////////////////////////////
///////////////////////关系图
relation = new JTextArea();
relation.setEditable(false);
JLabel lb = new JLabel("当前 关 系");
lb.setBounds(10, 10, 100, 20);
f.add(lb); jsp1 = new JScrollPane(relation);
jsp1.setBounds(10, 30, 200, 250);
f.add(jsp1);
///////////////////////////////////
/////////////////////////////////
//////////////////////////显示结果
JLabel lb2 = new JLabel("查 询 结 果");
lb2.setBounds(10, 290, 100, 30);
f.add(lb2);
out = new JTextArea();
out.setEditable(false);
jsp2 = new JScrollPane(out);
jsp2.setBounds(10, 320, 340, 230);
f.add(jsp2);
/////////////////////////////////////
////////////////////////////////////
////////////////////////////////按钮部分
final JButton Search=new JButton("查询");
JButton Back=new JButton("退出");
Search.setBounds(250, 30, 100, 30);
Back.setBounds(250, 90, 100, 30);
f.add(Search);
f.add(Back);
Back.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
f.dispose();
// back();
}
});
Search.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
search();
}
});
f.setVisible(true);
}
/////////////////////////////////////////////
/////////////////////////////////////////////
/////////////////////////////////创建新关系图
public static int[] path;//
public static void CreatGraph(int v) {
edge = 0;
vertex = v;
array = new int[vertex][vertex];
path = new int[vertex]; for (int ii = 0; ii < vertex; ii++) {
path[ii] = 0;
for (int jj = 0; jj < vertex; jj++)
array[ii][jj] = 0;
}
} // //////////////////////////////////////////////////
// ///////////////////////////////////////////////
// //////////////////////////////////重构二维数组
public static int[] InDegree;// 每个点的入度
public static void Data() {
int[][] b = new int[vertex - 1][vertex - 1];
for (int i = 0; i < (vertex) - 1; i++)
for (int j = 0; j < (vertex) - 1; j++)
b[i][j] = array[i][j];
array = new int[vertex][vertex];
path = new int[vertex];
for (int ii = 0; ii < (vertex - 1); ii++) {
path[ii] = 0;
for (int jj = 0; jj < (vertex - 1); jj++)
array[ii][jj] = b[ii][jj];
}
InDegree = new int[vertex + 1];
} /////////////////////////////////////////////////
// ///////////////////////////////////////////
//////////////////////////////////////////打开
各位大牛能不能给点建议,举个例子我该怎么弄。我贴的程序是未写上Graphics的程序
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.PrintWriter;
import java.security.SecureRandom;
import java.util.StringTokenizer;import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.filechooser.FileFilter;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
public class Aoe {
public static int vertex, edge;// 点和边的数目
public static int[][] array;// 存放耗费
public static JTextArea relation, out;// ///显示关系和结果的文本框
public static JScrollPane jsp1, jsp2;//滚动条
static Graphics g;
////////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////// AOE主窗口
public static void aoe() {
final JFrame f = new JFrame("AOE查询系统");
f.setLocation(200, 120);
f.setSize(400, 640);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(null);
// //菜单栏
JMenuBar mb=new JMenuBar();
JMenu memoMenu1=new JMenu("文件");
JMenu memoMenu2=new JMenu("编辑");
mb.add(memoMenu1);
mb.add(memoMenu2);
f.add(mb);
JMenuItem Open,New,Save,Add,Delete;
Open=new JMenuItem("打开");
Open.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
open(relation);
}
});
memoMenu1.add(Open);
New=new JMenuItem("新建");
New.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
vertex = 0;
newfile(relation);
}
});
memoMenu1.add(New);
Save=new JMenuItem("保存");
Save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
relation.setEditable(false);
save(relation);
}
});
memoMenu1.add(Save);
Add=new JMenuItem("添加");
Add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
editadd(relation);
}
});
memoMenu2.add(Add);
Delete=new JMenuItem("删除");
Delete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
editdelete(relation);
}
});
memoMenu2.add(Delete);
f.setJMenuBar(mb);
////////////////////////////
////////////////////////////
///////////////////////关系图
relation = new JTextArea();
relation.setEditable(false);
JLabel lb = new JLabel("当前 关 系");
lb.setBounds(10, 10, 100, 20);
f.add(lb); jsp1 = new JScrollPane(relation);
jsp1.setBounds(10, 30, 200, 250);
f.add(jsp1);
///////////////////////////////////
/////////////////////////////////
//////////////////////////显示结果
JLabel lb2 = new JLabel("查 询 结 果");
lb2.setBounds(10, 290, 100, 30);
f.add(lb2);
out = new JTextArea();
out.setEditable(false);
jsp2 = new JScrollPane(out);
jsp2.setBounds(10, 320, 340, 230);
f.add(jsp2);
/////////////////////////////////////
////////////////////////////////////
////////////////////////////////按钮部分
final JButton Search=new JButton("查询");
JButton Back=new JButton("退出");
Search.setBounds(250, 30, 100, 30);
Back.setBounds(250, 90, 100, 30);
f.add(Search);
f.add(Back);
Back.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
f.dispose();
// back();
}
});
Search.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
search();
}
});
f.setVisible(true);
}
/////////////////////////////////////////////
/////////////////////////////////////////////
/////////////////////////////////创建新关系图
public static int[] path;//
public static void CreatGraph(int v) {
edge = 0;
vertex = v;
array = new int[vertex][vertex];
path = new int[vertex]; for (int ii = 0; ii < vertex; ii++) {
path[ii] = 0;
for (int jj = 0; jj < vertex; jj++)
array[ii][jj] = 0;
}
} // //////////////////////////////////////////////////
// ///////////////////////////////////////////////
// //////////////////////////////////重构二维数组
public static int[] InDegree;// 每个点的入度
public static void Data() {
int[][] b = new int[vertex - 1][vertex - 1];
for (int i = 0; i < (vertex) - 1; i++)
for (int j = 0; j < (vertex) - 1; j++)
b[i][j] = array[i][j];
array = new int[vertex][vertex];
path = new int[vertex];
for (int ii = 0; ii < (vertex - 1); ii++) {
path[ii] = 0;
for (int jj = 0; jj < (vertex - 1); jj++)
array[ii][jj] = b[ii][jj];
}
InDegree = new int[vertex + 1];
} /////////////////////////////////////////////////
// ///////////////////////////////////////////
//////////////////////////////////////////打开
JFileChooser fc = new JFileChooser("src");
if (t == relation)
{ out.setText("");fc.setFileFilter(new TxtFileFilter());}
int i = fc.showOpenDialog(null);
File f;
if (i == JFileChooser.APPROVE_OPTION) {
f = fc.getSelectedFile();
try {
String filename = f.getPath();
try {
BufferedReader input = new BufferedReader(new FileReader(
filename));
t.setText("");
String line = null, a = null, b = null;
String value = null;
int x, y;
line =input.readLine();
vertex = Integer.parseInt(line);
CreatGraph(vertex);
InDegree = new int[vertex + 1];
System.out.println(line);
line =input.readLine();
System.out.println(line);
while (line != null) {
x = line.indexOf("-");
y = line.indexOf(":");
a = line.substring(0, x);
b = line.substring(x + 1, y);
value = line.substring(y + 1);
t.append(line + "\n");
array[Integer.parseInt(b) - 1][Integer.parseInt(a) - 1] = Integer
.parseInt(value);
line =input.readLine();
}
} catch (Exception e) {
if (t == relation)
out.setText("您选择的文件格式不对或内容无法识别");
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
} // //////////////////////////////////////////////
// /////////////////////////////////////
public static void newfile(final JTextArea t)//////新建的界面,将relation设置为可编辑
{
if (vertex == 0) {
final JFrame frame = new JFrame();
frame.setTitle("新建一个AOE网络");
frame.setSize(385, 265);
frame.setLocation(600, 230);
frame.setLayout(null);
JLabel jl = new JLabel("请输入事件数");
jl.setBounds(10, 10, 80, 60);
final JTextField tf = new JTextField(8);
tf.setBounds(100, 10, 110, 60);
final JButton jb1 = new JButton("确认");
JButton jb2 = new JButton("返回"); jb1.setBounds(230, 10, 110, 60);
jb2.setBounds(240, 150, 100, 60);
frame.add(tf);
frame.add(jb1);
frame.add(jb2);
frame.add(jl);
frame.setVisible(true);
jb1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {JOptionPane.showMessageDialog(null, "已输入事件数",
null, -1); vertex = Integer.parseInt(tf.getText());
CreatGraph(vertex);
InDegree = new int[vertex + 1];
} catch (Exception ee) {
Exception4();
}
}
});
jb2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.dispose();
}
});
final JTextField jtf1 = new JTextField(4);
jtf1.setBounds(60, 80, 40, 60);
final JTextField jtf2 = new JTextField(4);
jtf2.setBounds(175, 80, 40, 60);
final JTextField jtf3 = new JTextField(4);
jtf3.setBounds(290, 80, 40, 60);
JLabel lb1, lb2, lb3;
lb1 = new JLabel("开始事件");
lb2 = new JLabel("结束事件");
lb3 = new JLabel("期间耗费");
lb1.setBounds(10, 80, 60, 60);
lb2.setBounds(115, 80, 60, 60);
lb3.setBounds(230, 80, 60, 60);
JButton button1 = new JButton("确认");
JButton button2 = new JButton("完成");
button1.setBounds(10, 150, 100, 60);
button2.setBounds(125, 150, 100, 60);
frame.add(lb1);
frame.add(lb2);
frame.add(lb3);
frame.add(jtf1);
frame.add(jtf2);
frame.add(jtf3);
frame.add(button1);
frame.add(button2);
frame.setVisible(true);
// ///////////////////////////
// /////////////////////
// /////////////////////
button1.addActionListener(new ActionListener() {// /确认按钮的监听
public void actionPerformed(ActionEvent e) {
int a, b, c;
String aa = jtf1.getText(), bb =jtf2.getText() ,
cc = jtf3.getText();
try {
a = Integer.parseInt(aa);
b = Integer.parseInt(bb);
c = Integer.parseInt(cc);
if (c < 0||a<0||b<0)
NegertiveInputException();
else {
if (a>vertex||b>vertex)
ActiveInputException();
else {
if (a == b) {
BadInputException(a);// 选择的是同一个点
} else {
if (t == relation) {
array[b - 1][a - 1] = c;
}
g.drawLine(1, 2, 10, 20);
}
}
jtf1.setText(null);
jtf2.setText(null);
jtf3.setText(null);
}
} catch (Exception ee) {
Exception3();
}
t.setText("");
Output(t);
} private void NegertiveInputException() {
JOptionPane.showMessageDialog(null, "耗费不能为负",
null, 0); }
public void ActiveInputException(){
JOptionPane.showMessageDialog(null, "超过已确定的最大定点数,请重新输入",
null, 0); }
private void BadInputException(int a) {
JOptionPane.showMessageDialog(null, "该点到其本身耗费默认为0", null,
0);
}
});
button2.addActionListener(new ActionListener() { // //完成的监听
public void actionPerformed(ActionEvent e) {
frame.dispose();
t.setText("");
Output(t);
}
});
}
}
////////////////////////////////////////////
//////////////////////添加的界面
public static void editadd(final JTextArea t){
final JFrame addframe=new JFrame();
addframe.setTitle("添加");
addframe.setSize(340, 150);
addframe.setLocation(600, 230);
addframe.setLayout(null);
final JLabel addjb1=new JLabel("开始事件");
addjb1.setBounds(10, 10, 60, 35);
final JLabel addjb2=new JLabel("结束事件");
addjb2.setBounds(110, 10, 60, 35);
final JLabel addjb3=new JLabel("耗费");
addjb3.setBounds(210, 10, 60, 35);
final JTextField addjtf1 = new JTextField(4);
addjtf1.setBounds(70, 10, 40, 35);
final JTextField addjtf2 = new JTextField(4);
addjtf2.setBounds(170, 10, 40, 35);
final JTextField addjtf3 = new JTextField(4);
addjtf3.setBounds(270, 10, 40, 35);
JButton addbt1 = new JButton("添加顶点");
JButton addbt2 = new JButton("写入");
JButton addbt3 = new JButton("完成");
addbt1.setBounds(10, 55, 90, 35);
addbt2.setBounds(115,55, 90, 35);
addbt3.setBounds(220, 55, 90, 35);
addframe.add(addjb1);
addframe.add(addjb2);
addframe.add(addjb3);
addframe.add(addjtf1);
addframe.add(addjtf2);
addframe.add(addjtf3);
addframe.add(addbt1);
addframe.add(addbt2);
addframe.add(addbt3);
addframe.setVisible(true);
addbt1.addActionListener(new ActionListener() {//添加按钮的监听
public void actionPerformed(ActionEvent e) {
String s="新添加事件",
ss=Integer.toString(vertex+1);
s=s+ss;
vertex=vertex+1;
Data();
JOptionPane.showMessageDialog(null, s, null, -1);
}});
addbt2.addActionListener(new ActionListener() {// /写入按钮的监听
public void actionPerformed(ActionEvent e) {
int adda, addb, addc;
String addaa = addjtf1.getText(), addbb =addjtf2.getText() ,
addcc = addjtf3.getText();
try {
adda = Integer.parseInt(addaa);
addb = Integer.parseInt(addbb);
addc = Integer.parseInt(addcc);
if (addc < 0||adda<0||addb<0)
NegertiveInputException();
else {
if (adda>vertex||addb>vertex)
ActiveInputException();
else {
if (adda == addb) {
BadInputException(adda);// 选择的是同一个点
} else {
if (t == relation) {
array[addb - 1][adda - 1] =addc;
}
}
}
addjtf1.setText(null);
addjtf2.setText(null);
addjtf3.setText(null);
}
} catch (Exception ee) {
Exception3();
}
t.setText("");
Output(t);
} private void NegertiveInputException() {
JOptionPane.showMessageDialog(null, "耗费不能为负,请重新输入",
null, -1); }
public void ActiveInputException(){
JOptionPane.showMessageDialog(null, "超过已确定的最大定点数,请重新输入",
null, -1); }
private void BadInputException(int a) {
JOptionPane.showMessageDialog(null, "该点到其本身耗费默认为0", null,
-1);
}
});
addbt3.addActionListener(new ActionListener() { // //完成的监听
public void actionPerformed(ActionEvent e) {
addframe.dispose();
t.setText("");
Output(t);
}
});
}
//////////////////////////////
/////////////删除的界面
public static void editdelete(final JTextArea t){
final JFrame deframe=new JFrame();
deframe.setTitle("删除");
deframe.setSize(250, 160);
deframe.setLocation(600, 110);
deframe.setLayout(null);
final JLabel dejb1=new JLabel("开始事件");
dejb1.setBounds(10, 10, 60, 40);
final JLabel dejb2=new JLabel("结束事件");
dejb2.setBounds(120, 10, 60, 40);
final JTextField dejtf1 = new JTextField(4);
dejtf1.setBounds(70, 10, 40, 40);
final JTextField dejtf2 = new JTextField(4);
dejtf2.setBounds(180, 10, 40, 40);
JButton debt1 = new JButton("删除");
JButton debt2 = new JButton("完成");
debt1.setBounds(10, 60, 100, 40);
debt2.setBounds(120, 60, 100, 40);
deframe.add(dejb1);
deframe.add(dejb2);
deframe.add(dejtf1);
deframe.add(dejtf2);
deframe.add(debt1);
deframe.add(debt2);
deframe.setVisible(true);
debt1.addActionListener(new ActionListener() {// 删除按钮
public void actionPerformed(ActionEvent e) {
int dea, deb, dec;
String deaa = dejtf1.getText(), debb =dejtf2.getText() ;
dea = Integer.parseInt(deaa);
deb = Integer.parseInt(debb);
if (dea ==deb) {
for (int i = 0; i < vertex; i++) {
array[i][dea - 1] = 0;
array[dea - 1][i] = 0;
}
} else {
array[deb - 1][dea - 1] = 0;
}
}
});
debt2.addActionListener(new ActionListener() { // //完成的监听
public void actionPerformed(ActionEvent e) {
deframe.dispose();
t.setText("");
Output(t);
}
});
}
// /////////////////////////////////
// ////////////////////////
// ///////////////////////////
private static void Output(JTextArea t) {// /在tas上以标准形式输出关系网络
for (int i = 0; i < vertex; i++)
for (int j = 0; j < vertex; j++)
if (array[j][i] != 0)
t
.append((i + 1) + "-" + (j + 1) + ":" + array[j][i]
+ "\n");
if (t == relation)
out.setText(null);
}
// ////////////////////////
// ////////////////////////
// ///////////////////////
public static void save(JTextArea t) {// /////保存界面
String s = t.getText();
try {
vertex = StrMethod.lll(s);
String string = s;
StringTokenizer st = new StringTokenizer(string, "\n");
String key = st.nextToken();// 分析key
String a = null, b = null;
String value = null;
int x, y;
CreatGraph(vertex);
InDegree = new int[vertex + 1];
while (st.hasMoreTokens()) {
try {
x = key.indexOf("-");
y = key.indexOf(":");
a = key.substring(0, x);
b = key.substring(x + 1, y);
value = key.substring(y + 1);
if (array[Integer.parseInt(b) - 1][Integer.parseInt(a) - 1] != 0) {
Exception2(Integer.parseInt(value),
Integer.parseInt(b) - 1,
Integer.parseInt(a) - 1, t);
} else {
array[Integer.parseInt(b) - 1][Integer.parseInt(a) - 1] = Integer
.parseInt(value);
}
key = st.nextToken();
} catch (Exception e) {
Exception();
}
}
x = key.indexOf("-");
y = key.indexOf(":");
a = key.substring(0, x);
b = key.substring(x + 1, y);
value = key.substring(y + 1);
array[Integer.parseInt(b) - 1][Integer.parseInt(a) - 1] = Integer
.parseInt(value);
// ///////////////////
// /////////////////////
// //////////////////
PrintWriter outputstream = null;
JFileChooser j2 = new JFileChooser();
if (t == relation)
j2.setFileFilter(new TxtFileFilter());
j2.setCurrentDirectory(null);// 设置打开对话框的默认路径
int n = j2.showSaveDialog(null);
File file = j2.getSelectedFile();
String sss = file.getPath();
if (t == relation)
sss = sss + ".txt";
if (n == JFileChooser.APPROVE_OPTION) {
try {
FileOutputStream output = new FileOutputStream(sss);
outputstream = new PrintWriter(output);
} catch (FileNotFoundException e1) {
}
String vertexNum = Integer.toString(vertex);
outputstream.println(vertexNum);
String string1 = s;
StringTokenizer st1 = new StringTokenizer(string1, "\n");
String key1 = st1.nextToken();
String jiami;
for (; st1.hasMoreTokens(); key1 = st1.nextToken()) {
jiami=key1;
outputstream.println(jiami);
}
jiami=key1;
outputstream.println(jiami);
outputstream.close();
}
} catch (Exception e) {// Exception();
}
} // ////////////////////////////////////
// ////////////////////////////////////////////
// ////////////处理异常
private static void Exception2(final int i, final int a, final int b,final JTextArea t) {
}
private static void Exception() {// ////输入格式有误
JOptionPane.showMessageDialog(null, "输入的格式有误",
null, -1);
}
private static void Exception5() {// //起始事件不唯一
out.setText("源点不唯一,请重新输入");
} private static void Exception3() {// //// 没有输入两者的耗费
JOptionPane.showMessageDialog(null, " 输入不完整", null, -1);
} private static void Exception4() {// //// 没有输入点的个数
JOptionPane.showMessageDialog(null, " 输入不完整", null, -1);
} // ///////////////////////////计算出入度
// //////////////////////计算顶点的出入度
public static int Outdegree(int i) {
int outdegree = 0;
for (int k = 0; k < vertex; k++)
if (array[k][i - 1] != 0)
outdegree++;
return outdegree;
} public static int Indegree(int j) {
int indegree = 0;
for (int k = 0; k < vertex; k++)
if (array[j - 1][k] != 0)
indegree++;
return indegree;
}
//////////////////////////
////////////////////// 拓扑排序
public static boolean topological(int v[]) {
int n = vertex;
Stack s = new Stack(n);
int i;
for (i = 1; i <= n; i++)
if (InDegree[i] == 0)
s.push(i);
i = 0;
while (!s.isEmpty()) {
int w;
w = s.pop();
v[i++] = w;
for (int ii = 1; ii <= n; ii++) {
if (array[ii - 1][w - 1] != 0)
InDegree[ii]--;
if (InDegree[ii] == 0) {
s.push(ii);
InDegree[ii] = -1;
}
}
}
i--;
return (i == vertex);
} /////////////////////////////////////////
///////////////////////////////////////
////////////////// C表示两点间的最长距离
public static void longpath(int[][] c, int[][] kay) {
for (int i = 1; i <= vertex; i++)
for (int j = 1; j <= vertex; j++) {
c[i][j] = array[i - 1][j - 1];
kay[i][j] = 0;
}
for (int i = 1; i <= vertex; i++)
c[i][i] = 0;
// ////////////计算c【i】【j】=c(i,j,k)
for (int k = 1; k <= vertex; k++)
for (int i = 1; i <= vertex; i++)
for (int j = 1; j <= vertex; j++) {
int t1, t2, t3;
t1 = c[i][k];
t2 = c[k][j];
t3 = c[i][j];
if (t1 != 0 && t2 != 0 && (t3 == 0 || t1 + t2 > t3)) {
c[i][j] = t1 + t2;
kay[i][j] = k;
}
}
} ///////////////////////////////////
///////////////////////////////////
////////////////////// C表示两点间的最短距离
public static void ShotLength(int[][] c, int[][] kay) {
for (int i = 1; i <= vertex; i++)
for (int j = 1; j <= vertex; j++) {
c[i][j] = array[i - 1][j - 1];
kay[i][j] = 0;
}
for (int i = 1; i <= vertex; i++)
c[i][i] = 0;
for (int k = 1; k <= vertex; k++)
for (int i = 1; i <= vertex; i++)
for (int j = 1; j <= vertex; j++) {
int t1, t2, t3;
t1 = c[i][k];
t2 = c[k][j];
t3 = c[i][j];
if (t1 != 0 && t2 != 0 && (t3 == 0 || t1 + t2 < t3)) {
c[i][j] = t1 + t2;
kay[i][j] = k;
}
}
}
// //////////////////////////////////////////
// //////////////////////////////////输出路径
// ////////////////////////////////输出最长路径
public static String Shortpath;// 用于输出路径
public static void OutputPath(int kay[][], int i, int j) {
if (i == j)
return;
if (kay[i][j] == 0) {
Shortpath = Integer.toString(j) + "-" + Shortpath;
} else {
OutputPath(kay, i, kay[i][j]);
OutputPath(kay, kay[i][j], j);
}
} public static void Path(int c[][], int kay[][], int i, int j) {
if (c[i][j] == 0) {
System.out.println("从" + i + "到" + j + "最短路径为");
return;
}
Shortpath = Integer.toString(i);
OutputPath(kay, i, j);
} // /////////////////////////////
// /////////////////////////////////
private static int min(int a, int b) {
if (a < b)
return a;
else
return b;
} // ///////////////////////////////////
// ///////////////////////////////
public static int vt[];// 用于表示活动的最晚发生时间
public static int vl[];// 用于表示事件的最晚发生时间
public static boolean ve[];// 用于由逆拓扑序列表示点i的最晚发生时间是否已得到
public static void lastest(int c[][], int v[]) {// 计算各个点的最早、最晚发生时间,
// 并将其保存在vt中
ve = new boolean[vertex + 1];
for (int i = 1; i <= vertex; i++)
ve[i] = false;
int index = vertex - 1;
vt = new int[vertex + 1];
vl = new int[vertex + 1];
ve = new boolean[vertex + 1];
ve[v[index]] = true;// //////////////
// ///////////////////
int ceshi = 1;
for (; ceshi <= vertex; ceshi++) {
if (Indegree(ceshi) == 0)
break;
}
// ///////////////////
vt[v[index]] = c[v[index]][1];
for (int i = 1; i < vertex; i++) {
index--;
int num = v[index];
vt[num] =100;
int j = 0;
// /////////////////////
while (j < vertex) {// ////////////////////////
if (array[j][num - 1] != 0) {
if (ve[j + 1]) {
vl[num] = vt[num];
vt[num] = min(vt[num], vt[j + 1] - array[j][num - 1]);
ve[num] = true;
}
}
j++;
}
}
} // ///////////////////////////////////
// ///////////////////////////
// ///////////////////////////////
public static void time(int c[][], int[] path) {// //用于AOE中,显示关键路径一级活动的最早、最晚发生时间
out.setText("");
int index = 0;
for (int i = 0; i < vertex; i++) {
for (int j = 0; j < vertex; j++)
if (array[j][i] != 0) {
int jj = j + 1;
int ii = i + 1;
int last = vt[jj] - array[j][i]; String s = null;
s = "活动" + ii + "-->" + jj + "\t Ve:" + c[ii][1]
+ " Vl:" + last;
if (c[ii][1] == last) {
path[index++] = ii;
s = s + " 关键活动";
}
out.append(s + "\n");
}
}
out.append("关键路径为:" + Shortpath);
} // ///////////////////////////////////
// /////////////////////////////////////
// ////////////////////////////////////////
// ////////////////////////////////////
private static void search() {// ///AOE查询方法
// output();
int j = 0, k = 0;
try {
System.out.println("各个顶点的入度为:");
for (int x = 1; x < vertex + 1; x++) {
InDegree[x] = Indegree(x);
System.out.print(InDegree[x] + "**");
if (InDegree[x] == 0)
j++;
if (Outdegree(x) == 0)
k++;
}
int[] v = new int[vertex + 1];
boolean bool = topological(v);
if (bool) { for (int i = 0; i < vertex; i++)
System.out.print(v[i] + " ");
System.out.println();
int[][] c = new int[vertex + 1][vertex + 1];
int kay[][] = new int[vertex + 1][vertex + 1];
int[] path = new int[vertex];
for (int i = 0; i < vertex; i++)
path[i] = 0;
longpath(c, kay);
Path(c, kay, vertex, 1);// /////输出最长路径
if (Indegree(1) == 0) {
lastest(c, v);// //////计算各个弧、点的最早、最晚发生时间
time(c, path);
} else {
out.setText("无法识别");
}
} else
out.setText("无法形成拓扑序列");
} catch (Exception e) {
if (j > 1)
Exception5();
if (k > 1)
out.setText("汇点不唯一,请重新输入");
}
} // /////////////////////
public static void main(String args[]) {
aoe();
}
}// ///////////////////
// ///////////////////////////
// /////////////////////////////
class TxtFileFilter extends FileFilter {
public boolean accept(File f) {
return f.getPath().toLowerCase().endsWith(".txt") || f.isDirectory();
} public String getDescription() {
return "txt 文件";
}
}
谢谢了