-
Notifications
You must be signed in to change notification settings - Fork 0
/
App
625 lines (510 loc) · 17 KB
/
App
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.*;
public class App {
static ActionListener returnToMenu = new ActionListener() {
public void actionPerformed(ActionEvent e) {
chooseOption();
}
};
static JFrame frame = new JFrame("Linear Algebra");
static JLabel title, rowLabel, colLabel, scalLabel, rowLabel2, colLabel2, scalLabel2;
static JTextField rowTextfield, colTextfield, scalTextfield, rowTextfield2, colTextfield2, scalTextfield2;
static ArrayList<JTextField> matrix1TextFields = new ArrayList<JTextField>();
static ArrayList<JTextField> matrix2TextFields = new ArrayList<JTextField>();
static int cols, rows, cols2, rows2;
static double[][] matrix1Arr;
static double[][] matrix2Arr;
static JButton menu = button("Return to Menu", 432, 5, 150, 35);
public static void main(String[] args) {
menu.addActionListener(returnToMenu);
chooseOption();
}
public static void chooseOption() {
clearFrame();
title = title("What would you like to compute?", 150); // title function creates a label on top of the
// window with the inputted string
// creates button with given dimensions (see method)
JButton addition = button("Matrix Addition", 30, 50, 150, 50);
JButton subtraction = button("Matrix Subtraction", 30, 130, 150, 50);
JButton multiplication = button("Matrix Multiplication", 220, 50, 150, 50);
JButton scalerMult = button("Scaler Multiplication", 220, 130, 150, 50);
JButton inverse = button("Inverse", 410, 50, 150, 50);
JButton determinant = button("Determinant", 410, 130, 150, 50);
ActionListener add = new ActionListener() {// creates an action that can be attached to a button that is pressed
// similar to an if statement (if button is pressed)
public void actionPerformed(ActionEvent e) {
basicComputations(0);
}
};
ActionListener sub = new ActionListener() {
public void actionPerformed(ActionEvent e) {
basicComputations(1);
}
};
ActionListener mult = new ActionListener() {
public void actionPerformed(ActionEvent e) {
basicComputations(2);
}
};
ActionListener scalMult = new ActionListener() {
public void actionPerformed(ActionEvent e) {
scalerMult();
}
};
ActionListener inv = new ActionListener() {
public void actionPerformed(ActionEvent e) {
inverse();
}
};
ActionListener det = new ActionListener() {
public void actionPerformed(ActionEvent e) {
determinant();
}
};
// attaches actions to buttons
addition.addActionListener(add);
subtraction.addActionListener(sub);
multiplication.addActionListener(mult);
scalerMult.addActionListener(scalMult);
inverse.addActionListener(inv);
determinant.addActionListener(det);
// adding elements to frame
frame.add(title);
frame.add(addition);
frame.add(subtraction);
frame.add(multiplication);
frame.add(scalerMult);
frame.add(inverse);
frame.add(determinant);
// specifying frame dimensions
frame.setSize(600, 250);
frame.getContentPane().setBackground(Color.white);
frame.setLayout(null);
frame.setVisible(true);
}
public static void basicComputations(int x) {// both addition and subtraction depending on value of adding
clearFrame();
if (x == 0) {
title = title("Matrix Addition", 225);
} else if (x == 1) {
title = title("Matrix Subtraction", 225);
} else {
title = title("Matrix Multiplication", 200);
}
displayRowAndColEntry(10, 50); // method creates input row and col text and the box with it
if (x == 2) {
displayRowAndColEntry2(210, 50);
}
JButton button = button("Submit", 10, 130, 150, 50);
// adding created buttons, text, and textfields to the window
frame.add(title);
frame.add(button);
frame.add(menu);
ActionListener a = new ActionListener() {
public void actionPerformed(ActionEvent e) {
// sets integer value rows and cols to whatever text is in the corresponding
// boxes
rows = Integer.parseInt(rowTextfield.getText());
cols = Integer.parseInt(colTextfield.getText());
if (x == 2) {
rows2 = Integer.parseInt(rowTextfield2.getText());
cols2 = Integer.parseInt(colTextfield2.getText());
} else {
rows2 = cols;
}
if (cols != rows2) {
clearFrame();
JLabel Error = header("Matrix 1's columns and matrix 2's rows must be the same.", 10, 185);
Error.setForeground(Color.red);
displayRowAndColEntry(10, 50);
displayRowAndColEntry2(210, 50);
frame.add(button);
frame.add(title);
frame.add(menu);
frame.add(Error);
} else {
if (rows > 7) {
frame.setSize(600, 260 + 20 * (rows - 7));
}
clearFrame(); // clears the window
/// adds input matrix text and makes input boxes corresponding to the number of
/// cols and rows
displayInputMatrix(10, 50, 1);
if (x == 2) {
displayInputMatrix2(300, 50, 2);
} else {
displayInputMatrix(300, 50, 2);
}
JButton button = button("Compute", 5, 5, 150, 35);
frame.add(button);
frame.add(title);
frame.add(menu);
ActionListener a = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(x==2) {
createMatrixArrays2();
}
else {
createMatrixArrays(true);
}
// creates a 2d int array and fills entries based on what was
// inputted
// in
// the boxes
// creates two matrix objects using the new arrays
Matrix matrix1 = new Matrix(matrix1Arr);
Matrix matrix2 = new Matrix(matrix2Arr);
matrix1.printMatrix();
matrix2.printMatrix();
Matrix result;
if (x == 0) {
result = matrix1.add(matrix2);
} else if (x == 1) {
result = matrix1.sub(matrix2);
} else {
result = matrix1.mult(matrix2);
}
clearFrame();
frame.add(title);
frame.add(menu);
displayMatrix(result, 10, 40, "Result", true); // method to display matrix on window with
// given
// x and y
}
};
button.addActionListener(a); // attaches second action event to the calculate button
}
}
};
button.addActionListener(a); // attaches first action event to the submit button
}
public static void scalerMult() {
clearFrame();
title = title("Scaler Multiplication", 200);
displayRowAndColEntry(10, 50);
JButton button = button("Submit", 10, 130, 150, 50);
frame.add(button);
frame.add(title);
frame.add(menu);
ActionListener a = new ActionListener() {
public void actionPerformed(ActionEvent e) {
rows = Integer.parseInt(rowTextfield.getText());
cols = Integer.parseInt(colTextfield.getText());
if (rows > 7) {
frame.setSize(600, 260 + 20 * (rows - 7));
}
clearFrame();
displayInputMatrix(10, 50, 1);
displayScalerInput(432, 50);
JButton button = button("Compute", 5, 5, 150, 35);
frame.add(button);
frame.add(title);
frame.add(menu);
ActionListener a = new ActionListener() {
public void actionPerformed(ActionEvent e) {
clearFrame();
frame.add(title);
frame.add(menu);
createMatrixArrays(false);
Matrix result = (new Matrix(matrix1Arr)).scalarMult(Integer.parseInt(scalTextfield.getText()));
displayMatrix(result, 10, 40, "Result", true);
}
};
button.addActionListener(a);
}
};
button.addActionListener(a);
}
public static void inverse() {
clearFrame();
title = title("Compute Inverse", 220);
displayRowAndColEntry(10, 50);
JButton button = button("Submit", 10, 130, 150, 50);
ActionListener a = new ActionListener() {
public void actionPerformed(ActionEvent e) {
rows = Integer.parseInt(rowTextfield.getText());
cols = Integer.parseInt(colTextfield.getText());
if (rows != cols) {
clearFrame();
JLabel Error = header("Only square matrices can have an inverse.", 10, 185);
Error.setForeground(Color.red);
displayRowAndColEntry(10, 50);
frame.add(button);
frame.add(title);
frame.add(menu);
frame.add(Error);
} else {
if (rows > 7) {
frame.setSize(600, 260 + 20 * (rows - 7));
}
clearFrame();
displayInputMatrix(10, 50, 1);
JButton button = button("Compute", 5, 5, 150, 35);
frame.add(button);
frame.add(title);
frame.add(menu);
ActionListener a = new ActionListener() {
public void actionPerformed(ActionEvent e) {
clearFrame();
frame.add(title);
frame.add(menu);
createMatrixArrays(false);
Matrix result = ((new Matrix(matrix1Arr)).inverse());
if(result.equals(null)) {
JLabel error = header("This matrix has no inverse.", 10, 50);
frame.add(error);
}
else {
displayMatrix(result, 10, 40, "Result", true);
}
}
};
button.addActionListener(a);
}
}
};
button.addActionListener(a);
frame.add(button);
frame.add(menu);
frame.add(title);
}
public static void determinant() {
clearFrame();
title = title("Compute Determinant", 200);
displayRowAndColEntry(10, 50);
JButton button = button("Submit", 10, 130, 150, 50);
ActionListener a = new ActionListener() {
public void actionPerformed(ActionEvent e) {
rows = Integer.parseInt(rowTextfield.getText());
cols = Integer.parseInt(colTextfield.getText());
if (rows != cols) {
clearFrame();
JLabel Error = header("Only square matrices can have a determinant.", 10, 185);
Error.setForeground(Color.red);
displayRowAndColEntry(10, 50);
frame.add(button);
frame.add(title);
frame.add(menu);
frame.add(Error);
} else {
if (rows > 7) {
frame.setSize(600, 260 + 20 * (rows - 7));
}
clearFrame();
displayInputMatrix(10, 50, 1);
JButton button = button("Compute", 5, 5, 150, 35);
frame.add(button);
frame.add(title);
frame.add(menu);
ActionListener a = new ActionListener() {
public void actionPerformed(ActionEvent e) {
clearFrame();
frame.add(title);
frame.add(menu);
createMatrixArrays(false);
double result = ((new Matrix(matrix1Arr)).determinant());
JLabel res = header("" + result, 10, 40);
frame.add(res);
}
};
button.addActionListener(a);
}
}
};
button.addActionListener(a);
frame.add(button);
frame.add(menu);
frame.add(title);
}
public static JLabel title(String s, int x) {// creates a title in the center of the screen
JLabel title = new JLabel(s);
title.setFont(new Font("Georgia", 100, 20));
title.setBounds(x, 10, 2000, 30);
return title;
}
public static JLabel header(String s, int x, int y) {// creates text at given x and y coordinate
JLabel header = new JLabel(s);
header.setFont(new Font("Candara", 0, 15));
header.setBounds(x, y, 1000, 20);
return header;
}
// creates input row and col text and text boxes at given x and y
public static void displayRowAndColEntry(int x, int y) {
rowLabel = new JLabel("Input rows:");
rowLabel.setFont(new Font("Candara", 0, 15));
rowLabel.setBounds(x, y + 4, 200, 20);
rowTextfield = new JTextField("", 20);
rowTextfield.setFont(new Font("Times New Roman", 0, 15));
rowTextfield.setBounds(x + 117, y + 2, 40, 25);
colLabel = new JLabel("Input columns:");
colLabel.setFont(new Font("Candara", 0, 15));
colLabel.setBounds(x, y + 45, 200, 20);
colTextfield = new JTextField("", 20);
colTextfield.setFont(new Font("Times New Roman", 0, 15));
colTextfield.setBounds(x + 117, y + 42, 40, 25);
frame.add(colLabel);
frame.add(colTextfield);
frame.add(rowLabel);
frame.add(rowTextfield);
}
public static void displayRowAndColEntry2(int x, int y) {
rowLabel2 = new JLabel("Input rows:");
rowLabel2.setFont(new Font("Candara", 0, 15));
rowLabel2.setBounds(x, y + 4, 200, 20);
rowTextfield2 = new JTextField("", 20);
rowTextfield2.setFont(new Font("Times New Roman", 0, 15));
rowTextfield2.setBounds(x + 117, y + 2, 40, 25);
colLabel2 = new JLabel("Input columns:");
colLabel2.setFont(new Font("Candara", 0, 15));
colLabel2.setBounds(x, y + 45, 200, 20);
colTextfield2 = new JTextField("", 20);
colTextfield2.setFont(new Font("Times New Roman", 0, 15));
colTextfield2.setBounds(x + 117, y + 42, 40, 25);
frame.add(colLabel2);
frame.add(colTextfield2);
frame.add(rowLabel2);
frame.add(rowTextfield2);
}
public static void displayScalerInput(int x, int y) {
scalLabel = new JLabel("Input scaler:");
scalLabel.setFont(new Font("Candara", 0, 15));
scalLabel.setBounds(x, y + 5, 200, 20);
scalTextfield = new JTextField("", 20);
scalTextfield.setFont(new Font("Times New Roman", 0, 15));
scalTextfield.setBounds(x + 110, y + 2, 40, 25);
frame.add(scalLabel);
frame.add(scalTextfield);
}
public static void displayInputMatrix(int x, int y, int n) {// Creates a series of text boxes in the shape matrix
// with inputted rows and columns
int startx = x;
JLabel matrixTitle;
if (n == 0) {
matrixTitle = new JLabel("Input Matrix ");
} else {
matrixTitle = new JLabel("Input Matrix " + n);
}
matrixTitle.setFont(new Font("Candara", 0, 15));
matrixTitle.setBounds(x, y, 100, 20);
y += 22;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
JTextField tempTextField = new JTextField("", 20);
tempTextField.setFont(new Font("Times New Roman", 0, 15));
tempTextField.setBounds(x, y, 30, 20);
if (n == 1) {// value of n decides if this goes in to matrix1's field or matrix 2's
matrix1TextFields.add(tempTextField);
} else {
matrix2TextFields.add(tempTextField);
}
frame.add(tempTextField);
x += 30; // moves next box to the right
}
x = startx; // resets x value
y += 20; // moves next row of text boxes lower
}
frame.add(matrixTitle);
}
public static void displayInputMatrix2(int x, int y, int n) {// Creates a series of text boxes in the shape matrix
// with inputted rows and columns
int startx = x;
JLabel matrixTitle;
if (n == 0) {
matrixTitle = new JLabel("Input Matrix ");
} else {
matrixTitle = new JLabel("Input Matrix " + n);
}
matrixTitle.setFont(new Font("Candara", 0, 15));
matrixTitle.setBounds(x, y, 100, 20);
y += 22;
for (int i = 0; i < rows2; i++) {
for (int j = 0; j < cols2; j++) {
JTextField tempTextField = new JTextField("", 20);
tempTextField.setFont(new Font("Times New Roman", 0, 15));
tempTextField.setBounds(x, y, 30, 20);
if (n == 1) {// value of n decides if this goes in to matrix1's field or matrix 2's
matrix1TextFields.add(tempTextField);
} else {
matrix2TextFields.add(tempTextField);
}
frame.add(tempTextField);
x += 30; // moves next box to the right
}
x = startx; // resets x value
y += 20; // moves next row of text boxes lower
}
frame.add(matrixTitle);
}
public static JButton button(String s, int x, int y, int length, int width) {// creates button with given dimension
JButton button = new JButton(s);
button.setBounds(x, y, length, width);
button.setFont(new Font("Candara", 0, 13));
button.setBackground(Color.pink);
return button;
}
public static void createMatrixArrays(boolean twoMatrices) {// Goes through array of textbox entrys and moves them
// into 2d arrays
int x = 0;
matrix1Arr = new double[rows][cols];
matrix2Arr = new double[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix1TextFields.get(x).getText();
matrix1Arr[i][j] = Double.parseDouble(matrix1TextFields.get(x).getText());
if (twoMatrices) {
matrix2Arr[i][j] = Double.parseDouble(matrix2TextFields.get(x).getText());
}
x++;
}
}
}
public static void createMatrixArrays2() {// Goes through array of textbox entrys and moves them
// into 2d arrays
int x = 0;
matrix1Arr = new double[rows][cols];
matrix2Arr = new double[rows2][cols2];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix1Arr[i][j] = Double.parseDouble(matrix1TextFields.get(x).getText());
x++;
}
}
x=0;
for (int i = 0; i < rows2; i++) {
for (int j = 0; j < cols2; j++) {
matrix2Arr[i][j] = Double.parseDouble(matrix2TextFields.get(x).getText());
x++;
}
}
}
public static void clearFrame() {
frame.getContentPane().removeAll();
frame.getContentPane().repaint();
}
// displays inputted matrix with given dimensions with option to give it a
// header
public static void displayMatrix(Matrix m, int x, int y, String s, boolean string) {
if (string) {
JLabel resultTitle = header(s, 10, 20);
frame.add(resultTitle);
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
JLabel temp = new JLabel("" + (Math.round(m.getElement(i, j) * 100.0) / 100.0));
temp.setFont(new Font("Times New Roman", 0, 15));
temp.setBounds(x, y, 100, 20);
frame.add(temp);
x += 50;
}
x = 10;
y += 40;
}
if (y > frame.getSize().height) {
frame.setSize(600, y + 20);
}
}
}