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