-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUIMain.java
More file actions
608 lines (539 loc) · 28.5 KB
/
UIMain.java
File metadata and controls
608 lines (539 loc) · 28.5 KB
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
/* Jason Ivey 2017
*
* Test Cases:
*
* INPUT: Point one (0, 0, 0) Point two (1, 1, 1)
* Expected: 1.7320508075688772
* OUTPUT: 1.7320508075688772
*
* INPUT: Point one (RANDOM DIMMENSION 8) Point two (RANDOM DIMMENSION 8)
* Expected: Point one (#, #, #, #, #, #, #, #) Point Tne(#, #, #, #, #, #, #, #)
* OUTPUT: Point one(203.0 63.0 303.0 241.0 237.0 121.0 675.0 669.0) Point two(327.0 961.0 231.0 732.0 108.0 347.0 223.0 116.0)
*
* INPUT: press [HELP]
* Expected: Help Dialog
* OUTPUT: Help Dialog
*
* INPUT: press [STATS] after making points
* Expected: Stats Dialog
* OUTPUT: Stats Dialog
*
* INPUT: press [HELP]
* Expected: Help Dialog
* OUTPUT: Help Dialog
*
* INPUT: press [DISTANCE] before inputing points
* Expected: Error Dialog
* OUTPUT: You cannot yet compare these points.
*
* Java III Lab01
*/
package Lab001;
import java.util.Random;
/**
*
* @author Jason Ivey
*/
public class UIMain extends javax.swing.JFrame {
//This boolean will determine if the text box 1 has been clicked already
private boolean firstClick1 = true;
//This boolean will determine if the text box 2 has been clicked already
private boolean firstClick2 = true;
//This boolean will determine if the text box 3 has been clicked already
private boolean firstClick3 = true;
//This boolean will determine if the text box 4 has been clicked already
private boolean firstClick4 = true;
//This boolean will determine if the help should be ereased or printed!
private boolean helpBinarySwitch = true;
//This boolean will determine if the stats should be ereased or printed!
private boolean statsBinarySwitch = true;
//String for input ereased by help request
private String previousText;
//General Debug Boolean
private final boolean DEBUG = true;
//Storgae object for storing things
Storage store = new Storage();
/**
* Creates an instance of this form Important for mkaing the first welcom
* text work
*/
public UIMain() {
initComponents();
String asciiPanelText = "Enter in some coordinates to get started!";//Welcome text
textPanel.setText(asciiPanelText);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*
* This code is automatically made by GUI designer, I cannot edit so no
* comments inside it
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jPopupMenu1 = new javax.swing.JPopupMenu();
jScrollPane1 = new javax.swing.JScrollPane();
textPanel = new javax.swing.JTextPane();
input1 = new javax.swing.JTextField();
label1 = new javax.swing.JLabel();
labelForRandom1 = new javax.swing.JLabel();
randomButton1 = new javax.swing.JButton();
input2 = new javax.swing.JTextField();
label2 = new javax.swing.JLabel();
labelForRandom2 = new javax.swing.JLabel();
randomButton2 = new javax.swing.JButton();
helpButton = new javax.swing.JButton();
statsButton = new javax.swing.JButton();
distanceButton = new javax.swing.JButton();
dimmensionLabel2 = new javax.swing.JLabel();
random2Dim = new javax.swing.JTextField();
random1Dim = new javax.swing.JTextField();
DimmensionLabel = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
setSize(new java.awt.Dimension(0, 0));
textPanel.setEditable(false);
jScrollPane1.setViewportView(textPanel);
input1.setText("Seperate with blank spaces, ' '");
input1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
input1MouseClicked(evt);
}
});
label1.setText("Point One = ");
labelForRandom1.setText("or click here");
randomButton1.setText("Random");
randomButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
randomButton1ActionPerformed(evt);
}
});
input2.setText("Seperate with blank spaces, ' '");
input2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
input2MouseClicked(evt);
}
});
label2.setText("Point Two = ");
labelForRandom2.setText("or click here");
randomButton2.setText("Random");
randomButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
randomButton2ActionPerformed(evt);
}
});
helpButton.setText("Help");
helpButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
helpButtonActionPerformed(evt);
}
});
statsButton.setText("Stats");
statsButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
statsButtonActionPerformed(evt);
}
});
distanceButton.setText("Distance!");
distanceButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
distanceButtonActionPerformed(evt);
}
});
dimmensionLabel2.setText("dimmension");
random2Dim.setText("# of dimmensions");
random2Dim.setToolTipText("default 2");
random2Dim.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
random2DimMouseClicked(evt);
}
});
random1Dim.setText("# of dimmensions");
random1Dim.setToolTipText("default 2");
random1Dim.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
random1DimMouseClicked(evt);
}
});
DimmensionLabel.setText("dimmension");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addGap(56, 56, 56)
.addComponent(labelForRandom1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(randomButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(DimmensionLabel)
.addGap(5, 5, 5)
.addComponent(random1Dim, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(label1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(input1, javax.swing.GroupLayout.PREFERRED_SIZE, 255, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(helpButton))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(label2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(input2, javax.swing.GroupLayout.PREFERRED_SIZE, 255, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(44, 44, 44)
.addComponent(labelForRandom2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(randomButton2)
.addGap(7, 7, 7)
.addComponent(dimmensionLabel2)
.addGap(5, 5, 5)
.addComponent(random2Dim, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(statsButton)
.addComponent(distanceButton, javax.swing.GroupLayout.PREFERRED_SIZE, 328, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 443, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(input1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(label1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(randomButton1)
.addComponent(labelForRandom1)
.addComponent(DimmensionLabel)
.addComponent(random1Dim, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(64, 64, 64)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(input2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(label2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(labelForRandom2)
.addComponent(randomButton2)
.addComponent(dimmensionLabel2)
.addComponent(random2Dim, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(distanceButton, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(helpButton)
.addComponent(statsButton)))
.addComponent(jScrollPane1))
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
// Will send the hulp dialog to the text for first click and remove it on second.
private void helpButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_helpButtonActionPerformed
// This will display help in the text box!
if (DEBUG) { // Debug statement for developement
System.out.println("\n-Help Called and helpBinarySwitch = " + helpBinarySwitch);
}
//Checks if it should display the help text or replace old text
if (helpBinarySwitch == true) {
previousText = textPanel.getText(); //Setes previous text so that it can retrieve it later
textPanel.setText("Don't Panic! Here's that help you wanted: \n\n"
+ "**************** COSC 2336 lab 1 by Jason Ivey *********************************\n"
+ "* This program allows you specify points in N-dimensional space:\n"
+ " - You may request a random number for any coordinate by pressing \"RANDOM\" and choosing a dimmension.\n"
+ " - When you are finished entering the cordinate just press the <Distance> button\n"
+ "* Pairs of point are used to create a lines\n"
+ " - If the 2 points have mismatched dimensions an error will be shown\n"
+ " - When you press the distance button a line is created and the distance is displayed\n"
+ "* When you are done just hit the black X in the top right corner to exit.\n"
+ "* Help and stats can be toggled by double clicking.\n"
+ "* Random number will be scaled between -1,000.00 and +1,000.00");
helpBinarySwitch = false;//Changes the siwtch so next call will get rid of help
} else { // Will set the text box back to previous text
textPanel.setText(previousText);
helpBinarySwitch = true;
previousText = "";
}
}//GEN-LAST:event_helpButtonActionPerformed
//A method that determines if a mouse click has occured then adjusts input acordingly
private void input2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_input2MouseClicked
// This will clear the text from the input box so our user can put their own in
if (DEBUG) { //Debug statement
System.out.println("\n-input2MouseClicked");
}
if (firstClick1 == true) { //Clears text so user can input
input2.setText("");
firstClick1 = false; //Shows that next click is not first click, this prevents multiple clearings on click
}
}//GEN-LAST:event_input2MouseClicked
//This will clean up text input when a user clicks
private void input1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_input1MouseClicked
// This will clear the text from the input box so our user can put their own in
if (DEBUG) { //Debug statement
System.out.println("\n-input1MouseClicked");
}
if (firstClick2 == true) { //Clears text so user can input
input1.setText("");
firstClick2 = false; //Stops multiple erasings of text
}
}//GEN-LAST:event_input1MouseClicked
//This checks for a press on the button if so this will set the first text box to a random
private void randomButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_randomButton1ActionPerformed
// Will set the text of input1 to a random double
if (DEBUG) { //Debug statement
System.out.println("\n- randomButton1ActionPerformed");
}
input1.setText(random(random1DimGetNumeral())); //Asings the random to the input box based on dimmension
}//GEN-LAST:event_randomButton1ActionPerformed
//Does the same action as previos method for button1
private void randomButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_randomButton2ActionPerformed
// Will set the text of input2 to a random double
if (DEBUG) { //Debug statement
System.out.println("\n- randomButton2ActionPerformed");
}
input2.setText(random(random2DimGetNumeral())); //Asings the random to the input box based on dimmension
}//GEN-LAST:event_randomButton2ActionPerformed
//returns an int for the random 1 dim field
private int random1DimGetNumeral() {
if (DEBUG) { //Debug statement for developing
System.out.println("\n- random1DimGetNumeral: ");
}
if (firstClick3 == true || random1Dim.getText().equals("")) { //Checks if this is first click or if it is empty, so it can return a filler
if (DEBUG) {
System.out.println(" getNumeral1Failed return 2 ");//Another debug statement
}
return 2;
} else { //help from stack exhcange see: http://stackoverflow.com/questions/41955844/java-swing-is-not-retrying-methods-after-exceptions/41955923#41955923
try {
return Integer.parseInt(random1Dim.getText().trim());
} catch (Exception inputError) {
textPanel.setText("Input Error for Random Dimension field1.\n"
+ "Default dimmension of 2 set instead.\n");
return 2;
}
}
}
//returns an int for the random 2 dim field
private int random2DimGetNumeral() {
if (DEBUG) {
System.out.println("\n- random2DimGetNumeral: ");
}
if (firstClick4 == true || random2Dim.getText().equals("")) {
return 2;
} else { //help from stack exhcange see: http://stackoverflow.com/questions/41955844/java-swing-is-not-retrying-methods-after-exceptions/41955923#41955923
try {
return Integer.parseInt(random2Dim.getText().trim());
} catch (Exception inputError) {
textPanel.setText("Input Error for Random Dimension field 2.\n"
+ "Default dimmension of 2 set instead");
return 2;
}
}
}
//Sets the text to blank when clicked
private void random1DimMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_random1DimMouseClicked
if (DEBUG) {
System.out.println("\n- random1DimMouseClicked: "); //Debug statement
}
if (firstClick3 == true) { //sets first click to true so input will nto be ereased
random1Dim.setText("");
firstClick3 = false;
}
}//GEN-LAST:event_random1DimMouseClicked
//sets the input code to blank when clicked
private void random2DimMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_random2DimMouseClicked
if (DEBUG) {
System.out.println("\n- random2DimMouseClicked: "); //debug statement
}
if (firstClick4 == true) {
random2Dim.setText(""); //Clears up input so user cna input to box
firstClick4 = false;
}
}//GEN-LAST:event_random2DimMouseClicked
// Main distanc calculator and storage function calling method
private void distanceButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_distanceButtonActionPerformed
if (DEBUG) {
System.out.println("\n- distanceButtonActionPerformed: "); //Debug statement
}
try { //Will try, if it fails then an error wil be printed to screen
Point one = pointMaker(true); //makes point one, true == input one
Point two = pointMaker(false); //makes piont two, false = input2
Line theLine = new Line(one, two);
//Stores points one and two
store.store(one);
store.store(two);
store.store(theLine); //Stores the line
if (DEBUG) {
System.out.println("\n- Line made and stored: "); //Debug statement
}
textPanel.setText("The distance from \n\nPoint One: " + one
+ "\n\nto Point Two: " + two
+ "\n\n= " + Double.toString(theLine.getLineLength())); //prints out distance
} catch (DimmensionalError e) { //Prints out dimmensional error
if (DEBUG) {
System.out.println("\n- Dimmensional Error ");
}
textPanel.setText("Dimmensional Error, you have tried to compare 2 points with different dimmensions.\n"
+ "Despite that it is mathmatically impossible to do that, it's just silly, try again.\n");
return;
} catch (Exception e) {
textPanel.setText("You cannot yet compare these points."); //The input is not yet valid
}
}//GEN-LAST:event_distanceButtonActionPerformed
//This prints out stats when button is pushed and ereases them when pushed again
private void statsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_statsButtonActionPerformed
try {
if (store.linesExist()) { //If so will show statse else will turn onff stats
if (statsBinarySwitch) {
statsBinarySwitch = false;
previousText = textPanel.getText();
textPanel.setText(store.stats());//Shows stats
} else {
statsBinarySwitch = true; //Delets stats and shows text
textPanel.setText(previousText);
previousText = null;
}
}
} catch (Exception e) {
textPanel.setText("Invalid input, distance cannot yet be compared."); //Another case of invalid input
}
}//GEN-LAST:event_statsButtonActionPerformed
//makes a point from either input 1 or 2, true is one false is 2
private Point pointMaker(boolean selection) throws Exception {
if (DEBUG) {
System.out.println("\n- pointMaker called "); //Debug statement
}
double[] coordinates; //The coordinates to make point with
if (DEBUG) {
System.out.println("\n- pointMaker int sel = " + selection); //Another one
}
if (selection) { //point one maker
try {
coordinates = inputParseToDoubleArray(input1.getText()); //parses input into coordinates
Point point = new Point(coordinates); //Makes a point
return point;
} catch (InvalidInputException error) {
textPanel.setText("INPUT INVALID FOR INPUT BOX 1"); //Error text
}
} else {
try { //makes point 2
coordinates = inputParseToDoubleArray(input2.getText());
Point point = new Point(coordinates);
return point;
} catch (InvalidInputException error) {
textPanel.setText("INPUT INVALID FOR INPUT BOX 2"); //Error text
}
}
throw new Exception(); //if all esle failes then that is an exception
//Will never be returned! Method just needed to return something
}
//Will parse input into a array of doubles
//Thanks to: http://javadevnotes.com/java-split-string-into-array-of-integers-example
private double[] inputParseToDoubleArray(String input) throws InvalidInputException {
if (isNumeric(input)) { // checks if input is even worth the trouble
String[] stringArray = input.split(" "); //Splits on blank spaces
double[] doubleArray = new double[stringArray.length]; //Array to return
for (int i = 0; i < stringArray.length; i++) { //Main for loop
String numberAsString = stringArray[i]; //makes the string singular
doubleArray[i] = Double.parseDouble(numberAsString); // Parses double
}
return doubleArray; //returns result
} else {
throw new InvalidInputException("Invalid input for comparing length."); //Inavlid input
}
}
//Checks if a string is numeric
public static boolean isNumeric(String str) //class made from example in citations, checks input for numeric characters. I was interested in for loop's condition.
{
boolean result = false;
for (char c : str.toCharArray()) {
if (Character.isDigit(c)) {
result = true;//;If any characters are numeric this returns true
}
}
return result; //Returning result
}
//Assings an array of length n to random doubles
public static String random(int dimmension) //makes 3 lines of assinging a random array into 1
{
Random rand = new Random(); //Our generator
double[] result = new double[dimmension]; //result to be returneds
String value = ""; //initializes value
for (int iterations = 0; iterations < dimmension; iterations++) {
result[iterations] = 0 + (1000.0 - 0.0) * rand.nextDouble(); //the parameters of the random are 0 - 1000
result[iterations] = (double) ((int) (result[iterations])); //Rounding to the 2nd decimal
value += Double.toString(result[iterations]) + " ";
}
return value.substring(0, value.length() - 1); //Gets rid of trailing space
}
//main method that runs everything, made by gui designer
public static void main(String args[]) {
try {
//Gives the nimbus look and feel
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
//This battery of catches keep the program running smothly no matter what
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(UIMain.class
.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(UIMain.class
.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(UIMain.class
.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(UIMain.class
.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new UIMain().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel DimmensionLabel;
private javax.swing.JLabel dimmensionLabel2;
private javax.swing.JButton distanceButton;
private javax.swing.JButton helpButton;
private javax.swing.JTextField input1;
private javax.swing.JTextField input2;
private javax.swing.JPopupMenu jPopupMenu1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JLabel label1;
private javax.swing.JLabel label2;
private javax.swing.JLabel labelForRandom1;
private javax.swing.JLabel labelForRandom2;
private javax.swing.JTextField random1Dim;
private javax.swing.JTextField random2Dim;
private javax.swing.JButton randomButton1;
private javax.swing.JButton randomButton2;
private javax.swing.JButton statsButton;
private javax.swing.JTextPane textPanel;
// End of variables declaration//GEN-END:variables
}
class InvalidInputException extends Exception { //The exception we throw when input is invalid
public InvalidInputException(String message) {
super(message); //Can return a message of your choosing
}
}