PageRenderTime 169ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src/mpv5/ui/misc/Calc.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 1125 lines | 511 code | 156 blank | 458 comment | 122 complexity | 2f5a03824c7eab4e88f9d80221b4f1ff MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, GPL-3.0, GPL-2.0, AGPL-3.0, JSON, BSD-3-Clause
  1. /*
  2. * This file is part of YaBS.
  3. *
  4. * YaBS is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * YaBS is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with YaBS. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. package mpv5.ui.misc;
  18. /******************************************************************************
  19. *
  20. * Program Name: Java Calculator Applet
  21. * Author: Jason Elias
  22. * Last Modified: April 12, 2001
  23. * Purpose: Provide all of the basic functions of a calculator in
  24. * a GUI environment -- an applet. Also illustrates how
  25. * to multithread an applet with two different
  26. * implementations to illustrate it: one with a Thread
  27. * internally defined in the applet, and a Clock thread
  28. * defined outside of the applet.
  29. *
  30. * Notes: Feel free to use and improve this code, and I would love
  31. * to see any improvements that are made.
  32. *
  33. * Email: jasonelias@hotmail.com
  34. * Website: www.geocities.com/entity05
  35. *
  36. *****************************************************************************/
  37. import java.awt.BorderLayout;
  38. import java.awt.Color;
  39. import java.awt.Event;
  40. import java.awt.Font;
  41. import java.awt.event.ActionEvent;
  42. import java.awt.event.ActionListener;
  43. import java.awt.event.KeyEvent;
  44. import java.awt.event.KeyListener;
  45. import javax.swing.JButton;
  46. import javax.swing.JFrame;
  47. import javax.swing.JLabel;
  48. import javax.swing.border.EtchedBorder;
  49. public class Calc extends JLabel implements ActionListener,KeyListener {
  50. private static final long serialVersionUID = 1L;
  51. int Counter; //counts the number of digits entered
  52. double Result; //the answer displayed, as well as the second
  53. //operator taken for an operation
  54. double Operand; //the first number entered for an operation
  55. double Mem; //the variable which holds whatever value the user
  56. //wants kept in "memory"
  57. boolean DecimalFlag; //the 'flag' that will signify whether or not the
  58. //decimal button has been pressed
  59. boolean SignFlag; //the 'flag' that will signify whether or not the
  60. //plus/minus button has been pressed
  61. boolean OperatorKey; //the 'flag' that will signify whether or not any
  62. //operator button has been pressed
  63. boolean FunctionKey; //the 'flag' that will signify whether or not any f
  64. //function button has been pressed
  65. int Operator; //an integer value to indicate which operator
  66. //button was pressed
  67. char currchar; //a character to hold the value of the key most
  68. //recently pressed
  69. String GrStatus; //String to hold the status of various graphic
  70. //operations of the program
  71. String Status; //String to hold the status of various parts
  72. //This label will display all error messages
  73. JLabel DisplError = new JLabel(" ", JLabel.CENTER);
  74. /* This label is just to the left of the Display Label lcdDisplay, and will
  75. * indicate whether or not a value is being held in the calculator's "memory"
  76. */
  77. JLabel LabelMem = new JLabel(" ", JLabel.RIGHT);
  78. /* This is the Display Label, which is declared as a label so the user will not
  79. * be able to enter any text into it to possibly crash the calculator
  80. */
  81. JLabel lcdDisplay = new JLabel("0", JLabel.RIGHT);
  82. // This is the declaration of all numeric buttons to be used in the applet
  83. JButton button1 = new JButton("1");
  84. JButton button2 = new JButton("2");
  85. JButton button3 = new JButton("3");
  86. JButton button4 = new JButton("4");
  87. JButton button5 = new JButton("5");
  88. JButton button6 = new JButton("6");
  89. JButton button7 = new JButton("7");
  90. JButton button8 = new JButton("8");
  91. JButton button9 = new JButton("9");
  92. JButton button0 = new JButton("0");
  93. /* This is the declaration of all operation buttons that are used in applet.
  94. * MPlus, MClear and MR are all memory functions.
  95. */
  96. JButton buttonMinus = new JButton("-");
  97. JButton buttonMultiply = new JButton("x");
  98. JButton buttonPlus = new JButton("+");
  99. JButton buttonEquals = new JButton("=");
  100. JButton buttonDivide = new JButton("/");
  101. JButton buttonClear = new JButton("C");
  102. JButton buttonDecimal = new JButton(".");
  103. JButton buttonNegative = new JButton("+/-");
  104. JButton buttonMPlus = new JButton("M+");
  105. JButton buttonMClear = new JButton("MC");
  106. JButton buttonMR = new JButton("MR");
  107. JButton buttonPercent = new JButton("%");
  108. JButton buttonOneOverX = new JButton("1/X");
  109. JButton buttonSqr = new JButton("xÂ?");
  110. JButton buttonSqrRoot = new JButton("sqrt");
  111. public static void main(String[] args) {
  112. JFrame j = new JFrame();
  113. j.setLayout(new BorderLayout());
  114. j.add(new Calc(), BorderLayout.CENTER);
  115. j.setSize(340, 300);
  116. j.pack();
  117. j.setVisible(true);
  118. }
  119. /* *This the only method that is called explicitly -- every other method is
  120. * called depending on the user's actions.
  121. */
  122. public Calc() {
  123. //Allows for configuring a layout with the restraints of a grid or
  124. //something similar
  125. setLayout(null);
  126. //This will resize the applet to the width and height provided
  127. setSize(340, 300);
  128. //
  129. // //This sets the default font to Helvetica, plain, size 12
  130. // setFont(new Font("Helvetica", Font.PLAIN, 12));
  131. /* Display Panel, which appears at the top of the screen. The label is
  132. * placed and sized with the setBounds(x,y,width,height) method, and the
  133. * font, foreground color and background color are all set. Then the
  134. * label is added to the layout of the applet.
  135. */
  136. lcdDisplay.setBounds(2, 15, 308, 30);
  137. lcdDisplay.setFont(new Font("Helvetica", Font.PLAIN, 20));
  138. lcdDisplay.setForeground(Color.black);
  139. lcdDisplay.setBackground(Color.WHITE);
  140. lcdDisplay.setBorder(new EtchedBorder());
  141. add(lcdDisplay);
  142. /* Memory Panel, which appears just to the right of the Display Panel.
  143. * The label is placed and sized with the setBounds(x,y,width,height)
  144. * method, and the font, foreground color and background color are all
  145. * set. Then the label is added to the layout of the applet.
  146. */
  147. LabelMem.setBounds(310, 15, 20, 30);
  148. LabelMem.setFont(new Font("Helvetica", Font.PLAIN, 16));
  149. LabelMem.setForeground(new Color(65280));
  150. LabelMem.setBackground(Color.LIGHT_GRAY);
  151. LabelMem.setBorder(new EtchedBorder());
  152. add(LabelMem);
  153. /* The following declarations initialize all of the Numberic Buttons
  154. * that will displayed on the applet.
  155. *
  156. * First, an ActionListener (which will capture events) is added to the
  157. * button, sending the applet as the argument
  158. *
  159. * Second, the button is placed and sized with the setBounds(x,y,width,
  160. * height) method.
  161. *
  162. * Then the default font is set for the button, and the button is added
  163. * to the layout of the applet.
  164. */
  165. button1.addActionListener(this);
  166. button1.setBounds(2, 65, 60, 34);
  167. button1.addKeyListener(this);
  168. add(button1);
  169. button2.addActionListener(this);
  170. button2.setBounds(66, 65, 60, 34);
  171. button2.addKeyListener(this);
  172. add(button2);
  173. button3.addActionListener(this);
  174. button3.setBounds(130, 65, 60, 34);
  175. button3.addKeyListener(this);
  176. add(button3);
  177. button4.addActionListener(this);
  178. button4.setBounds(2, 104, 60, 34);
  179. button4.addKeyListener(this);
  180. add(button4);
  181. button5.addActionListener(this);
  182. button5.setBounds(66, 104, 60, 34);
  183. button5.addKeyListener(this);
  184. add(button5);
  185. button6.addActionListener(this);
  186. button6.setBounds(130, 104, 60, 34);
  187. button6.addKeyListener(this);
  188. add(button6);
  189. button7.addActionListener(this);
  190. button7.setBounds(2, 142, 60, 34);
  191. button7.addKeyListener(this);
  192. add(button7);
  193. button8.addActionListener(this);
  194. button8.setBounds(66, 142, 60, 34);
  195. button8.addKeyListener(this);
  196. add(button8);
  197. button9.addActionListener(this);
  198. button9.setBounds(130, 142, 60, 34);
  199. button9.addKeyListener(this);
  200. add(button9);
  201. button0.addActionListener(this);
  202. button0.setBounds(2, 180, 60, 34);
  203. button0.addKeyListener(this);
  204. add(button0);
  205. /* The following declaration and initialization statements set up all of
  206. * the operation buttons on the applet.
  207. *
  208. * First, an ActionListener is added to each button with the "this"
  209. * keyword indicating that "this object" (the applet) is being referred to.
  210. *
  211. * Then the button is "reshaped" to a certain coordinate (x and y) as
  212. * well as new dimensions (width, height).
  213. *
  214. * Then the font is set for that button, and the arguments include:
  215. * - String value representing the name of the font
  216. * - a member of Font called BOLD which bolds the text
  217. * - the size of the font as an integer
  218. *
  219. * Lastly, the button is added to the applet
  220. */
  221. buttonDecimal.addActionListener(this);
  222. buttonDecimal.setBounds(66, 180, 60, 34);
  223. buttonDecimal.addKeyListener(this);
  224. add(buttonDecimal);
  225. buttonNegative.addActionListener(this);
  226. buttonNegative.setBounds(130, 180, 60, 34);
  227. buttonNegative.addKeyListener(this);
  228. add(buttonNegative);
  229. buttonMinus.addActionListener(this);
  230. buttonMinus.setBounds(194, 104, 60, 34);
  231. buttonMinus.addKeyListener(this);
  232. add(buttonMinus);
  233. buttonMultiply.addActionListener(this);
  234. buttonMultiply.setBounds(194, 142, 60, 34);
  235. buttonMultiply.addKeyListener(this);
  236. add(buttonMultiply);
  237. buttonPlus.addActionListener(this);
  238. buttonPlus.setBounds(194, 65, 60, 34);
  239. buttonPlus.addKeyListener(this);
  240. add(buttonPlus);
  241. buttonPercent.addActionListener(this);
  242. buttonPercent.setBounds(2, 230, 60, 34);
  243. buttonPercent.addKeyListener(this);
  244. // add(buttonPercent);
  245. buttonOneOverX.addActionListener(this);
  246. buttonOneOverX.setBounds(66, 230, 60, 34);
  247. buttonOneOverX.addKeyListener(this);
  248. add(buttonOneOverX);
  249. buttonSqr.addActionListener(this);
  250. buttonSqr.setBounds(130, 230, 60, 34);
  251. buttonSqr.addKeyListener(this);
  252. add(buttonSqr);
  253. buttonSqrRoot.addActionListener(this);
  254. buttonSqrRoot.setBounds(194, 230, 60, 34);
  255. buttonSqrRoot.addKeyListener(this);
  256. add(buttonSqrRoot);
  257. buttonEquals.addActionListener(this);
  258. buttonEquals.setBounds(269, 230, 60, 34);
  259. buttonEquals.addKeyListener(this);
  260. add(buttonEquals);
  261. buttonDivide.addActionListener(this);
  262. buttonDivide.setBounds(194, 180, 60, 34);
  263. buttonDivide.addKeyListener(this);
  264. add(buttonDivide);
  265. buttonClear.addActionListener(this);
  266. buttonClear.setBounds(269, 65, 60, 34);
  267. buttonClear.addKeyListener(this);
  268. buttonClear.setForeground(new Color(16711680));
  269. buttonClear.setBackground(new Color(12632256));
  270. add(buttonClear);
  271. buttonMPlus.addActionListener(this);
  272. buttonMPlus.setBounds(269, 104, 60, 34);
  273. buttonMPlus.addKeyListener(this);
  274. buttonMPlus.setForeground(Color.blue);
  275. add(buttonMPlus);
  276. buttonMClear.addActionListener(this);
  277. buttonMClear.setBounds(269, 142, 60, 34);
  278. buttonMClear.addKeyListener(this);
  279. buttonMClear.setForeground(Color.blue);
  280. add(buttonMClear);
  281. buttonMR.addActionListener(this);
  282. buttonMR.setBounds(269, 180, 60, 34);
  283. buttonMR.addKeyListener(this);
  284. buttonMR.setForeground(Color.blue);
  285. add(buttonMR);
  286. /* This is the initialization of the DisplayError label. It is placed and
  287. * sized with the setBounds(x,y,width,height) method, and then the font,
  288. * foreground color and background color are all set. Finally, the label
  289. * is added to the layout of the applet.
  290. */
  291. DisplError.setBounds(2, 278, 329, 15);
  292. DisplError.setFont(new Font("Dialog", Font.BOLD, 12));
  293. DisplError.setForeground(new Color(16711680));
  294. DisplError.setBackground(new Color(0));
  295. add(DisplError);
  296. Clicked_Clear(); //calls the Clicked_Clear method (C button)
  297. } //end of calc init method
  298. /* The following integer values are being set as "final" because
  299. * they are going to be used for determining what button has been
  300. * pushed
  301. */
  302. public final static int OpMinus = 11, OpMultiply = 12, OpPlus = 13, OpDivide = 15, OpMPlus = 19, OpMClear = 20, OpMR = 21;
  303. /* This method is called whenever anything needs to be displayed
  304. * in the error message field at the bottom of the calculator,
  305. * accepting a String as an argument
  306. */
  307. void DisplayError(String err_msg) {
  308. /* calls the setText method of the Label DisplError, sending
  309. * whatever string it received initially
  310. */
  311. DisplError.setText(err_msg);
  312. }
  313. /* This method is called whenever a numeric button (0-9) is pushed. */
  314. public void NumericButton(int i) {
  315. DisplayError(" "); //Clears the error message field
  316. /* Declares a String called Display that will initialize to whatever
  317. * is currently displayed in the lcdDisplay of the calculator
  318. */
  319. String Display = lcdDisplay.getText();
  320. /* Checks if an operator key has just been pressed, and if it has,
  321. * then the limit of 20 digits will be reset for the user so that
  322. * they can enter in up to 20 new numbers
  323. */
  324. if (OperatorKey == true) {
  325. Counter = 0;
  326. }
  327. Counter = Counter + 1; //increments the counter
  328. /* This is a condition to see if the number currently displayed is zero OR
  329. * an operator key other than +, -, *, or / has been pressed.
  330. */
  331. if ((Display.equals("0")) || (Status.equals("FIRST"))) {
  332. Display = ""; //Do not display any new info
  333. }
  334. if (Counter < 21) //if more than 20 numbers are entered
  335. {
  336. //The number just entered is appended to the string currently displayed
  337. Display = Display + String.valueOf(i);
  338. } else {
  339. //call the DisplayError method and send it an error message string
  340. DisplayError("Digit Limit of 20 Digits Reached");
  341. }
  342. lcdDisplay.setText(Display); //sets the text of the lcdDisplay
  343. //Label
  344. Status = "VALID"; //sets the Status string to valid
  345. OperatorKey = false; //no operator key was pressed
  346. FunctionKey = false; //no function key was pressed
  347. }
  348. /* This method is called whenever an operator button is pressed, and is
  349. * sent an integer value representing the button pressed.
  350. */
  351. public void OperatorButton(int i) {
  352. DisplayError(" "); //Clears the error message field
  353. /* Creates a new Double object with the specific purpose of retaining
  354. * the string currently on the lcdDisplay label, and then immediately
  355. * converts that string into a double-precision real number and then
  356. * gives that number to the variable Result.
  357. */
  358. Result = (new Double(lcdDisplay.getText())).doubleValue();
  359. //If no operator key has been pressed OR a function has been pressed
  360. if ((OperatorKey == false) || (FunctionKey = true)) {
  361. switch (Operator) //depending on the operation performed
  362. {
  363. /* if the user pressed the addition button, add the two numbers
  364. * and put them in double Result
  365. */
  366. case OpPlus:
  367. Result = Operand + Result;
  368. break;
  369. /* if the user pressed the subtraction button, subtract the two
  370. * numbers and put them in double Result
  371. */
  372. case OpMinus:
  373. Result = Operand - Result;
  374. break;
  375. /* if the user pressed the multiplication button, multiply
  376. * the two numbers and put them in double Result
  377. */
  378. case OpMultiply:
  379. Result = Result * Operand;
  380. break;
  381. /* if the user pressed the division button, check to see if
  382. * the second number entered is zero to avoid a divide-by-zero
  383. * exception
  384. */
  385. case OpDivide:
  386. if (Result == 0) {
  387. //set the Status string to indicate an
  388. //an error
  389. Status = "ERROR";
  390. //display the word "ERROR" on the
  391. //lcdDisplay label
  392. lcdDisplay.setText("ERROR");
  393. /* call the DisplayError method and
  394. * send it a string indicating an error
  395. * has occured and of what type
  396. */
  397. DisplayError("ERROR: Division by Zero");
  398. } else {
  399. //divide the two numbers and put the
  400. //answer in double Result
  401. Result = Operand / Result;
  402. }
  403. }
  404. //if after breaking from the switch the Status string is not set
  405. //to "ERROR"
  406. if (!Status.equals("ERROR")) {
  407. Status = "FIRST"; /* set the Status string to "FIRST" to
  408. * indicate that a simple operation was
  409. * not performed
  410. */
  411. Operand = Result; //Operand holds the value of Result
  412. Operator = i; /* the integer value representing the
  413. * operation being performed is stored
  414. * in the integer Operator
  415. */
  416. //The lcdDisplay label has the value of double Result
  417. //displayed
  418. lcdDisplay.setText(String.valueOf(Result));
  419. //The boolean decimal flag is set false, indicating that the
  420. //decimal button has not been pressed
  421. DecimalFlag = false;
  422. //The boolean sign flag is set false, indicating that the sign
  423. //button has not been pressed
  424. SignFlag = false;
  425. //The boolean OperatorKey is set true, indicating that a simple
  426. //operation has been performed
  427. OperatorKey = true;
  428. //The boolean FunctionKey is set false, indicating that a
  429. //function key has not been pressed
  430. FunctionKey = false;
  431. DisplayError(" "); //Clears the error message field
  432. }
  433. }
  434. } //end of OperatorButton method
  435. /* This is a method that is called whenever the decimal button is
  436. * pressed.
  437. */
  438. public void DecimalButton() {
  439. DisplayError(" "); //Clears the error message field
  440. /* Declares a String called Display that will initialize to whatever
  441. * is currently displayed in the lcdDisplay of the calculator
  442. */
  443. String Display = lcdDisplay.getText();
  444. //if a simple operation was performed successfully
  445. if (Status .equals("FIRST")) {
  446. Display = "0"; //set Display string to character 0
  447. }
  448. //If the decimal button has not already been pressed
  449. if (!DecimalFlag) {
  450. //appends a decimal to the string Display
  451. Display = Display + ".";
  452. } else {
  453. /* calls the DisplayError method, sending a string
  454. * indicating that the number already has a decimal
  455. */
  456. DisplayError("Number already has a Decimal Point");
  457. }
  458. /* calls the setText method of the Label lcdDisplay and
  459. * sends it the string Display
  460. */
  461. lcdDisplay.setText(Display);
  462. DecimalFlag = true; //the decimal key has been pressed
  463. Status = "VALID"; /* Status string indicates a valid
  464. * operation has been performed
  465. */
  466. OperatorKey = false; //no operator key has been pressed
  467. } //end of the DecimalButton method
  468. /* This method is called whenever the percent button is pressed
  469. */
  470. void PercentButton() {
  471. DisplayError(" "); //clears the error message field
  472. /* Declares a String called Display that will initialize to whatever
  473. * is currently displayed in the lcdDisplay of the calculator
  474. */
  475. String Display = lcdDisplay.getText();
  476. /* if the Status string is not set to "FIRST" OR the Display string
  477. * does not currently hold the value "0"
  478. */
  479. if ((!Status.equals("FIRST")) || (!Display.equals("0"))) {
  480. /* Creates a new Double object with the specific purpose of retaining
  481. * the string currently on the lcdDisplay label, and then immediately
  482. * converts that string into a double-precision real number and then
  483. * gives that number to the variable Result.
  484. */
  485. Result = (new Double(lcdDisplay.getText())).doubleValue();
  486. //divide the double Result by 100, getting the percentage
  487. Result = Result / 100;
  488. /* call the setText method of Label lcdDisplay and send it the string
  489. * that represents the value in Result
  490. */
  491. lcdDisplay.setText(String.valueOf(Result));
  492. Status = "FIRST"; //
  493. OperatorKey = true; //an operator key has been pressed
  494. FunctionKey = true; //a function key has been pressed
  495. }
  496. } //end of the PercentButton method
  497. /* This method is called first when the calculator is initialized
  498. * with the init() method, and is called every time the "C" button
  499. * is pressed
  500. */
  501. void Clicked_Clear() {
  502. Counter = 0; //sets the counter to zero
  503. Status = "FIRST"; //sets Status to FIRST
  504. Operand = 0; //sets Operand to zero
  505. Result = 0; //sets Result to zero
  506. Operator = 0; //sets Operator integer to zero
  507. DecimalFlag = false; //decimal button has not been
  508. //pressed
  509. SignFlag = false; //sign button has not been pressed
  510. OperatorKey = false; //no operator button has been
  511. //pressed
  512. FunctionKey = false; //no function button has been
  513. //pressed
  514. /* calls the setText method of Label lcdDisplay and sends
  515. * it the character "0"
  516. */
  517. lcdDisplay.setText("0");
  518. DisplayError(" "); //clears the error message field
  519. }
  520. /* This method is called whenever the sign button is pressed */
  521. void PlusMinusButton() {
  522. DisplayError(" "); //clears the error message field
  523. /* Declares a String called Display that will initialize to whatever
  524. * is currently displayed in the lcdDisplay of the calculator
  525. */
  526. String Display = lcdDisplay.getText();
  527. /* if Status is not set to FIRST and the Display string does not
  528. * hold the value "0"
  529. */
  530. if ((!Status.equals("FIRST")) || (!Display.equals("0"))) {
  531. /* Creates a new Double object with the specific purpose of retaining
  532. * the string currently on the lcdDisplay label, and then immediately
  533. * converts that string into a double-precision real number and then
  534. * gives that number to the variable Result.
  535. */
  536. Result = (new Double(lcdDisplay.getText())).doubleValue();
  537. //sets the double Result to it's negative value
  538. Result = -Result;
  539. /* call the setText method of Label lcdDisplay and send it the string
  540. * that represents the value in Result
  541. */
  542. lcdDisplay.setText(String.valueOf(Result));
  543. Status = "VALID"; //sets Status string to VALID
  544. SignFlag = true; //the sign button has been pressed
  545. DecimalFlag = true; //a decimal has appeared
  546. }
  547. } //end of the PlusMinusButton method
  548. /* This method is called whenever the square button is pressed */
  549. void SqrButton() {
  550. DisplayError(" "); //clears the error message field
  551. /* Declares a String called Display that will initialize to whatever
  552. * is currently displayed in the lcdDisplay of the calculator
  553. */
  554. String Display = lcdDisplay.getText();
  555. /* if Status is not set to FIRST and the Display string does not
  556. * hold the value "0"
  557. */
  558. if ((!Status.equals("FIRST")) || (!Display.equals("0")) ){
  559. /* Creates a new Double object with the specific purpose of retaining
  560. * the string currently on the lcdDisplay label, and then immediately
  561. * converts that string into a double-precision real number and then
  562. * gives that number to the variable Result.
  563. */
  564. Result = (new Double(lcdDisplay.getText())).doubleValue();
  565. /* multiply the double Result by itself, effectively squaring
  566. * the number */
  567. Result = Result * Result;
  568. /* call the setText method of Label lcdDisplay and send it the string
  569. * that represents the value in Result
  570. */
  571. lcdDisplay.setText(String.valueOf(Result));
  572. Status = "FIRST"; //indicates this is the first time
  573. //this button has been pressed
  574. OperatorKey = true; //an operator button has been pressed
  575. FunctionKey = true; //a function button has been pressed
  576. }
  577. } //end of the SqrButton method
  578. /* This method is called whenever the square button is pressed */
  579. void SqrRootButton() {
  580. DisplayError(" "); //clears the error message field
  581. /* Declares a String called Display that will initialize to whatever
  582. * is currently displayed in the lcdDisplay of the calculator
  583. */
  584. String Display = lcdDisplay.getText();
  585. /* if Status is not set to FIRST and the Display string does not
  586. * hold the value "0"
  587. */
  588. if ((!Status.equals("FIRST")) || (!Display.equals( "0"))) {
  589. /* Creates a new Double object with the specific purpose of retaining
  590. * the string currently on the lcdDisplay label, and then immediately
  591. * converts that string into a double-precision real number and then
  592. * gives that number to the variable Result.
  593. */
  594. Result = (new Double(lcdDisplay.getText())).doubleValue();
  595. /* Makes a call to the Math class method "sqrt", which produced the
  596. * square root and stores the value in Result
  597. */
  598. Result = Math.sqrt(Result);
  599. /* call the setText method of Label lcdDisplay and send it the string
  600. * that represents the value in Result
  601. */
  602. lcdDisplay.setText(String.valueOf(Result));
  603. Status = "FIRST"; //indicates this is the first time
  604. //this button has been pressed
  605. OperatorKey = true; //an operator button has been pressed
  606. FunctionKey = true; //a function button has been pressed
  607. }
  608. } //end of the SqrRootButton method
  609. /* This method is called whenever the OneOverXButton is pressed */
  610. void OneOverXButton() {
  611. DisplayError(" "); //clears the error message field
  612. /* Declares a String called Display that will initialize to whatever
  613. * is currently displayed in the lcdDisplay of the calculator
  614. */
  615. String Display = lcdDisplay.getText();
  616. /* if Status is not set to FIRST and the Display string does not
  617. * hold the value "0"
  618. */
  619. if ((!Status.equals( "FIRST")) || (!Display .equals("0"))) {
  620. /* Creates a new Double object with the specific purpose of retaining
  621. * the string currently on the lcdDisplay label, and then immediately
  622. * converts that string into a double-precision real number and then
  623. * gives that number to the variable Result.
  624. */
  625. Result = (new Double(lcdDisplay.getText())).doubleValue();
  626. //divides one by the double Result
  627. Result = 1 / Result;
  628. /* call the setText method of Label lcdDisplay and send it the string
  629. * that represents the value in Result
  630. */
  631. lcdDisplay.setText(String.valueOf(Result));
  632. Status = "FIRST"; //indicates that this is the first time
  633. //this button has been pressed
  634. OperatorKey = true; //an operator key has been pressed
  635. FunctionKey = true; //a function key has been pressed
  636. }
  637. } //end of the OneOverXButton method
  638. /* This method is called whenever one of the three memory buttons is pressed,
  639. * accepting an integer representing the button pressed as an argument
  640. */
  641. void MemoryButton(int i) {
  642. DisplayError(" "); //clears the error message field
  643. /* Declares a String called Display that will initialize to whatever
  644. * is currently displayed in the lcdDisplay of the calculator
  645. */
  646. String Display = lcdDisplay.getText();
  647. //depending on the value sent representing the buttons
  648. switch (i) {
  649. /* if the M+ button is pressed, check if the Display string has the value
  650. * "0." OR just "0", but along with the second condition check if double
  651. * Mem holds the value zero
  652. */
  653. case OpMPlus:
  654. if (((Display.equals("0.")) || (Display.equals("0"))) && (Mem == 0)) {
  655. //clears the LabelMem label
  656. LabelMem.setText(" ");
  657. } else {
  658. /* Creates a new double variable called temp, which accepts the
  659. * double value of a new Double object retaining the string
  660. * currently on the lcdDisplay label, and then immediately
  661. * converts that string into a double-precision real number
  662. */
  663. double temp = (new Double(lcdDisplay.getText())).doubleValue();
  664. //set the Mem variable with that the sum of what's currently in
  665. //Mem and the temp variable
  666. Mem = Mem + temp;
  667. /* calls the setText method of the Label LabelMem, sending
  668. * it the character "M"
  669. */
  670. LabelMem.setText("M");
  671. }
  672. break;
  673. /* if the MR button is pressed, call the setText method of Label lcdDisplay,
  674. * sending it the string that represents what the value of Mem is
  675. */
  676. case OpMR:
  677. lcdDisplay.setText(String.valueOf(Mem));
  678. break;
  679. /* if the MC button is pressed, set the Mem variable to zero */
  680. case OpMClear:
  681. Mem = 0;
  682. //clears the LableMem field
  683. LabelMem.setText(" ");
  684. break;
  685. }
  686. Status = "FIRST"; //indicates that this is first time
  687. //button has been pressed
  688. OperatorKey = true; //an operator button has been pressed
  689. //if the Mem variable has the value zero
  690. if (Mem == 0) {
  691. LabelMem.setText(" "); //clear the LabelMem field
  692. }
  693. } //end of the MemoryButton method
  694. /* This was left commented out so that a shell would be ready if needed
  695. public void paint(Graphics g)
  696. {
  697. }*/
  698. /***************************************************************************
  699. *
  700. * This is the overridden update method for this applet -- update is used to
  701. * eliminate the flicker that happens with repainting an image from scratch.
  702. *
  703. **************************************************************************/
  704. // public void update(Graphics g) {
  705. // /* The format of the coordinates below is:
  706. // * x position on the applet
  707. // * y position on the applet
  708. // * width of the component
  709. // * height of the component
  710. // */
  711. //
  712. // //Sets the color to white and draw a rectangle around the applet
  713. // g.setColor(Color.white);
  714. // g.drawRect(0, 0, 420, 368);
  715. //
  716. // //sets the color to black and draws lines down the east and south sides
  717. // //of the applet to make it appear three-dimensional
  718. // g.setColor(Color.black);
  719. // g.drawLine(419, 0, 419, 367);
  720. // g.drawLine(0, 367, 419, 367);
  721. //
  722. // //sets the color to gray and draws lines right next to the black lines
  723. // //to enhance the three-dimensional effect
  724. // g.setColor(Color.gray);
  725. // g.drawLine(418, 1, 418, 366);
  726. // g.drawLine(1, 366, 418, 366);
  727. //
  728. // //sets the color to gray and draws a rectangle around the lcdDisplay
  729. // //label to give it some depth
  730. // g.setColor(Color.gray);
  731. // g.drawRect(41, 14, 329, 31);
  732. //
  733. // //sets the color to yellow and draws a rectangle around the DisplError
  734. // //label to give it some importance and help attract the eye to errors
  735. // //displayed there
  736. // g.setColor(Color.yellow);
  737. // g.drawRect(41, 277, 330, 16);
  738. //
  739. //
  740. // }
  741. /************************************************************************
  742. /*
  743. /* This method is called whenever an action is performed on the applet,
  744. /* specifically whenever one of the buttons on the applet is pressed
  745. /*
  746. * /***********************************************************************
  747. * @param event
  748. */
  749. public void actionPerformed(ActionEvent event) {
  750. /* Declares an object named "src" to represent the event.getSource()
  751. * method. This shortens the code that follows.
  752. */
  753. Object src = event.getSource();
  754. /* Checks to see if the source of the event which src is representing
  755. * is a Button and can behave like a Button
  756. */
  757. if (src instanceof JButton) {
  758. /* Checks to see if Status string holds the value ERROR OR if the
  759. * source of the event is the buttonClear ("C") button
  760. */
  761. if ((!Status.equals( "ERROR")) || (src.equals(buttonClear))) {
  762. /* The following conditions check for numeric buttons that have
  763. * been pressed, and then called the method NumericButton and
  764. * send it an integer value depending on the value of the button
  765. * pressed
  766. */
  767. if (src == button1) {
  768. NumericButton(1);
  769. }
  770. if (src == button2) {
  771. NumericButton(2);
  772. }
  773. if (src == button3) {
  774. NumericButton(3);
  775. }
  776. if (src == button4) {
  777. NumericButton(4);
  778. }
  779. if (src == button5) {
  780. NumericButton(5);
  781. }
  782. if (src == button6) {
  783. NumericButton(6);
  784. }
  785. if (src == button7) {
  786. NumericButton(7);
  787. }
  788. if (src == button8) {
  789. NumericButton(8);
  790. }
  791. if (src == button9) {
  792. NumericButton(9);
  793. }
  794. if (src == button0) {
  795. NumericButton(0);
  796. }
  797. /* The following conditions check for operator buttons that have
  798. * been pressed, and then call the OperatorButton method, sending
  799. * the corresponding integer value
  800. */
  801. if (src == buttonMinus) {
  802. OperatorButton(11);
  803. }
  804. if (src == buttonMultiply) {
  805. OperatorButton(12);
  806. }
  807. if (src == buttonPlus) {
  808. OperatorButton(13);
  809. }
  810. if (src == buttonEquals) {
  811. OperatorButton(14);
  812. }
  813. if (src == buttonDivide) {
  814. OperatorButton(15);
  815. }
  816. /* This condition checks if the decimal button has been pressed,
  817. * and then calls the DecimalButton method
  818. */
  819. if (src == buttonDecimal) {
  820. DecimalButton();
  821. }
  822. /* This condition checks if the percent button has been pressed,
  823. * and then calls the PercentButton method
  824. */
  825. if (src == buttonPercent) {
  826. PercentButton();
  827. }
  828. /* This condition checks if the clear button has been pressed,
  829. * and then calls the Clicked_Clear method
  830. */
  831. if (src == buttonClear) {
  832. Clicked_Clear();
  833. }
  834. /* This condition checks if the Plus Minus Button has been
  835. * pressed, and then calls the PlusMinusButton method
  836. */
  837. if (src == buttonNegative) {
  838. PlusMinusButton();
  839. }
  840. /* This condition checks if the Square Button has been pressed,
  841. * and then calls the SqrButton method
  842. */
  843. if (src == buttonSqr) {
  844. SqrButton();
  845. }
  846. /* This condition checks if the Square Root Button has been
  847. * pressed, and then calls the SqrRootButton method
  848. */
  849. if (src == buttonSqrRoot) {
  850. SqrRootButton();
  851. }
  852. /* This condition checks if the One over X Button has been
  853. * pressed, and then calls the OneOverXButton method
  854. */
  855. if (src == buttonOneOverX) {
  856. OneOverXButton();
  857. }
  858. /* These conditions checks to see if any of the Memory Buttons
  859. * have been pressed, and then calls the MemoryButton method and
  860. * sends them the corresponding integer values
  861. */
  862. if (src == buttonMPlus) {
  863. MemoryButton(19);
  864. }
  865. if (src == buttonMClear) {
  866. MemoryButton(20);
  867. }
  868. if (src == buttonMR) {
  869. MemoryButton(21);
  870. }
  871. }
  872. }
  873. } //end of actionPerformed method
  874. /****************************************************************************
  875. *
  876. * The following method handles all of the key events that occurs when a user
  877. * enters values from the keyboard. It accepts an Event object and an integer
  878. * representing the key pressed as arguments.
  879. *
  880. * It should be noted that in order to accept keys entered to the lcdDisplay,
  881. * the applet must be in focus.
  882. *
  883. **************************************************************************
  884. * @param evt
  885. * @param key
  886. */
  887. public void keyD(KeyEvent evt, char key) {
  888. //assigns key value to currchar through typecasting key to a character
  889. currchar = key;
  890. /* The following conditions check to see if a number key has been pressed
  891. * and calls the NumericButton method, sending it the appropriate integer
  892. * as an argument
  893. */
  894. if (currchar == '0') {
  895. NumericButton(0);
  896. }
  897. if (currchar == '1') {
  898. NumericButton(1);
  899. }
  900. if (currchar == '2') {
  901. NumericButton(2);
  902. }
  903. if (currchar == '3') {
  904. NumericButton(3);
  905. }
  906. if (currchar == '4') {
  907. NumericButton(4);
  908. }
  909. if (currchar == '5') {
  910. NumericButton(5);
  911. }
  912. if (currchar == '6') {
  913. NumericButton(6);
  914. }
  915. if (currchar == '7') {
  916. NumericButton(7);
  917. }
  918. if (currchar == '8') {
  919. NumericButton(8);
  920. }
  921. if (currchar == '9') {
  922. NumericButton(9);
  923. }
  924. /* The following conditions check to see if any of the operator keys
  925. * which are on the number pad have been pressed. It then calls the
  926. * OperatorButton method, sending it the appropriate integer value as
  927. * an argument.
  928. *
  929. * It should be noted that the main functions checked for are those on
  930. * the number pad, but if they are pressed at other occurences on the
  931. * keyboard, they are still accepted.
  932. */
  933. if (currchar == '/') {
  934. OperatorButton(15);
  935. }
  936. if (currchar == '*') {
  937. OperatorButton(12);
  938. }
  939. if (currchar == '-') {
  940. OperatorButton(11);
  941. }
  942. if (currchar == '+') {
  943. OperatorButton(13);
  944. }
  945. if (currchar == '=') {
  946. OperatorButton(14);
  947. }
  948. //This condition is different from the others because it tests for the
  949. //Enter key being pressed, and this is pre-built into the Event evt
  950. //object
  951. if (currchar == Event.ENTER) {
  952. OperatorButton(14);
  953. }
  954. /* This checks for the decimal key being pressed, either on the number
  955. * pad or the period key, and calls the DecimalButton method.
  956. */
  957. if (currchar == '.') {
  958. DecimalButton();
  959. }
  960. }
  961. public void keyTyped(KeyEvent evt) {
  962. keyD(evt, evt.getKeyChar());
  963. }
  964. public void keyPressed(KeyEvent e) {
  965. }
  966. public void keyReleased(KeyEvent e) {
  967. }
  968. }