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