import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class komvoi
{
private JFrame frame;
private Container contentPane;
private JButton bt1, bt2;
private JTextField tf1,tf2;
private JLabel label,label2,label3,label4,label5,label6;
private JMenuBar menuBar;
private JMenu fileMenu;
private JMenuItem saveas;
private JMenuItem Dijkstra;
private JMenuItem newaddItem;
private JMenuItem quitItem;
private JPanel centerPanel,innorthPanel,northpanel,inCenterPanel, southPanel,centerPanel2,inCenterPanel2,innorthPanel2,northpanel2;
private JTable table1,table2;
private int pinA[][];
private int pinC[][];
private int x;// arithmos komvwn
private int s;//arxikos komvos
private int pind[];//pinakas me tis arxikes apostaseis
private int pinap[];//pinakas arxikopoihseis twn timwn patera
private int pinSS[];//SS(j)=0,2,1 ������� �� �� �� � ������ j
private int pinT[];
// %������ ��� ������, ��� �������� ��� ������� � ���
public komvoi()
{
makeFrame2();
pind= new int[x];
pinap= new int[x];
pinSS= new int[x];
}
public void makeFrame2()
{
Font f;
frame = new JFrame("solver ");
contentPane = frame.getContentPane();
contentPane.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 10));
contentPane.setBackground(Color.blue);
menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
fileMenu = new JMenu("File");
menuBar.add(fileMenu);
newaddItem = new JMenuItem("new");
fileMenu.add(newaddItem);
saveas = new JMenuItem("Save as");
fileMenu.add(saveas);
quitItem = new JMenuItem("quit�");
fileMenu.add(quitItem);
fileMenu = new JMenu("Shortest path");
menuBar.add(fileMenu);
Dijkstra = new JMenuItem("Dijkstra Algorithm");
fileMenu.add(Dijkstra);
Font f1;
northpanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));
innorthPanel = new JPanel(new GridLayout(2,2,5,5));
label2 = new JLabel("give the number of nodes ");
f1 = new Font("TimesRoman", Font.BOLD, 16);
label2.setFont(f1);
innorthPanel.setBackground(Color.blue);
northpanel.setBackground(Color.blue);
label2.setForeground(Color.red);
innorthPanel.add(label2);
tf1 = new JTextField();
tf1.setSize(5,5);
innorthPanel.add(tf1);
label6= new JLabel("give the initial node");
f1 = new Font("TimesRoman", Font.BOLD, 16);
label6.setFont(f1);
label6.setForeground(Color.red);
innorthPanel.add(label6);
tf2 = new JTextField();
tf2.setSize(5,5);
innorthPanel.add(tf2);
northpanel.add(innorthPanel);
contentPane.add(northpanel, BorderLayout.NORTH);
centerPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));
inCenterPanel= new JPanel(new GridLayout(0,1,5,5));
bt1 = new JButton("ok");
inCenterPanel .add(bt1);
centerPanel.setBackground(Color.blue);
inCenterPanel.setBackground(Color.blue);
bt1.setSize(5,5);
centerPanel.add(inCenterPanel);
contentPane.add(centerPanel, BorderLayout.CENTER);
southPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));
label3 = new JLabel("");
southPanel.add(label3);
southPanel.setBackground(Color.blue);
contentPane.add(southPanel, BorderLayout.WEST);
bt1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
contentPane.setLayout(new GridLayout(0,1));
String q = tf1.getText();
x = Integer.parseInt(q);
String a = tf2.getText();
s= Integer.parseInt(a);
if (x<0){
label3.setText("give a number >0");
}
else if (x==0){
label3.setText("give a number >0");
}
else if (x>0) {
label2.setVisible(false);
innorthPanel.remove(label2);
innorthPanel.validate();
label3.setVisible(false);
southPanel.remove(label3);
southPanel.validate();
tf1.setVisible(false);
innorthPanel.remove( tf1);
innorthPanel.validate();
bt1.setVisible(false);
inCenterPanel.remove(bt1);
inCenterPanel.validate();
contentPane.setBackground(Color.blue);
contentPane.remove(southPanel);
contentPane.remove(northpanel);
contentPane.remove(centerPanel);
centerPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
inCenterPanel= new JPanel(new GridLayout(1,2));
label4 = new JLabel("A= ");
inCenterPanel.add(label4);
table1= new JTable(x,x);
table1.setBackground(Color.red);
inCenterPanel.add( table1);
centerPanel.setBackground(Color.blue);
inCenterPanel.setBackground(Color.blue);
centerPanel.add(inCenterPanel);
contentPane.add(centerPanel, BorderLayout. NORTH);
northpanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
innorthPanel = new JPanel(new GridLayout(1,2));
label5 = new JLabel("C= ");
innorthPanel.add(label5);
table2= new JTable(x,x);
table2.setBackground(Color.red);
innorthPanel.add( table2);
northpanel.add(innorthPanel);
innorthPanel.setBackground(Color.blue);
northpanel.setBackground(Color.blue);
contentPane.add(northpanel, BorderLayout.CENTER);
southPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));
bt1 = new JButton("ok");
southPanel .add(bt1);
southPanel.setBackground(Color.blue);
contentPane.add(southPanel, BorderLayout.SOUTH);
bt1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
Dijkstra ();
}
});
}
}
});
frame.pack();
frame.setSize(500 ,300);
frame.setVisible(true);
}
public void Dijkstra ()
{
pinA = new int[x][x];
//System.out.println("x = " + x);
for(int i = 0; i<x; i++)
{
for(int j=0; j<x; j++)
{
pinA[i][j] = Integer.parseInt((String)table1.getValueAt(i,j));
//System.out.println("table1 i= " + i + " j= " + j + "---- " + pinA[i][j]);
}
}
boolean flag = true;
for (int i=0; i<x; i++)
{
for(int j=0; j<x; j++)
{
if ( i==j && pinA[i][j]!=0){
System.out.println("ta diagvnia stoixeia tou pinaka A einai diafwra tou midenos ");
flag = false;
}
}
}
for(int i = 0; i<x; i++)
{
for(int j=0; j<x; j++)
{
if ( (pinA[i][j]!=0) && (pinA[i][j]!=1) ){
System.out.println(" o pinakas A den einai mitra geitonias ");
}
}
}
for(int i = 0; i<x; i++)
{
for(int j=0; j<x; j++)
{
System.out.print(pinA[i][j] + ", ");
}
System.out.println();
}
pinC = new int[x][x];
for(int i = 0; i<x; i++)
{
for(int j=0; j<x; j++)
{
pinC[i][j] = Integer.parseInt((String)table2.getValueAt(i,j));
}
}
for(int i = 0; i<x; i++)
{
for(int j=0; j<x; j++)
{
System.out.print(pinC[i][j] + ", ");
}
System.out.println();
}
for(int i=0; i<x; i++)
{
pind[i]= 1000;
pind[s]=s;
System.out.print(pind[i] + ", ");
}
for(int i=0; i<x; i++)
{
pinap[i]= 0;
pinap[s]=s;
System.out.print(pinap[i] + ", ");
}
for(int i=0; i<x; i++)
{
pinSS[i]= 1;
pinSS[s]=0;
System.out.print(pinSS[i] + ", ");
}
pinT[s]=s;
}
}