Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package placeholder;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JScrollPane;
- import javax.swing.JTextArea;
- import javax.swing.JTextField;
- import javax.swing.ScrollPaneConstants;
- public class WarshallProgram extends JFrame implements ActionListener {
- JLabel titleLabel;
- JTextField input[][] = new JTextField[4][4];
- JButton computeBtn;
- JButton clearBtn;
- JScrollPane scroll;
- JTextArea resultArea;
- String result = "";
- public WarshallProgram() {
- super("Warshall Program");
- setLayout(null);
- int r = 20;
- titleLabel = new JLabel("Warshall Algorithm");
- computeBtn = new JButton("Calculate");
- clearBtn = new JButton("Clear");
- resultArea = new JTextArea("");
- scroll = new JScrollPane(resultArea);
- scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
- titleLabel.setBounds(20, 20, 500, 20);
- computeBtn.setBounds(300, 75, 100, 40);
- clearBtn.setBounds(300, 150, 100, 40);
- scroll.setBounds(20, 225, 400, 150);
- resultArea.setLineWrap(true);
- resultArea.setWrapStyleWord(true);
- resultArea.setEditable(false);
- for (int i = 0; i < 4; i++) {
- int c = 0;
- r += 40;
- for (int j = 0; j < 4; j++) {
- c += 60;
- input[i][j] = new JTextField("");
- input[i][j].setBounds(c, r, 30, 25);
- add(input[i][j]);
- if(i==j){
- input[i][j].setText("0");
- input[i][j].setEditable(false);
- }
- }
- }
- add(clearBtn);
- add(titleLabel);
- add(computeBtn);
- add(scroll);
- clearBtn.addActionListener(this);
- computeBtn.addActionListener(this);
- setVisible(true);
- setSize(475, 450);
- setLocation(300, 100);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- if (e.getSource() == computeBtn) {
- int n=0;
- for(int i=0; i<4; i++){
- for(int j=0; j<4; j++){
- if ((input[i][j].getText().equals("1")) || (input[i][j].getText().equals("0"))){
- n+=1;
- }
- }
- }
- if(n!=16){
- JOptionPane.showMessageDialog(this, "Invalid input. Please enter 1 or 0 only.",
- "Error", JOptionPane.ERROR_MESSAGE);
- }
- else{
- result = "Result: \n";
- int inputs[][] = new int[4][4];
- for(int i=0; i<4; i++){
- for(int j=0; j<4; j++){
- inputs[i][j] = Integer.parseInt(input[i][j].getText());
- }
- }
- //for testing
- //int sample[][] = {{0, 0, 1, 0}, {1, 0, 0, 1}, {0, 0, 0, 0}, {0, 1, 0, 0}};
- WarshallSolution(inputs, 4);
- resultArea.setText(result);
- }
- }
- if (e.getSource() == clearBtn) {
- for (int i = 0; i < 4; i++) {
- for (int j = 0; j < 4; j++) {
- input[i][j].setText("");
- }
- }
- input[0][0].setText("0");
- input[1][1].setText("0");
- input[2][2].setText("0");
- input[3][3].setText("0");
- result = "";
- resultArea.setText(result);
- }
- }
- public static void main(String[] args) {
- Placeholder wp = new Placeholder();
- wp.setVisible(true);
- }
- /**
- * This method holds the process for the Warshall Algorithm.
- * @param inputs array - user input of matrix
- * @param size size of the matrix
- */
- public void WarshallSolution(int[][] inputs, int size) {
- //displaying the original input
- result += "R(" + 0 + "):\n";
- for (int j = 0; j < 4; j++) {
- for (int k = 0; k < 4; k++) {
- result += inputs[j][k] + "\t";
- }
- result+="\n";
- }
- result+="\n\n";
- //warshall algo process
- warshall(0,inputs,size);
- }
- public void warshall(int n, int[][] inputs,int size){
- if(n==size){
- return;
- }
- for (int r = 0; r < size; r++) {
- if (r == n) {
- continue;
- }
- for (int c = 0; c < size; c++) {
- if (c == n) {
- continue;
- }
- int prod = inputs[r][n] * inputs[n][c];
- if ((prod == 1 || inputs[r][c] == 1)) {
- inputs[r][c] = 1;
- }
- }
- }
- result += "R(" + (n + 1) + "):\n";
- for (int j = 0; j < 4; j++) {
- for (int k = 0; k < 4; k++) {
- result += inputs[j][k] + "\t";
- }
- result += "\n";
- }
- result += "\n\n";
- warshall(n+1,inputs,size);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement