001package hw3;
002
003import java.awt.*;
004import java.awt.event.*;
005
006import javax.swing.*;
007import javax.swing.border.BevelBorder;
008import javax.swing.border.Border;
009
010
011/**
012 * <B>CalculatorFrame</b> is part of the graphical calculator that utilizes all
013 * of the other classes in package hw3. CalculatorFrame contains the main()
014 * function which starts the calculator.
015 */
016
017public final class CalculatorFrame extends JFrame {
018    JPanel jPanel1 = new JPanel();
019
020    JPanel jPanel2 = new JPanel();
021
022    JPanel jPanel3 = new JPanel();
023
024    JPanel jPanel4 = new JPanel();
025
026    JPanel jPanel5 = new JPanel();
027
028    JPanel jPanel6 = new JPanel();
029
030    JPanel jPanel8 = new JPanel();
031
032    Border border1;
033
034    JLabel jLabel5 = new JLabel();
035
036    JLabel jLabel6 = new JLabel();
037
038    JTextField jTextField1 = new JTextField();
039
040    JTextField jTextField2 = new JTextField();
041
042    JButton jButton_exp = new JButton();
043
044    JButton jButton_div = new JButton();
045
046    JButton jButton_mul = new JButton();
047
048    JButton jButton_7 = new JButton();
049
050    JButton jButton_8 = new JButton();
051
052    JButton jButton_9 = new JButton();
053
054    JButton jButton_4 = new JButton();
055
056    JButton jButton_5 = new JButton();
057
058    JButton jButton_6 = new JButton();
059
060    JButton jButton_1 = new JButton();
061
062    JButton jButton_2 = new JButton();
063
064    JButton jButton_3 = new JButton();
065
066    JButton jButton_0 = new JButton();
067
068    JButton jButton_x = new JButton();
069
070    JButton jButton_Enter = new JButton();
071
072    JButton jButton_plus = new JButton();
073
074    JButton jButton_minus = new JButton();
075
076    JButton jButton_swap = new JButton();
077
078    JButton jButton_pop = new JButton();
079
080    JButton jButton_dup = new JButton();
081
082    JButton jButton_int = new JButton();
083
084    JButton jButton_dif = new JButton();
085
086    JTextArea jTextArea_Current = new JTextArea();
087
088    JTextArea jTextArea_Stack_3 = new JTextArea();
089
090    JTextArea jTextArea_Stack_2 = new JTextArea();
091
092    JTextArea jTextArea_Stack_1 = new JTextArea();
093
094    JTextArea jTextArea_Stack_0 = new JTextArea();
095
096    BorderLayout borderLayout1 = new BorderLayout();
097
098    PolyGraph pg;
099
100    RatPolyStack stack;
101
102    Border border2;
103
104    Border border3;
105
106    Border border4;
107
108    Border border5;
109
110    JPanel jPanel7 = new JPanel();
111
112    JButton jButton_stack_mul = new JButton();
113
114    JButton jButton_stack_div = new JButton();
115
116    JButton jButton_stack_add = new JButton();
117
118    JButton jButton_stack_sub = new JButton();
119
120    JButton jButton_Clear = new JButton();
121
122    // Serializable classes are supposed to have this
123    private static final long serialVersionUID = 42L;
124
125    public CalculatorFrame() {
126        try {
127            // Student-written code that may throw an exception
128            stack = new RatPolyStack();
129
130            jbInit();
131            // exit the application when this window is closed
132            addWindowListener(new WindowAdapter() {
133                @Override
134                public void windowClosing(WindowEvent e) {
135                    setVisible(false);
136                    dispose();
137                    System.exit(0);
138                }
139            });
140        } catch (Exception e) {
141            e.printStackTrace();
142        }
143    }
144
145    /*@AssertNonNullAfter({"pg", "border1", "border2", "border3", "border4", "border5"})*/
146    private void jbInit() throws Exception {
147        border1 = BorderFactory
148                .createBevelBorder(BevelBorder.LOWERED, Color.white,
149                        Color.white, Color.white, new Color(124, 124, 124));
150        border2 = BorderFactory.createBevelBorder(BevelBorder.LOWERED,
151                Color.red, Color.white, new Color(178, 178, 178), new Color(
152                        124, 124, 124));
153        border3 = BorderFactory.createBevelBorder(BevelBorder.LOWERED,
154                Color.blue, Color.white, new Color(178, 178, 178), new Color(
155                        124, 124, 124));
156        border4 = BorderFactory.createBevelBorder(BevelBorder.LOWERED,
157                Color.green, Color.white, new Color(178, 178, 178), new Color(
158                        124, 124, 124));
159        border5 = BorderFactory.createBevelBorder(BevelBorder.LOWERED,
160                Color.orange, Color.white, new Color(178, 178, 178), new Color(
161                        124, 124, 124));
162        jPanel1.setLayout(null);
163        jPanel1.setBackground(Color.gray);
164        jPanel1.setBorder(BorderFactory.createRaisedBevelBorder());
165        jPanel2.setBorder(BorderFactory.createRaisedBevelBorder());
166        jPanel2.setBounds(new Rectangle(21, 21, 330, 371));
167        jPanel2.setLayout(borderLayout1);
168        jPanel3.setBorder(BorderFactory.createRaisedBevelBorder());
169        jPanel3.setBounds(new Rectangle(366, 23, 259, 219));
170        jPanel3.setLayout(null);
171        jPanel4.setBorder(BorderFactory.createRaisedBevelBorder());
172        jPanel4.setBounds(new Rectangle(365, 258, 258, 132));
173        jPanel4.setLayout(null);
174        jPanel5.setBorder(BorderFactory.createRaisedBevelBorder());
175        jPanel5.setBounds(new Rectangle(21, 410, 167, 90));
176        jPanel5.setLayout(null);
177        jPanel6.setBorder(BorderFactory.createRaisedBevelBorder());
178        jPanel6.setBounds(new Rectangle(197, 410, 196, 275));
179        jPanel6.setLayout(null);
180        jPanel8.setBorder(BorderFactory.createRaisedBevelBorder());
181        jPanel8.setBounds(new Rectangle(21, 510, 167, 90));
182        jPanel8.setLayout(null);
183        jLabel5.setText("X max");
184        jLabel5.setBounds(new Rectangle(8, 47, 34, 25));
185        jLabel6.setBounds(new Rectangle(10, 10, 34, 24));
186        jLabel6.setText("X min");
187        jTextField1.setText("-1.0");
188        jTextField1.setBounds(new Rectangle(50, 15, 104, 22));
189        jTextField1.addActionListener(new java.awt.event.ActionListener() {
190           @Override
191            public void actionPerformed(ActionEvent e) {
192                jTextField1_actionPerformed(e);
193            }
194        });
195        jTextField2.setBounds(new Rectangle(49, 51, 104, 22));
196        jTextField2.addActionListener(new java.awt.event.ActionListener() {
197            @Override
198            public void actionPerformed(ActionEvent e) {
199                jTextField2_actionPerformed(e);
200            }
201        });
202        jTextField2.setText("1.0");
203        jButton_exp.setFont(new java.awt.Font("Dialog", 0, 18));
204        jButton_exp.setMargin(new Insets(2, 2, 2, 2));
205        jButton_exp.setText("^");
206        jButton_exp.setBounds(new Rectangle(9, 10, 41, 44));
207        jButton_exp.addActionListener(new java.awt.event.ActionListener() {
208            @Override
209            public void actionPerformed(ActionEvent e) {
210                jButton_exp_actionPerformed(e);
211            }
212        });
213
214        jButton_int.setBounds(new Rectangle(10, 7, 146, 35));
215        jButton_int.setText("Integrate");
216        jButton_int.setFont(new java.awt.Font("Dialog", 0, 18));
217        jButton_int.setMargin(new Insets(2, 2, 2, 2));
218        jButton_int.addActionListener(new java.awt.event.ActionListener() {
219            @Override
220            public void actionPerformed(ActionEvent e) {
221                jButton_int_actionPerformed(e);
222            }
223        });
224
225        jButton_dif.setBounds(new Rectangle(10, 47, 146, 36));
226        jButton_dif.setText("Differentiate");
227        jButton_dif.setFont(new java.awt.Font("Dialog", 0, 18));
228        jButton_dif.setMargin(new Insets(2, 2, 2, 2));
229        jButton_dif.addActionListener(new java.awt.event.ActionListener() {
230            @Override
231            public void actionPerformed(ActionEvent e) {
232                jButton_dif_actionPerformed(e);
233            }
234        });
235
236        jButton_div.setBounds(new Rectangle(103, 10, 41, 44));
237        jButton_div.addActionListener(new java.awt.event.ActionListener() {
238            @Override
239            public void actionPerformed(ActionEvent e) {
240                jButton_div_actionPerformed(e);
241            }
242        });
243        jButton_div.setText("/");
244        jButton_div.setFont(new java.awt.Font("Dialog", 0, 18));
245        jButton_div.setMargin(new Insets(2, 2, 2, 2));
246        jButton_mul.setFont(new java.awt.Font("Dialog", 0, 18));
247        jButton_mul.setMargin(new Insets(2, 2, 2, 2));
248        jButton_mul.setText("*");
249        jButton_mul.setBounds(new Rectangle(55, 10, 41, 44));
250        jButton_mul.addActionListener(new java.awt.event.ActionListener() {
251            @Override
252            public void actionPerformed(ActionEvent e) {
253                jButton_mul_actionPerformed(e);
254            }
255        });
256        jButton_7.setBounds(new Rectangle(9, 60, 41, 44));
257        jButton_7.addActionListener(new java.awt.event.ActionListener() {
258            @Override
259            public void actionPerformed(ActionEvent e) {
260                jButton_7_actionPerformed(e);
261            }
262        });
263        jButton_7.setFont(new java.awt.Font("Dialog", 1, 18));
264        jButton_7.setHorizontalTextPosition(SwingConstants.CENTER);
265        jButton_7.setMargin(new Insets(2, 2, 2, 2));
266        jButton_7.setText("7");
267        jButton_8.setText("8");
268        jButton_8.setHorizontalTextPosition(SwingConstants.CENTER);
269        jButton_8.setMargin(new Insets(2, 2, 2, 2));
270        jButton_8.setFont(new java.awt.Font("Dialog", 1, 18));
271        jButton_8.setBounds(new Rectangle(55, 60, 41, 44));
272        jButton_8.addActionListener(new java.awt.event.ActionListener() {
273            @Override
274            public void actionPerformed(ActionEvent e) {
275                jButton_8_actionPerformed(e);
276            }
277        });
278        jButton_9.setBounds(new Rectangle(103, 60, 41, 44));
279        jButton_9.addActionListener(new java.awt.event.ActionListener() {
280            @Override
281            public void actionPerformed(ActionEvent e) {
282                jButton_9_actionPerformed(e);
283            }
284        });
285        jButton_9.setFont(new java.awt.Font("Dialog", 1, 18));
286        jButton_9.setHorizontalTextPosition(SwingConstants.CENTER);
287        jButton_9.setMargin(new Insets(2, 2, 2, 2));
288        jButton_9.setText("9");
289        jButton_4.setText("4");
290        jButton_4.setHorizontalTextPosition(SwingConstants.CENTER);
291        jButton_4.setMargin(new Insets(2, 2, 2, 2));
292        jButton_4.setFont(new java.awt.Font("Dialog", 1, 18));
293        jButton_4.setBounds(new Rectangle(9, 111, 41, 44));
294        jButton_4.addActionListener(new java.awt.event.ActionListener() {
295            @Override
296            public void actionPerformed(ActionEvent e) {
297                jButton_4_actionPerformed(e);
298            }
299        });
300        jButton_5.setBounds(new Rectangle(55, 111, 41, 44));
301        jButton_5.addActionListener(new java.awt.event.ActionListener() {
302            @Override
303            public void actionPerformed(ActionEvent e) {
304                jButton_5_actionPerformed(e);
305            }
306        });
307        jButton_5.setFont(new java.awt.Font("Dialog", 1, 18));
308        jButton_5.setHorizontalTextPosition(SwingConstants.CENTER);
309        jButton_5.setMargin(new Insets(2, 2, 2, 2));
310        jButton_5.setText("5");
311        jButton_6.setText("6");
312        jButton_6.setHorizontalTextPosition(SwingConstants.CENTER);
313        jButton_6.setMargin(new Insets(2, 2, 2, 2));
314        jButton_6.setFont(new java.awt.Font("Dialog", 1, 18));
315        jButton_6.setBounds(new Rectangle(103, 111, 41, 44));
316        jButton_6.addActionListener(new java.awt.event.ActionListener() {
317            @Override
318            public void actionPerformed(ActionEvent e) {
319                jButton_6_actionPerformed(e);
320            }
321        });
322        jButton_1.setBounds(new Rectangle(9, 163, 41, 44));
323        jButton_1.addActionListener(new java.awt.event.ActionListener() {
324            @Override
325            public void actionPerformed(ActionEvent e) {
326                jButton_1_actionPerformed(e);
327            }
328        });
329        jButton_1.setFont(new java.awt.Font("Dialog", 1, 18));
330        jButton_1.setHorizontalTextPosition(SwingConstants.CENTER);
331        jButton_1.setMargin(new Insets(2, 2, 2, 2));
332        jButton_1.setText("1");
333        jButton_2.setText("2");
334        jButton_2.setHorizontalTextPosition(SwingConstants.CENTER);
335        jButton_2.setMargin(new Insets(2, 2, 2, 2));
336        jButton_2.setFont(new java.awt.Font("Dialog", 1, 18));
337        jButton_2.setBounds(new Rectangle(55, 163, 41, 44));
338        jButton_2.addActionListener(new java.awt.event.ActionListener() {
339            @Override
340            public void actionPerformed(ActionEvent e) {
341                jButton_2_actionPerformed(e);
342            }
343        });
344        jButton_3.setBounds(new Rectangle(103, 163, 41, 44));
345        jButton_3.addActionListener(new java.awt.event.ActionListener() {
346            @Override
347            public void actionPerformed(ActionEvent e) {
348                jButton_3_actionPerformed(e);
349            }
350        });
351        jButton_3.setFont(new java.awt.Font("Dialog", 1, 18));
352        jButton_3.setHorizontalTextPosition(SwingConstants.CENTER);
353        jButton_3.setMargin(new Insets(2, 2, 2, 2));
354        jButton_3.setText("3");
355        jButton_0.setText("0");
356        jButton_0.setHorizontalTextPosition(SwingConstants.CENTER);
357        jButton_0.setMargin(new Insets(2, 2, 2, 2));
358        jButton_0.setFont(new java.awt.Font("Dialog", 1, 18));
359        jButton_0.setBounds(new Rectangle(9, 218, 89, 44));
360        jButton_0.addActionListener(new java.awt.event.ActionListener() {
361            @Override
362            public void actionPerformed(ActionEvent e) {
363                jButton_0_actionPerformed(e);
364            }
365        });
366        jButton_x.setText("x");
367        jButton_x.setHorizontalTextPosition(SwingConstants.CENTER);
368        jButton_x.setMargin(new Insets(2, 2, 2, 2));
369        jButton_x.setFont(new java.awt.Font("Dialog", 1, 18));
370        jButton_x.setBounds(new Rectangle(103, 218, 41, 44));
371        jButton_x.addActionListener(new java.awt.event.ActionListener() {
372            @Override
373            public void actionPerformed(ActionEvent e) {
374                jButton_x_actionPerformed(e);
375            }
376        });
377        jButton_Enter.setBounds(new Rectangle(8, 171, 77, 44));
378        jButton_Enter.addActionListener(new java.awt.event.ActionListener() {
379            @Override
380            public void actionPerformed(ActionEvent e) {
381                jButton_Enter_actionPerformed(e);
382            }
383        });
384        jButton_Enter.setFont(new java.awt.Font("Dialog", 1, 18));
385        jButton_Enter.setHorizontalTextPosition(SwingConstants.CENTER);
386        jButton_Enter.setMargin(new Insets(2, 2, 2, 2));
387        jButton_Enter.setText("Enter");
388        jButton_plus.setBounds(new Rectangle(149, 10, 41, 44));
389        jButton_plus.addActionListener(new java.awt.event.ActionListener() {
390            @Override
391            public void actionPerformed(ActionEvent e) {
392                jButton_plus_actionPerformed(e);
393            }
394        });
395        jButton_plus.setFont(new java.awt.Font("Dialog", 1, 18));
396        jButton_plus.setHorizontalTextPosition(SwingConstants.CENTER);
397        jButton_plus.setMargin(new Insets(2, 2, 2, 2));
398        jButton_plus.setText("+");
399        jButton_minus.setText("-");
400        jButton_minus.setHorizontalTextPosition(SwingConstants.CENTER);
401        jButton_minus.setMargin(new Insets(2, 2, 2, 2));
402        jButton_minus.setFont(new java.awt.Font("Dialog", 1, 18));
403        jButton_minus.setBounds(new Rectangle(149, 61, 41, 44));
404        jButton_minus.addActionListener(new java.awt.event.ActionListener() {
405            @Override
406            public void actionPerformed(ActionEvent e) {
407                jButton_minus_actionPerformed(e);
408            }
409        });
410        jButton_swap.setText("Swap");
411        jButton_swap.setHorizontalTextPosition(SwingConstants.CENTER);
412        jButton_swap.setMargin(new Insets(2, 2, 2, 2));
413        jButton_swap.setFont(new java.awt.Font("Dialog", 1, 18));
414        jButton_swap.setBounds(new Rectangle(8, 8, 75, 44));
415        jButton_swap.addActionListener(new java.awt.event.ActionListener() {
416            @Override
417            public void actionPerformed(ActionEvent e) {
418                jButton_swap_actionPerformed(e);
419            }
420        });
421        jButton_pop.setBounds(new Rectangle(8, 63, 75, 44));
422        jButton_pop.addActionListener(new java.awt.event.ActionListener() {
423            @Override
424            public void actionPerformed(ActionEvent e) {
425                jButton_pop_actionPerformed(e);
426            }
427        });
428        jButton_pop.setFont(new java.awt.Font("Dialog", 1, 18));
429        jButton_pop.setHorizontalTextPosition(SwingConstants.CENTER);
430        jButton_pop.setMargin(new Insets(2, 2, 2, 2));
431        jButton_pop.setText("Pop");
432        jButton_dup.setText("Dup");
433        jButton_dup.setHorizontalTextPosition(SwingConstants.CENTER);
434        jButton_dup.setMargin(new Insets(2, 2, 2, 2));
435        jButton_dup.setFont(new java.awt.Font("Dialog", 1, 18));
436        jButton_dup.setBounds(new Rectangle(8, 118, 76, 44));
437        jButton_dup.addActionListener(new java.awt.event.ActionListener() {
438            @Override
439            public void actionPerformed(ActionEvent e) {
440                jButton_dup_actionPerformed(e);
441            }
442        });
443        jTextArea_Current.setLineWrap(true);
444        jTextArea_Current.setBorder(BorderFactory.createLoweredBevelBorder());
445        jTextArea_Current.setBounds(new Rectangle(8, 11, 239, 111));
446        jTextArea_Stack_3.setLineWrap(true);
447        jTextArea_Stack_3.setBorder(border5);
448        jTextArea_Stack_3.setEditable(false);
449        jTextArea_Stack_3.setBounds(new Rectangle(8, 10, 241, 40));
450        jTextArea_Stack_2.setBounds(new Rectangle(8, 57, 241, 40));
451        jTextArea_Stack_2.setLineWrap(true);
452        jTextArea_Stack_2.setBorder(border4);
453        jTextArea_Stack_2.setEditable(false);
454        jTextArea_Stack_1.setLineWrap(true);
455        jTextArea_Stack_1.setBorder(border3);
456        jTextArea_Stack_1.setEditable(false);
457        jTextArea_Stack_1.setBounds(new Rectangle(8, 105, 241, 40));
458        jTextArea_Stack_0.setLineWrap(true);
459        jTextArea_Stack_0.setBorder(border2);
460        jTextArea_Stack_0.setEditable(false);
461        jTextArea_Stack_0.setBounds(new Rectangle(8, 154, 241, 40));
462        jPanel7.setBorder(BorderFactory.createRaisedBevelBorder());
463        jPanel7.setBounds(new Rectangle(403, 411, 199, 283));
464        jPanel7.setLayout(null);
465        jButton_stack_mul
466                .addActionListener(new java.awt.event.ActionListener() {
467                    @Override
468                    public void actionPerformed(ActionEvent e) {
469                        jButton_stack_mul_actionPerformed(e);
470                    }
471                });
472        jButton_stack_mul.setBounds(new Rectangle(105, 118, 75, 44));
473        jButton_stack_mul.setFont(new java.awt.Font("Dialog", 1, 18));
474        jButton_stack_mul.setMargin(new Insets(2, 2, 2, 2));
475        jButton_stack_mul.setHorizontalTextPosition(SwingConstants.CENTER);
476        jButton_stack_mul.setText("Mul");
477        jButton_stack_div.setText("Div");
478        jButton_stack_div.setHorizontalTextPosition(SwingConstants.CENTER);
479        jButton_stack_div.setMargin(new Insets(2, 2, 2, 2));
480        jButton_stack_div.setFont(new java.awt.Font("Dialog", 1, 18));
481        jButton_stack_div.setBounds(new Rectangle(105, 171, 75, 44));
482        jButton_stack_div
483                .addActionListener(new java.awt.event.ActionListener() {
484                    @Override
485                    public void actionPerformed(ActionEvent e) {
486                        jButton_stack_div_actionPerformed(e);
487                    }
488                });
489        jButton_stack_add.setText("Add");
490        jButton_stack_add.setHorizontalTextPosition(SwingConstants.CENTER);
491        jButton_stack_add.setMargin(new Insets(2, 2, 2, 2));
492        jButton_stack_add.setFont(new java.awt.Font("Dialog", 1, 18));
493        jButton_stack_add.setBounds(new Rectangle(105, 8, 75, 44));
494        jButton_stack_add
495                .addActionListener(new java.awt.event.ActionListener() {
496                    @Override
497                    public void actionPerformed(ActionEvent e) {
498                        jButton_stack_add_actionPerformed(e);
499                    }
500                });
501        jButton_stack_sub.setText("Sub");
502        jButton_stack_sub.setHorizontalTextPosition(SwingConstants.CENTER);
503        jButton_stack_sub.setMargin(new Insets(2, 2, 2, 2));
504        jButton_stack_sub.setFont(new java.awt.Font("Dialog", 1, 18));
505        jButton_stack_sub.setBounds(new Rectangle(105, 63, 75, 44));
506        jButton_stack_sub
507                .addActionListener(new java.awt.event.ActionListener() {
508                    @Override
509                    public void actionPerformed(ActionEvent e) {
510                        jButton_stack_sub_actionPerformed(e);
511                    }
512                });
513        jButton_Clear.setText("Clear");
514        jButton_Clear.setMargin(new Insets(2, 2, 2, 2));
515        jButton_Clear.setHorizontalTextPosition(SwingConstants.CENTER);
516        jButton_Clear.setFont(new java.awt.Font("Dialog", 1, 18));
517        jButton_Clear.addActionListener(new java.awt.event.ActionListener() {
518            @Override
519            public void actionPerformed(ActionEvent e) {
520                jButton_Clear_actionPerformed(e);
521            }
522        });
523        jButton_Clear.setBounds(new Rectangle(105, 225, 77, 44));
524        this.getContentPane().add(jPanel1, BorderLayout.CENTER);
525        jPanel1.add(jPanel2, null);
526        jPanel1.add(jPanel3, null);
527        jPanel3.add(jTextArea_Stack_3, null);
528        jPanel3.add(jTextArea_Stack_2, null);
529        jPanel3.add(jTextArea_Stack_0, null);
530        jPanel3.add(jTextArea_Stack_1, null);
531        jPanel1.add(jPanel4, null);
532        jPanel4.add(jTextArea_Current, null);
533        jPanel1.add(jPanel5, null);
534        jPanel1.add(jPanel8, null);
535        jPanel5.add(jLabel6, null);
536        jPanel5.add(jLabel5, null);
537        jPanel5.add(jTextField1, null);
538        jPanel5.add(jTextField2, null);
539        jPanel8.add(jButton_int, null);
540        jPanel8.add(jButton_dif, null);
541        jPanel1.add(jPanel6, null);
542        jPanel6.add(jButton_exp, null);
543        jPanel6.add(jButton_8, null);
544        jPanel6.add(jButton_9, null);
545        jPanel6.add(jButton_4, null);
546        jPanel6.add(jButton_5, null);
547        jPanel6.add(jButton_6, null);
548        jPanel6.add(jButton_3, null);
549        jPanel6.add(jButton_0, null);
550        jPanel6.add(jButton_x, null);
551        jPanel6.add(jButton_div, null);
552        jPanel6.add(jButton_plus, null);
553        jPanel6.add(jButton_mul, null);
554        jPanel6.add(jButton_7, null);
555        jPanel6.add(jButton_2, null);
556        jPanel6.add(jButton_1, null);
557        jPanel6.add(jButton_minus, null);
558        jPanel1.add(jPanel7, null);
559        jPanel7.add(jButton_pop, null);
560        jPanel7.add(jButton_dup, null);
561        jPanel7.add(jButton_Enter, null);
562        jPanel7.add(jButton_swap, null);
563        jPanel7.add(jButton_stack_div, null);
564        jPanel7.add(jButton_stack_mul, null);
565        jPanel7.add(jButton_stack_add, null);
566        jPanel7.add(jButton_stack_sub, null);
567        jPanel7.add(jButton_Clear, null);
568
569        pg = new PolyGraph(this);
570        // pg.setBackground(Color.white);
571        jPanel2.add(pg, "Center");
572    }
573
574    /** Main method */
575    public static void main(String[] args) {
576        try {
577            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
578        } catch (Exception e) {
579            e.printStackTrace();
580        }
581
582        CalculatorFrame frame = new CalculatorFrame();
583
584        // Validate frames that have preset sizes
585        frame.validate();
586
587        // Center the window
588        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
589
590        frame.setSize(new Dimension(670, 730));
591        frame.setTitle("Polynomial Calculator");
592
593        Dimension frameSize = frame.getSize();
594
595        if (frameSize.height > screenSize.height) {
596            frameSize.height = screenSize.height;
597        }
598        if (frameSize.width > screenSize.width) {
599            frameSize.width = screenSize.width;
600        }
601        frame.setLocation((screenSize.width - frameSize.width) / 2,
602                (screenSize.height - frameSize.height) / 2);
603        frame.setVisible(true);
604    }
605
606    boolean startNewEq = false;
607
608    void appendToCurrentText(String textToAdd) {
609        if (startNewEq) {
610            jTextArea_Current.setText("");
611            startNewEq = false;
612        }
613        String currentText = jTextArea_Current.getText();
614        jTextArea_Current.setText(currentText + textToAdd);
615    }
616
617    void jButton_1_actionPerformed(ActionEvent e) {
618        appendToCurrentText("1");
619    }
620
621    void jButton_2_actionPerformed(ActionEvent e) {
622        appendToCurrentText("2");
623    }
624
625    void jButton_3_actionPerformed(ActionEvent e) {
626        appendToCurrentText("3");
627    }
628
629    void jButton_4_actionPerformed(ActionEvent e) {
630        appendToCurrentText("4");
631    }
632
633    void jButton_5_actionPerformed(ActionEvent e) {
634        appendToCurrentText("5");
635    }
636
637    void jButton_6_actionPerformed(ActionEvent e) {
638        appendToCurrentText("6");
639    }
640
641    void jButton_7_actionPerformed(ActionEvent e) {
642        appendToCurrentText("7");
643    }
644
645    void jButton_8_actionPerformed(ActionEvent e) {
646        appendToCurrentText("8");
647    }
648
649    void jButton_9_actionPerformed(ActionEvent e) {
650        appendToCurrentText("9");
651    }
652
653    void jButton_exp_actionPerformed(ActionEvent e) {
654        if (startNewEq == true) {
655            startNewEq = false;
656        }
657        appendToCurrentText("^");
658    }
659
660    void jButton_mul_actionPerformed(ActionEvent e) {
661        if (startNewEq == true) {
662            startNewEq = false;
663        }
664        appendToCurrentText("*");
665    }
666
667    void jButton_div_actionPerformed(ActionEvent e) {
668        if (startNewEq == true) {
669            startNewEq = false;
670        }
671        appendToCurrentText("/");
672    }
673
674    void jButton_plus_actionPerformed(ActionEvent e) {
675        if (startNewEq == true) {
676            startNewEq = false;
677        }
678        appendToCurrentText("+");
679    }
680
681    void jButton_minus_actionPerformed(ActionEvent e) {
682        if (startNewEq == true) {
683            startNewEq = false;
684        }
685        appendToCurrentText("-");
686    }
687
688    void jButton_0_actionPerformed(ActionEvent e) {
689        appendToCurrentText("0");
690    }
691
692    void jButton_x_actionPerformed(ActionEvent e) {
693        appendToCurrentText("x");
694    }
695
696    void jButton_swap_actionPerformed(ActionEvent e) {
697        try {
698            if (stack.size() > 1) {
699                stack.swap();
700            }
701            // Update displays (so that they display new stack contents).
702            updateStackDisplay();
703            updateGraph();
704        } catch (Exception e1) {
705            e1.printStackTrace();
706            return;
707        }
708    }
709
710    void jButton_pop_actionPerformed(ActionEvent e) {
711        try {
712            if (stack.size() > 0) {
713                RatPoly popped = stack.pop();
714                jTextArea_Current.setText(popped.toString());
715                startNewEq = true;
716            }
717            // Update displays (so that they display new stack contents).
718            updateStackDisplay();
719            updateGraph();
720        } catch (Exception e1) {
721            e1.printStackTrace();
722            return;
723        }
724    }
725
726    void jButton_dup_actionPerformed(ActionEvent e) {
727        try {
728            if (stack.size() > 0) {
729                stack.dup();
730            }
731            // Update displays (so that they display new stack contents).
732            updateStackDisplay();
733            updateGraph();
734        } catch (Exception e1) {
735            e1.printStackTrace();
736            return;
737        }
738    }
739
740    void jButton_Enter_actionPerformed(ActionEvent e) {
741        String currentText = jTextArea_Current.getText();
742        RatPoly parsedRatPoly;
743
744        startNewEq = true;
745        jTextArea_Current.setText("");
746
747        // Exception handler used here since some of this calls what will
748        // ultimately be student-written code.
749        try {
750            // Parse text to get new RatPoly and push it onto the stack.
751            parsedRatPoly = RatPoly.valueOf(currentText);
752            stack.push(parsedRatPoly);
753
754            // Update displays (so that they display new stack contents).
755            updateStackDisplay();
756            updateGraph();
757        } catch (Exception e1) {
758            // Provide more informative message here.
759            jTextArea_Current.setText(currentText);
760            e1.printStackTrace();
761            return;
762        }
763
764    }
765
766    void updateStackDisplay() {
767        // First, clear all the text areas for the stack display.
768        jTextArea_Stack_0.setText("");
769        jTextArea_Stack_1.setText("");
770        jTextArea_Stack_2.setText("");
771        jTextArea_Stack_3.setText("");
772
773        RatPoly currentRatPoly;
774        String tempString;
775
776        // Now fill in new information base on what's in stack.
777        // Note that size of stack must be checked.
778        if (stack.size() > 0) {
779            currentRatPoly = stack.getNthFromTop(0);
780            tempString = currentRatPoly.toString();
781            jTextArea_Stack_0.setText(tempString);
782        }
783
784        if (stack.size() > 1) {
785            currentRatPoly = stack.getNthFromTop(1);
786            tempString = currentRatPoly.toString();
787            jTextArea_Stack_1.setText(tempString);
788        }
789
790        if (stack.size() > 2) {
791            currentRatPoly = stack.getNthFromTop(2);
792            tempString = currentRatPoly.toString();
793            jTextArea_Stack_2.setText(tempString);
794        }
795
796        if (stack.size() > 3) {
797            currentRatPoly = stack.getNthFromTop(3);
798            tempString = currentRatPoly.toString();
799            jTextArea_Stack_3.setText(tempString);
800        }
801        // Consider abstracting this better! This would require
802        // putting the text areas into an array.
803
804    }
805
806    void updateGraph() {
807        pg.repaint();
808    }
809
810    void jTextField1_actionPerformed(ActionEvent e) {
811        updateGraph();
812    }
813
814    void jTextField2_actionPerformed(ActionEvent e) {
815        updateGraph();
816    }
817
818    void jButton_stack_mul_actionPerformed(ActionEvent e) {
819        try {
820            if (stack.size() > 1)
821                stack.mul();
822            // Update displays (so that they display new stack contents).
823            updateStackDisplay();
824            updateGraph();
825        } catch (Exception e1) {
826            e1.printStackTrace();
827            return;
828        }
829    }
830
831    void jButton_stack_div_actionPerformed(ActionEvent e) {
832        try {
833            if (stack.size() > 1)
834                stack.div();
835            // Update displays (so that they display new stack contents).
836            updateStackDisplay();
837            updateGraph();
838        } catch (Exception e1) {
839            e1.printStackTrace();
840            return;
841        }
842    }
843
844    void jButton_int_actionPerformed(ActionEvent e) {
845        try {
846            // System.out.println("Integrate Button Clicked");
847            stack.integrate();
848            updateStackDisplay();
849            updateGraph();
850        } catch (Exception e1) {
851            e1.printStackTrace();
852            return;
853        }
854    }
855
856    void jButton_dif_actionPerformed(ActionEvent e) {
857        try {
858            // System.out.println("Differentiate Button Clicked");
859            stack.differentiate();
860            updateStackDisplay();
861            updateGraph();
862        } catch (Exception e1) {
863            e1.printStackTrace();
864            return;
865        }
866    }
867
868    void jButton_stack_add_actionPerformed(ActionEvent e) {
869        try {
870            if (stack.size() > 1)
871                stack.add();
872            // Update displays (so that they display new stack contents).
873            updateStackDisplay();
874            updateGraph();
875        } catch (Exception e1) {
876            e1.printStackTrace();
877            return;
878        }
879    }
880
881    void jButton_stack_sub_actionPerformed(ActionEvent e) {
882        try {
883            if (stack.size() > 1)
884                stack.sub();
885            // Update displays (so that they display new stack contents).
886            updateStackDisplay();
887            updateGraph();
888        } catch (Exception e1) {
889            e1.printStackTrace();
890            return;
891        }
892    }
893
894    void jButton_Clear_actionPerformed(ActionEvent e) {
895        try {
896            if (stack.size() > 0)
897                stack.clear();
898            // Update displays (so that they display new stack contents).
899            updateStackDisplay();
900            updateGraph();
901        } catch (Exception e1) {
902            e1.printStackTrace();
903            return;
904        }
905    }
906
907}