-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRPNForm.java
More file actions
698 lines (667 loc) · 25.7 KB
/
Copy pathRPNForm.java
File metadata and controls
698 lines (667 loc) · 25.7 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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
package RPN;
/* RPNForm.java */
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.ListIterator;
import javax.swing.*;
/**
* GUI for Reverse Polish Notation Calculator
* @author Paul Bladek, Bret A. Van Hof
*/
public class RPNForm extends JFrame
{
public static final int FRAME_WIDTH = 660;
public static final int FRAME_HEIGHT = 330;
public static final String MacroFile = "macroFile.txt";
private Container contentPane;
private JPanel displayPanel;
private JLabel RPNLabel;
private JTextField displayTextField;
private JPanel buttonPanel;
private JButton[][] buttonGrid;
private RPNCalculator calc;
private boolean helpMode = false;
private boolean recordMode = false;
private boolean msgOn = false;
private boolean commandPerformed = false;
private boolean getOn = false;
private boolean setOn = false;
private String inString = "";
private String displayString = "";
private String error = "error!";
private boolean wasDigit = false;
/**
* Creates and displays a window of the class RPNClaculator.
* @param args the command-line arguments
*/
public static void main(String[] args)
{
RPNForm gui = new RPNForm();
gui.setVisible(true);
}
/**
* constructor -- set up the form
*/
public RPNForm()
{
calc = new RPNCalculator();
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setTitle(" RPN Calculator");
setLocation(40, 40);
contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
setDisplayPanel();
}
/**
* sets up the displayPanel
*/
public final void setDisplayPanel()
{
/**
* inner class -- listens for any button actions
*/
class StatusListener implements ActionListener
{
/**
* deal with an action
* @param event --the actionEvent performed
*/
@Override
public void actionPerformed(ActionEvent event)
{
dealWith(event.getActionCommand());
displayTextField.requestFocusInWindow();
}
}
/**
* inner class -- listens for any button actions
*/
class DisplayListener implements KeyListener
{
/**
* not implemented
* @param event --the actionEvent performed
*/
@Override
public void keyPressed(KeyEvent event)
{}
/**
* deal with a keystroke
* @param event --the actionEvent performed
*/
@Override
public void keyReleased(KeyEvent event)
{
char c = event.getKeyChar();
if(event.getKeyCode() == KeyEvent.VK_SHIFT)
return;
if(event.getKeyCode() == KeyEvent.VK_BACK_SPACE ||
event.getKeyCode() == KeyEvent.VK_DELETE)
{
displayString = displayTextField.getText();
return;
}
displayTextField.setText(displayString);
if(validChar(c))
dealWith(String.valueOf(c));
displayTextField.requestFocusInWindow();
}
/**
* not implemented
* @param event --the actionEvent performed
*/
@Override
public void keyTyped(KeyEvent event)
{}
}
KeyListener displayListener = new DisplayListener();
ActionListener buttonListener = new StatusListener();
displayPanel = new JPanel( );
displayPanel.setLayout(new BoxLayout(displayPanel, BoxLayout.X_AXIS));
displayPanel.setBorder(BorderFactory.createMatteBorder(5, 5, 5, 5,
new Color(0XCC, 0X99, 0XFF)));
RPNLabel = new JLabel(" RPN ");
RPNLabel.setBackground(new Color(0X33, 0X00, 0X66));
RPNLabel.setFont(new Font("Courier New", 1, 36));
RPNLabel.setForeground(new Color(0x66, 0x33, 0x66));
displayPanel.add(RPNLabel);
displayTextField = new JTextField("");
displayTextField.setFont(new Font("Courier New", 1, 24));
displayTextField.setHorizontalAlignment(JTextField.RIGHT);
displayTextField.setActionCommand("Enter");
displayTextField.addKeyListener(displayListener);
displayPanel.add(displayTextField);
contentPane.add(displayPanel, BorderLayout.NORTH);
buttonPanel = new JPanel( );
buttonPanel.setBackground(new Color(0xff, 0xef, 0xdf));
buttonPanel.setLayout(new GridLayout(RPNCalculator.ROWS,
RPNCalculator.COLS));
buttonPanel.setBorder(BorderFactory.createMatteBorder(5, 5, 5, 5,
new Color(0X99, 0XFF, 0XFF)));
buttonGrid = new JButton[RPNCalculator.ROWS][RPNCalculator.COLS];
for(int i = 0; i < RPNCalculator.ROWS; i++)
{
for(int j = 0; j < RPNCalculator.COLS; j++)
{
buttonGrid[i][j] = new JButton();
buttonGrid[i][j].setFont(new Font("Courier New", 1, 16));
buttonGrid[i][j].addActionListener(buttonListener);
buttonGrid[i][j].setBorder(BorderFactory.createBevelBorder(1));
buttonPanel.add(buttonGrid[i][j]);
}
}
buttonGrid[0][0].setText("eXit");
buttonGrid[0][1].setText("C");
buttonGrid[0][2].setText("CE");
buttonGrid[0][3].setFont(new Font("Courier New", 1, 20));
buttonGrid[0][3].setBackground(new Color(0xf0, 0xf6, 0xff));
buttonGrid[0][3].setForeground(new Color(0x99, 0x00, 0x66));
buttonGrid[0][3].setText("7");
buttonGrid[0][4].setFont(new Font("Courier New", 1, 20));
buttonGrid[0][4].setBackground(new Color(0xf0, 0xf6, 0xff));
buttonGrid[0][4].setForeground(new Color(0x99, 0x00, 0x66));
buttonGrid[0][4].setText("8");
buttonGrid[0][5].setFont(new Font("Courier New", 1, 20));
buttonGrid[0][5].setBackground(new Color(0xf0, 0xf6, 0xff));
buttonGrid[0][5].setForeground(new Color(0x99, 0x00, 0x66));
buttonGrid[0][5].setText("9");
buttonGrid[0][6].setText("+");
buttonGrid[0][7].setText("x");
buttonGrid[1][0].setText("Set");
buttonGrid[1][1].setText("Get");
buttonGrid[1][2].setText("Up");
buttonGrid[1][3].setFont(new Font("Courier New", 1, 20));
buttonGrid[1][3].setBackground(new Color(0xf0, 0xf6, 0xff));
buttonGrid[1][3].setForeground(new Color(0x99, 0x00, 0x66));
buttonGrid[1][3].setText("4");
buttonGrid[1][4].setFont(new Font("Courier New", 1, 20));
buttonGrid[1][4].setBackground(new Color(0xf0, 0xf6, 0xff));
buttonGrid[1][4].setForeground(new Color(0x99, 0x00, 0x66));
buttonGrid[1][4].setText("5");
buttonGrid[1][5].setFont(new Font("Courier New", 1, 20));
buttonGrid[1][5].setBackground(new Color(0xf0, 0xf6, 0xff));
buttonGrid[1][5].setForeground(new Color(0x99, 0x00, 0x66));
buttonGrid[1][5].setText("6");
buttonGrid[1][6].setText("-");
buttonGrid[1][7].setText("/");
buttonGrid[2][0].setText("Load");
buttonGrid[2][1].setText("Save");
buttonGrid[2][2].setText("Down");
buttonGrid[2][3].setFont(new Font("Courier New", 1, 20));
buttonGrid[2][3].setBackground(new Color(0xf0, 0xf6, 0xff));
buttonGrid[2][3].setForeground(new Color(0x99, 0x00, 0x66));
buttonGrid[2][3].setText("1");
buttonGrid[2][4].setFont(new Font("Courier New", 1, 20));
buttonGrid[2][4].setBackground(new Color(0xf0, 0xf6, 0xff));
buttonGrid[2][4].setForeground(new Color(0x99, 0x00, 0x66));
buttonGrid[2][4].setText("2");
buttonGrid[2][5].setFont(new Font("Courier New", 1, 20));
buttonGrid[2][5].setBackground(new Color(0xf0, 0xf6, 0xff));
buttonGrid[2][5].setForeground(new Color(0x99, 0x00, 0x66));
buttonGrid[2][5].setText("3");
buttonGrid[2][6].setText("^");
buttonGrid[2][7].setText("%");
buttonGrid[3][0].setText("Rec");
buttonGrid[3][1].setText("Run");
buttonGrid[3][2].setText("?");
buttonGrid[3][3].setText("+/-");
buttonGrid[3][4].setFont(new Font("Courier New", 1, 20));
buttonGrid[3][4].setBackground(new Color(0xf0, 0xf6, 0xff));
buttonGrid[3][4].setForeground(new Color(0x99, 0x00, 0x66));
buttonGrid[3][4].setText("0");
buttonGrid[3][5].setText(".");
buttonGrid[3][6].setText("1/n");
buttonGrid[3][7].setFont(new Font("Courier New", 1, 15));
buttonGrid[3][7].setBackground(new Color(0xf6, 0xf0, 0xff));
buttonGrid[3][7].setForeground(new Color(0x99, 0x00, 0x66));
buttonGrid[3][7].setText("Enter");
contentPane.add(buttonPanel, BorderLayout.CENTER);
disableAlpha();
}
/**
* method: enter
* sets displayString to an empty string, pushes the String displayed on
* displayTextField to the stack if it can be parsed to a double, and sets
* the displayTextField to display inString, commandPerformed is set to
* false
*/
public void enter()
{
if(!displayTextField.getText().equals("") || !displayTextField.getText().
equals(error) || !displayTextField.getText().equals("?"))
{
calc.theStack.push(Double.parseDouble(displayTextField.getText
()));
displayString = "";
inString = calc.theStack.peek() + "";
displayTextField.setText(inString);
commandPerformed = false;
}
}
/**
* deal with an action
* @param actionCommand --the actionEvent performed
*/
public void dealWith(String actionCommand)
{
if(msgOn)
{
msgOn = false;
displayTextField.setForeground(Color.BLACK);
if(recordMode)
displayTextField.setForeground(Color.MAGENTA);
displayTextField.setText("");
}
try
{
if(helpMode)
{
displayHelp(actionCommand);
helpMode = false;
return;
}
else
inString = displayTextField.getText();
if(getOn)
{
getOn = false;
String test = actionCommand;
if(recordMode)
calc.instructions.add(test);
if(Character.isDigit(test.charAt(0)))
{
double aDigit = calc.register[Integer.parseInt
(test.charAt(0) + "")];
displayTextField.setText
(aDigit + "");
displayString = aDigit + "";
}
return;
}
if(setOn)
{
setOn = false;
String test = actionCommand;
if(recordMode)
calc.instructions.add(test);
if(Character.isDigit(test.charAt(0)))
calc.register[Integer.parseInt(test.charAt(0) + "")] =
Double.parseDouble(displayTextField.getText());
return;
}
if(recordMode)
{
displayTextField.setForeground(Color.MAGENTA);
if(!actionCommand.equals("Run"))
{
if(Character.isDigit(actionCommand.charAt(0)))
{
calc.instructions.add(actionCommand);
wasDigit = true;
}
else
{
if(!wasDigit)
{
calc.instructions.add(actionCommand);
}
else if(!actionCommand.equals("Rec"))
{
calc.instructions.add(actionCommand);
}
}
}
}
if(actionCommand.equals("?"))
{
helpMode = true;
displayTextField.setForeground(new Color(0, 0X99, 0X66));
displayTextField.setText(inString = "?");
}
else if(actionCommand.equals("eXit"))
System.exit(0);
else if(actionCommand.equals("Save"))
{
PrintWriter out = new PrintWriter(new FileOutputStream
(MacroFile));
Iterator itr = calc.instructions.iterator();
while(itr.hasNext())
out.println((String)itr.next());
out.close();
}
else if(actionCommand.equals("Load"))
{
BufferedReader in
= new BufferedReader(new FileReader(MacroFile));
calc.instructions.clear();
while(in.ready())
calc.instructions.add(in.readLine());
in.close();
}
else if(actionCommand.equals("Rec"))
{
if(recordMode)
{
recordMode = false;
displayTextField.setForeground(Color.BLACK);
}
else
{
recordMode = true;
calc.instructions.clear();
}
}
else if(actionCommand.equals("Run"))
{
if(recordMode)
displayTextField.setForeground(Color.BLACK);
recordMode = false;
displayString = "";
inString = "";
displayTextField.setText("");
ListIterator itr = calc.instructions.listIterator();
while(itr.hasNext())
dealWith((String)itr.next());
}
else if(actionCommand.equals("C")
|| actionCommand.equals("c"))
{
displayTextField.setText("");
displayString = "";
inString = "";
calc.theStack.pop();
if(calc.theStack.size() > 0)
displayTextField.setText(calc.theStack.peek() + "");
}
else if(actionCommand.equals("CE"))
{
displayTextField.setText("");
displayString = "";
inString = "";
calc.theStack.clear();
}
else if(actionCommand.equals("Get"))
{
getOn = true;
}
else if(actionCommand.equals("Set"))
{
setOn = true;
}
else if(actionCommand.equals("Enter")
|| actionCommand.equals("\\n"))
{
enter();
}
else if (actionCommand.equals("1/n"))
{
displayString = displayTextField.getText();
if(displayString.equals(error))
displayString = calc.theStack.peek() + "";
double aDouble = Double.parseDouble(displayString);
if(aDouble != 0)
aDouble = 1 / aDouble;
displayString = "" + aDouble;
displayTextField.setText(displayString);
}
else if(actionCommand.equals("+/-"))
{
displayString = displayTextField.getText();
if(displayString.substring(0,1).equals("-"))
{
displayString = displayString.substring(1);
displayTextField.setText(displayString);
}
else if(Double.parseDouble(displayString) != 0)
displayString = "-" + displayString;
displayTextField.setText(displayString);
}
else if (actionCommand.equals("."))
{
if(displayString.contains("."))
return;
if(displayString.contains("E"))
return;
if(displayString.equals(""))
displayString += "0.";
else
displayString += ".";
displayTextField.setText(displayString);
}
else if(Character.isDigit(actionCommand.charAt(0)))
{
displayString += actionCommand.charAt(0);
displayTextField.setText(displayString);
commandPerformed = false;
}
else if(actionCommand.equals("x")
|| actionCommand.equals("X")
|| actionCommand.equals("*"))
{
if(!displayString.equals("") && !commandPerformed)
enter();
if(calc.theStack.size() < RPNCalculator.BINARY)
displayTextField.setText(error);
else
{
double answer = calc.multiply();
inString = "" + answer;
displayTextField.setText(inString);
commandPerformed = true;
}
}
else if(actionCommand.equals("+"))
{
if(!displayString.equals("") && !commandPerformed)
enter();
if(calc.theStack.size() < RPNCalculator.BINARY)
displayTextField.setText(error);
else
{
double answer = calc.add();
inString = "" + answer;
displayTextField.setText(inString);
commandPerformed = true;
}
}
else if(actionCommand.equals("-"))
{
if(!displayString.equals("") && !commandPerformed)
enter();
if(calc.theStack.size() < RPNCalculator.BINARY)
displayTextField.setText(error);
else
{
double answer = calc.subtract();
inString = "" + answer;
displayTextField.setText(inString);
commandPerformed = true;
}
}
else if(actionCommand.equals("/"))
{
if(!displayString.equals("") && !commandPerformed)
enter();
if(calc.theStack.size() < RPNCalculator.BINARY)
displayTextField.setText(error);
else
{
double answer = calc.divide();
inString = "" + answer;
displayTextField.setText(inString);
commandPerformed = true;
}
}
else if(actionCommand.equals("^"))
{
if(!displayString.equals("") && !commandPerformed)
enter();
if(calc.theStack.size() < RPNCalculator.BINARY)
displayTextField.setText(error);
else
{
double answer = calc.exponentiation();
inString = "" + answer;
displayTextField.setText(inString);
commandPerformed = true;
}
}
else if(actionCommand.equals("%"))
{
if(!displayString.equals("") && !commandPerformed)
enter();
if(calc.theStack.size() < RPNCalculator.BINARY)
displayTextField.setText(error);
else
{
double answer = calc.modOperator();
inString = "" + answer;
displayTextField.setText(inString);
commandPerformed = true;
}
}
else if(actionCommand.equals("Up"))
{
calc.up();
displayTextField.setText(calc.theStack.peek() + "");
}
else if(actionCommand.equals("Down"))
{
calc.down();
displayTextField.setText(calc.theStack.peek() + "");
}
}
catch(Exception e)
{
displayTextField.setText("");
}
}
/**
* displays the appropriate help
* @param actionCommand the command from the triggering event
*/
private void displayHelp(String actionCommand)
{
msgOn = true;
if(actionCommand.equals("eXit"))
displayTextField.setText("eXit: Exits program");
else if(actionCommand.equals("C")
|| actionCommand.equals("c"))
displayTextField.setText("C: Clears top element");
else if(actionCommand.equals("CE"))
displayTextField.setText("CE: Clears entire stack");
else if(actionCommand.equals("+"))
displayTextField.setText("+: adds top 2 elements");
else if(actionCommand.equals("x")
|| actionCommand.equals("X")
|| actionCommand.equals("*"))
displayTextField.setText("x: multiplies top 2 elements");
else if(actionCommand.equals("Set"))
displayTextField.setText("Set: Sets register (0-9)");
else if(actionCommand.equals("Get"))
displayTextField.setText("Get: gets register (0-9)");
else if(actionCommand.equals("Up"))
displayTextField.setText("Up: Rotates stack up");
else if(actionCommand.equals("-"))
displayTextField.setText("-: subtracts top 2 elements");
else if(actionCommand.equals("/"))
displayTextField.setText("/: divides top 2 elements");
else if(actionCommand.equals("Load"))
displayTextField.setText("Load: Loads program from file");
else if(actionCommand.equals("Save"))
displayTextField.setText("Save: Saves program to file");
else if(actionCommand.equals("Down"))
displayTextField.setText("Down: Rotates stack down");
else if(actionCommand.equals("^"))
displayTextField.setText("^: exponent");
else if(actionCommand.equals("%"))
displayTextField.setText("%: Mods top 2 elements");
else if(actionCommand.equals("Rec"))
displayTextField.setText("Rec: Program mode on/off");
else if(actionCommand.equals("Run"))
displayTextField.setText("Run: Runs program");
else if(actionCommand.equals("?"))
displayTextField.setText("?: press ? then key for help");
else if(actionCommand.equals("+/-"))
displayTextField.setText("+/-: changes sign of number");
else if(actionCommand.equals("1/n"))
displayTextField.setText("1/n: inverts the number");
else if(actionCommand.equals("Enter"))
displayTextField.setText("Enter: element to stack");
else
displayTextField.setText("");
}
/**
*
* @param c the character to test
* @return true is c is valid, false otherwise
*/
private boolean validChar(char c)
{
if(Character.isDigit(c))
return true;
switch(c)
{
case '+':
case '-':
case '*':
case 'x':
case 'X':
case '/':
case 'C':
case 'c':
case '^':
case '%':
case '?':
case '.':
case '\r':
case '\n':
return true;
}
return false;
}
/**
* disables non-numeric-related keys
*/
private void disableAlpha()
{
for(char c = '\0'; c < '%'; c++)
displayTextField.getInputMap().put(KeyStroke.getKeyStroke(c),
"none");
for(char c = '&'; c < '*'; c++)
displayTextField.getInputMap().put(KeyStroke.getKeyStroke(c),
"none");
for(char c = ':'; c <= '?'; c++)
displayTextField.getInputMap().put(KeyStroke.getKeyStroke(c),
"none");
for(char c = '@'; c <= 'C'; c++)
displayTextField.getInputMap().put(KeyStroke.getKeyStroke(c),
"none");
for(char c = 'D'; c <= 'X'; c++)
displayTextField.getInputMap().put(KeyStroke.getKeyStroke(c),
"none");
for(char c = 'Y'; c <= '^'; c++)
displayTextField.getInputMap().put(KeyStroke.getKeyStroke(c),
"none");
for(char c = '_'; c <= 'c'; c++)
displayTextField.getInputMap().put(KeyStroke.getKeyStroke(c),
"none");
for(char c = 'd'; c <= 'x'; c++)
displayTextField.getInputMap().put(KeyStroke.getKeyStroke(c),
"none");
for(char c = 'y'; c <= '~'; c++)
displayTextField.getInputMap().put(KeyStroke.getKeyStroke(c),
"none");
displayTextField.getInputMap().put(KeyStroke.getKeyStroke('/'),
"none");
}
}