Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.io.BufferedReader;
- import java.io.FileReader;
- class Gui extends JFrame {
- private int[][] inputMatrix = new int[100][100];
- private int[][] outputMatrix = new int[100][100];
- private int rows, columns;
- private JLabel outputMatrixLabel = new JLabel();
- Gui() {
- super("Matrix");
- setSize(500, 500);
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- JPanel content = new JPanel();
- content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
- JLabel answerLabel = new JLabel();
- answerLabel.setText("Result matrix: ");
- answerLabel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
- outputMatrixLabel.setBorder(BorderFactory.createEmptyBorder(20, 10, 20, 20));
- JButton openFileButton = new JButton("Open file");
- openFileButton.setSize(new Dimension(100, 100));
- openFileButton.setMargin(new Insets(5, 5, 5, 5));
- openFileButton.addActionListener(e -> {
- FileDialog openFile = new FileDialog(this, "Choose file", FileDialog.LOAD);
- openFile.setVisible(true);
- String file = openFile.getDirectory() + openFile.getFile();
- if (file.contains("null")) return;
- System.out.println("file : " + file);
- try {
- BufferedReader reader = new BufferedReader(new FileReader(file));
- int i = 0, j;
- String line;
- do {
- line = reader.readLine();
- j = 0;
- String[] numbers = line.split(" ");
- for (String number : numbers) {
- System.out.print(number + " ");
- inputMatrix[i][j] = Integer.parseInt(number);
- j++;
- }
- System.out.println();
- i++;
- rows = i;
- columns = j;
- } while (line != null);
- reader.close();
- } catch (Exception e1) {
- e1.printStackTrace();
- }
- System.out.println("Rows: " + rows + "\nColumns: " + columns);
- int[][] deletedRows = new int[rows][columns];
- int[][] deletedColumns = new int[rows][columns];
- int leftRowsCount = 0;
- int leftColumnsCount = 0;
- for (int i = 0; i < rows; i++) {
- int count = 0;
- for (int j = 0; j < columns; j++)
- if (inputMatrix[i][j] == 0)
- count++;
- if (count != columns) {
- for (int j = 0; j < columns; j++)
- deletedRows[leftRowsCount][j] = inputMatrix[i][j];
- leftRowsCount++;
- }
- }
- System.out.println("Result rows: " + leftRowsCount + "\nResult columns: " + columns);
- for (int i = 0; i < leftRowsCount; i++) {
- for (int j = 0; j < columns; j++)
- System.out.print(deletedRows[i][j] + " ");
- System.out.println();
- }
- System.out.println();
- for (int j = 0; j < columns; j++) {
- int count = 0;
- for (int i = 0; i < leftRowsCount; i++)
- if (deletedRows[i][j] == 0)
- count++;
- if (count != leftRowsCount) {
- for (int i = 0; i < leftRowsCount; i++)
- deletedColumns[i][leftColumnsCount] = deletedRows[i][j];
- leftColumnsCount++;
- }
- }
- System.out.println("Result rows: " + leftRowsCount + "\nResult columns: " + leftColumnsCount);
- for (int i = 0; i < leftRowsCount; i++) {
- for (int j = 0; j < leftColumnsCount; j++) {
- System.out.print(deletedColumns[i][j] + " ");
- outputMatrix[i][j] = deletedColumns[i][j];
- }
- System.out.println();
- }
- drawOutputMatrix(leftRowsCount, leftColumnsCount);
- });
- content.add(openFileButton);
- content.add(answerLabel);
- content.add(outputMatrixLabel);
- getContentPane().add(content);
- }
- void drawOutputMatrix(int rows, int columns) {
- String matrixToString = "<html>";
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < columns; j++)
- matrixToString += outputMatrix[i][j] + " ";
- matrixToString += "<br/>";
- }
- matrixToString += "</html>";
- outputMatrixLabel.setText(matrixToString);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement