Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.io.BufferedReader;
  4. import java.io.FileReader;
  5.  
  6. class Gui extends JFrame {
  7.  
  8. private int[][] inputMatrix = new int[100][100];
  9. private int[][] outputMatrix = new int[100][100];
  10. private int rows, columns;
  11. private JLabel outputMatrixLabel = new JLabel();
  12.  
  13. Gui() {
  14. super("Matrix");
  15.  
  16. setSize(500, 500);
  17. setDefaultCloseOperation(EXIT_ON_CLOSE);
  18.  
  19. JPanel content = new JPanel();
  20. content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
  21.  
  22. JLabel answerLabel = new JLabel();
  23.  
  24. answerLabel.setText("Result matrix: ");
  25. answerLabel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
  26.  
  27. outputMatrixLabel.setBorder(BorderFactory.createEmptyBorder(20, 10, 20, 20));
  28.  
  29. JButton openFileButton = new JButton("Open file");
  30. openFileButton.setSize(new Dimension(100, 100));
  31. openFileButton.setMargin(new Insets(5, 5, 5, 5));
  32. openFileButton.addActionListener(e -> {
  33. FileDialog openFile = new FileDialog(this, "Choose file", FileDialog.LOAD);
  34. openFile.setVisible(true);
  35. String file = openFile.getDirectory() + openFile.getFile();
  36.  
  37. if (file.contains("null")) return;
  38.  
  39. System.out.println("file : " + file);
  40.  
  41. try {
  42. BufferedReader reader = new BufferedReader(new FileReader(file));
  43. int i = 0, j;
  44. String line;
  45. do {
  46. line = reader.readLine();
  47. j = 0;
  48. String[] numbers = line.split(" ");
  49. for (String number : numbers) {
  50. System.out.print(number + " ");
  51. inputMatrix[i][j] = Integer.parseInt(number);
  52. j++;
  53. }
  54. System.out.println();
  55. i++;
  56. rows = i;
  57. columns = j;
  58.  
  59. } while (line != null);
  60. reader.close();
  61.  
  62. } catch (Exception e1) {
  63. e1.printStackTrace();
  64. }
  65.  
  66. System.out.println("Rows: " + rows + "\nColumns: " + columns);
  67.  
  68. int[][] deletedRows = new int[rows][columns];
  69. int[][] deletedColumns = new int[rows][columns];
  70. int leftRowsCount = 0;
  71. int leftColumnsCount = 0;
  72.  
  73. for (int i = 0; i < rows; i++) {
  74. int count = 0;
  75. for (int j = 0; j < columns; j++)
  76. if (inputMatrix[i][j] == 0)
  77. count++;
  78. if (count != columns) {
  79. for (int j = 0; j < columns; j++)
  80. deletedRows[leftRowsCount][j] = inputMatrix[i][j];
  81. leftRowsCount++;
  82. }
  83. }
  84.  
  85. System.out.println("Result rows: " + leftRowsCount + "\nResult columns: " + columns);
  86.  
  87. for (int i = 0; i < leftRowsCount; i++) {
  88. for (int j = 0; j < columns; j++)
  89. System.out.print(deletedRows[i][j] + " ");
  90. System.out.println();
  91. }
  92.  
  93. System.out.println();
  94.  
  95.  
  96. for (int j = 0; j < columns; j++) {
  97. int count = 0;
  98. for (int i = 0; i < leftRowsCount; i++)
  99. if (deletedRows[i][j] == 0)
  100. count++;
  101. if (count != leftRowsCount) {
  102. for (int i = 0; i < leftRowsCount; i++)
  103. deletedColumns[i][leftColumnsCount] = deletedRows[i][j];
  104. leftColumnsCount++;
  105. }
  106. }
  107.  
  108. System.out.println("Result rows: " + leftRowsCount + "\nResult columns: " + leftColumnsCount);
  109.  
  110. for (int i = 0; i < leftRowsCount; i++) {
  111. for (int j = 0; j < leftColumnsCount; j++) {
  112. System.out.print(deletedColumns[i][j] + " ");
  113. outputMatrix[i][j] = deletedColumns[i][j];
  114. }
  115. System.out.println();
  116. }
  117.  
  118. drawOutputMatrix(leftRowsCount, leftColumnsCount);
  119.  
  120. });
  121.  
  122. content.add(openFileButton);
  123. content.add(answerLabel);
  124. content.add(outputMatrixLabel);
  125.  
  126.  
  127. getContentPane().add(content);
  128. }
  129.  
  130. void drawOutputMatrix(int rows, int columns) {
  131. String matrixToString = "<html>";
  132. for (int i = 0; i < rows; i++) {
  133. for (int j = 0; j < columns; j++)
  134. matrixToString += outputMatrix[i][j] + " ";
  135. matrixToString += "<br/>";
  136. }
  137. matrixToString += "</html>";
  138.  
  139. outputMatrixLabel.setText(matrixToString);
  140. }
  141.  
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement