Ultra Geile Studenten Benutzer Oberfläche (UGSBO)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

210 lines
7.2 KiB

  1. package com.ugsbo.matrixcalc;
  2. import javafx.fxml.FXML;
  3. import javafx.scene.control.*;
  4. import javafx.scene.text.Text;
  5. import javafx.scene.text.TextAlignment;
  6. public class MatrixCalcController {
  7. // Hier werden die fx:id Attribute verknuepft.
  8. @FXML
  9. private Button multiplyButton, addButton, DetAButton, DetBButton, substractButton, transposeButton;
  10. @FXML
  11. private Text errorText, outputText;
  12. @FXML
  13. private TextArea matrixATextArea, matrixBTextArea;
  14. /**
  15. * Konstructor is called before initialize()
  16. */
  17. public MatrixCalcController() {
  18. // Setup of some Fields could be defined here.
  19. }
  20. /**
  21. * Initializes the controller class. This method is automatically called after
  22. * the fxml file has been loaded.
  23. */
  24. @FXML
  25. public void initialize() {
  26. /**
  27. * Convert Strings to matricies, multiply them and output the result.
  28. */
  29. multiplyButton.setOnMouseClicked((event) -> {
  30. MatrixCalcMath math = new MatrixCalcMath();
  31. MatrixCalcIOUtils util = new MatrixCalcIOUtils();
  32. String stringMatrixA = matrixATextArea.getText();
  33. String stringMatrixB = matrixBTextArea.getText();
  34. if (util.checkInput(stringMatrixA) && util.checkInput(stringMatrixB)) {
  35. double[][] matrixA = util.stringToMatrix(stringMatrixA);
  36. double[][] matrixB = util.stringToMatrix(stringMatrixB);
  37. double[][] result = math.matrixMultiplication(matrixA, matrixB);
  38. String DisplayableString = util.outputMatrixToOutputText(result);
  39. outputText.setText(DisplayableString);
  40. outputText.setTextAlignment(TextAlignment.CENTER);
  41. }
  42. // System.out.println(matrixATextArea.getText());
  43. });
  44. transposeButton.setOnMouseClicked((event) -> {
  45. MatrixCalcMath math = new MatrixCalcMath();
  46. MatrixCalcIOUtils util = new MatrixCalcIOUtils();
  47. String stringMatrixA = matrixATextArea.getText();
  48. if (util.checkInput(stringMatrixA)) {
  49. double[][] matrixA = util.stringToMatrix(stringMatrixA);
  50. double[][] result = math.matrixTransponation(matrixA);
  51. String DisplayableString = util.outputMatrixToOutputText(result);
  52. outputText.setText(DisplayableString);
  53. outputText.setTextAlignment(TextAlignment.CENTER);
  54. }
  55. });
  56. addButton.setOnMouseClicked((event) -> {
  57. MatrixCalcMath math = new MatrixCalcMath();
  58. MatrixCalcIOUtils util = new MatrixCalcIOUtils();
  59. String stringMatrixA = matrixATextArea.getText();
  60. String stringMatrixB = matrixBTextArea.getText();
  61. if (util.checkInput(stringMatrixA) && util.checkInput(stringMatrixB)) {
  62. double[][] matrixA = util.stringToMatrix(stringMatrixA);
  63. double[][] matrixB = util.stringToMatrix(stringMatrixB);
  64. double[][] result = math.matrixAddition(matrixA, matrixB);
  65. String DisplayableString = util.outputMatrixToOutputText(result);
  66. outputText.setText(DisplayableString);
  67. outputText.setTextAlignment(TextAlignment.CENTER);
  68. }
  69. });
  70. substractButton.setOnMouseClicked((event) -> {
  71. MatrixCalcMath math = new MatrixCalcMath();
  72. MatrixCalcIOUtils util = new MatrixCalcIOUtils();
  73. String stringMatrixA = matrixATextArea.getText();
  74. String stringMatrixB = matrixBTextArea.getText();
  75. boolean stop = false;
  76. try {
  77. util.checkInput(stringMatrixA);
  78. } catch (Exception e) {
  79. stop = true;
  80. outputText.setText(e.getMessage() + "A");
  81. outputText.setTextAlignment(TextAlignment.CENTER);
  82. }
  83. try {
  84. util.checkInput(stringMatrixB);
  85. } catch (Exception e) {
  86. stop = true;
  87. outputText.setText(e.getMessage() + "B");
  88. outputText.setTextAlignment(TextAlignment.CENTER);
  89. }
  90. if (util.checkInput(stringMatrixA) && util.checkInput(stringMatrixB) && !stop) {
  91. double[][] matrixA = util.stringToMatrix(stringMatrixA);
  92. double[][] matrixB = util.stringToMatrix(stringMatrixB);
  93. try {
  94. double[][] result = math.matrixSubstraction(matrixA, matrixB);
  95. String DisplayableString = util.outputMatrixToOutputText(result);
  96. outputText.setText(DisplayableString);
  97. outputText.setTextAlignment(TextAlignment.CENTER);
  98. } catch (Exception e) {
  99. outputText.setText(e.getMessage());
  100. outputText.setTextAlignment(TextAlignment.CENTER);
  101. }
  102. }
  103. });
  104. DetAButton.setOnMouseClicked((event) -> {
  105. MatrixCalcMath math = new MatrixCalcMath();
  106. MatrixCalcIOUtils util = new MatrixCalcIOUtils();
  107. String stringMatrixA = matrixATextArea.getText();
  108. boolean stop = false;
  109. try {
  110. util.checkInput(stringMatrixA);
  111. } catch (IllegalArgumentException e) {
  112. stop = true;
  113. outputText.setText(e.getMessage());
  114. outputText.setTextAlignment(TextAlignment.CENTER);
  115. }
  116. if (util.checkInput(stringMatrixA) && !stop) {
  117. double[][] matrixA = util.stringToMatrix(stringMatrixA);
  118. try {
  119. double result = math.calcDeterminat(matrixA);
  120. String DisplayableString = Double.toString(result);
  121. outputText.setText(DisplayableString);
  122. outputText.setTextAlignment(TextAlignment.CENTER);
  123. } catch (IllegalArgumentException e) {
  124. outputText.setText(e.getMessage());
  125. outputText.setTextAlignment(TextAlignment.CENTER);
  126. }
  127. }
  128. });
  129. DetBButton.setOnMouseClicked((event) -> {
  130. MatrixCalcMath math = new MatrixCalcMath();
  131. MatrixCalcIOUtils util = new MatrixCalcIOUtils();
  132. String stringMatrixB = matrixBTextArea.getText();
  133. boolean stop = false;
  134. try {
  135. util.checkInput(stringMatrixB);
  136. } catch (IllegalArgumentException e) {
  137. stop = true;
  138. outputText.setText(e.getMessage());
  139. outputText.setTextAlignment(TextAlignment.CENTER);
  140. }
  141. if (util.checkInput(stringMatrixB) && !stop) {
  142. double[][] matrixB = util.stringToMatrix(stringMatrixB);
  143. try {
  144. double result = math.calcDeterminat(matrixB);
  145. String DisplayableString = Double.toString(result);
  146. outputText.setText(DisplayableString);
  147. outputText.setTextAlignment(TextAlignment.CENTER);
  148. } catch (IllegalArgumentException e) {
  149. outputText.setText(e.getMessage());
  150. outputText.setTextAlignment(TextAlignment.CENTER);
  151. }
  152. }
  153. });
  154. }
  155. }