youmed

Calculator near Final

Dec 9th, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 31.75 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Dimension;
  3. import java.awt.Font;
  4. import java.awt.Insets;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.io.IOException;
  8.  
  9. import javax.swing.JButton;
  10. import javax.swing.JFrame;
  11. import javax.swing.JTextField;
  12. public class Operator extends JFrame implements ActionListener
  13. {
  14.  
  15. private static final long serialVersionUID = 1L;
  16. static int c1, c2, c3;
  17. static JTextField answer;
  18. static JButton b0, b1, b2, b3, b4, b5, b6 ,b7, b8, b9, clear, multiply, divide, add, subtract, equals, root, sin, cos , tan, dot, squared, cubed, ans, pi, plusOrMinus, help, shift;
  19. static double iFNum, iSNum , iFinalAnswer;
  20. static double iFNumSin, iFNumCos, iFNumTan;
  21. static double iFinalAnswerSin, iFinalAnswerCos, iFinalAnswerTan;
  22. static String sFNum, sSNum;
  23. Font textFont = new Font("Comic Sans MS", Font.PLAIN, 15);
  24. Font textFont2 = new Font("Comic Sans MS", Font.PLAIN, 15);
  25. Font textFont3 = new Font("Comic Sans MS", Font.PLAIN, 9);
  26. static int i = 0;
  27.  
  28.  
  29.  
  30. static boolean bMultiply = false, bDivide = false, bAdd = false, bSubtract = false, bRoot = false, bSin = false, bCos =false, bTan = false, bSquared = false, bCubed = false, bShift = false, bShiftSin = false, bShiftCos = false, bShiftTan = false;
  31.  
  32. public Operator(){
  33. super("Calc");
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41. setBackground(Color.yellow);
  42.  
  43.  
  44. setSize(330,400);
  45. setResizable(false);
  46. setLayout(null);
  47. setLocationRelativeTo(null);
  48.  
  49.  
  50.  
  51. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  52. Insets insets = getInsets();
  53.  
  54.  
  55. b0 = new JButton("0");
  56. Dimension size = b0.getPreferredSize();
  57.  
  58. b0.setBounds(10 + insets.left, 240 + insets.top, size.width, size.height);
  59. b0.setSize(50, 50);
  60. b0.setFont(textFont);
  61. b0.addActionListener(this);
  62. getContentPane().add(b0);
  63.  
  64. b1 = new JButton("1");
  65.  
  66. b1.setBounds(10 + insets.left, 180 + insets.top, size.width, size.height);
  67. b1.setSize(50, 50);
  68. b1.setFont(textFont);
  69. b1.addActionListener(this);
  70. getContentPane().add(b1);
  71.  
  72. b2 = new JButton("2");
  73.  
  74. b2.setBounds(70 + insets.left, 180 + insets.top, size.width, size.height);
  75. b2.setSize(50, 50);
  76. b2.setFont(textFont);
  77. b2.addActionListener(this);
  78. getContentPane().add(b2);
  79.  
  80. b3 = new JButton("3");
  81.  
  82. b3.setBounds(130 + insets.left, 180 + insets.top, size.width, size.height);
  83. b3.setSize(50, 50);
  84. b3.setFont(textFont);
  85. b3.addActionListener(this);
  86. getContentPane().add(b3);
  87.  
  88. b4= new JButton("4");
  89.  
  90. b4.setBounds(10 + insets.left, 120 + insets.top, size.width, size.height);
  91. b4.setSize(50, 50);
  92. b4.setFont(textFont);
  93. b4.addActionListener(this);
  94. getContentPane().add(b4);
  95.  
  96. b5 = new JButton("5");
  97.  
  98. b5.setBounds(70 + insets.left, 120 + insets.top, size.width, size.height);
  99. b5.setSize(50, 50);
  100. b5.setFont(textFont);
  101. b5.addActionListener(this);
  102. getContentPane().add(b5);
  103.  
  104. b6 = new JButton("6");
  105.  
  106. b6.setBounds(130 + insets.left, 120 + insets.top, size.width, size.height);
  107. b6.setSize(50, 50);
  108. b6.setFont(textFont);
  109. b6.addActionListener(this);
  110. getContentPane().add(b6);
  111.  
  112. b7 = new JButton("7");
  113.  
  114. b7.setBounds(10 + insets.left, 60 + insets.top, size.width, size.height);
  115. b7.setSize(50, 50);
  116. b7.setFont(textFont);
  117. b7.addActionListener(this);
  118. getContentPane().add(b7);
  119.  
  120. b8 = new JButton("8");
  121.  
  122. b8.setBounds(70 + insets.left, 60 + insets.top, size.width, size.height);
  123. b8.setSize(50, 50);
  124. b8.setFont(textFont);
  125. b8.addActionListener(this);
  126. getContentPane().add(b8);
  127.  
  128. b9 = new JButton("9");
  129.  
  130. b9.setBounds(130 + insets.left, 60 + insets.top, size.width, size.height);
  131. b9.setSize(50, 50);
  132. b9.setFont(textFont);
  133. b9.addActionListener(this);
  134. getContentPane().add(b9);
  135.  
  136. clear = new JButton("AC");
  137. clear.setBackground(Color.red);
  138. clear.setBounds(70 + insets.left, 300 + insets.top, size.width, size.height);
  139. clear.setSize(110,50);
  140. clear.setFont(textFont);
  141. clear.addActionListener(this);
  142. getContentPane().add(clear);
  143.  
  144. multiply = new JButton(" * ");
  145.  
  146. multiply.setBounds(190 + insets.left, 60 + insets.top, size.width, size.height);
  147. multiply.setSize(50, 40);
  148. multiply.setFont(textFont);
  149. multiply.addActionListener(this);
  150. getContentPane().add(multiply);
  151.  
  152.  
  153. root = new JButton("Sqrt");
  154.  
  155. root.setBounds(250 + insets.left, 60 + insets.top, size.width, size.height);
  156. root.setSize(70, 40);
  157. root.setFont(textFont2);
  158. root.addActionListener(this);
  159. getContentPane().add(root);
  160.  
  161. divide = new JButton(" / ");
  162.  
  163. divide.setBounds(190 + insets.left, 105 + insets.top, size.width, size.height);
  164. divide.setSize(50, 40);
  165. divide.setFont(textFont);
  166. divide.addActionListener(this);
  167. getContentPane().add(divide);
  168.  
  169. sin = new JButton("sin");
  170.  
  171. sin.setBounds(250 + insets.left, 105 + insets.top, size.width, size.height);
  172. sin.setSize(70,40);
  173. sin.setFont(textFont2);
  174. sin.addActionListener(this);
  175. getContentPane().add(sin);
  176.  
  177.  
  178.  
  179. add = new JButton(" + ");
  180.  
  181. add.setBounds(190 + insets.left, 150 + insets.top, size.width, size.height);
  182. add.setSize(50, 40);
  183. add.setFont(textFont);
  184. add.addActionListener(this);
  185. getContentPane().add(add);
  186.  
  187. cos = new JButton("cos");
  188.  
  189. cos.setBounds(250 + insets.left, 150 + insets.top, size.width, size.height);
  190. cos.setSize(70, 40);
  191. cos.setFont(textFont2);
  192. cos.addActionListener(this);
  193. getContentPane().add(cos);
  194.  
  195.  
  196.  
  197.  
  198. subtract = new JButton(" - ");
  199.  
  200. subtract.setBounds(190 + insets.left, 195 + insets.top, size.width, size.height);
  201. subtract.setSize(50, 40);
  202. subtract.setFont(textFont);
  203. subtract.addActionListener(this);
  204. getContentPane().add(subtract);
  205.  
  206. tan = new JButton("tan");
  207.  
  208. tan.setBounds(250 + insets.left, 195 + insets.top, size.width, size.height);
  209. tan.setSize(70,40);
  210. tan.setFont(textFont2);
  211. tan.addActionListener(this);
  212. getContentPane().add(tan);
  213.  
  214. equals = new JButton(" = ");
  215.  
  216. equals.setBounds(250 + insets.left, 240 + insets.top, size.width, size.height);
  217. equals.setSize(70, 50);
  218. equals.setBackground(Color.GREEN);
  219. equals.setFont(textFont);
  220. equals.addActionListener(this);
  221. getContentPane().add(equals);
  222.  
  223. dot = new JButton(".");
  224. dot.setBounds(190 + insets.left, 240 + insets.top, size.width, size.height);
  225. dot.setSize(50, 50);
  226.  
  227. dot.setFont(textFont);
  228. dot.addActionListener(this);
  229. getContentPane().add(dot);
  230.  
  231. shift = new JButton("shift");
  232. shift.setBounds(190 + insets.left, 300 + insets.top, size.width, size.height);
  233. shift.setSize(70, 50);
  234.  
  235. shift.setFont(textFont);
  236. shift.addActionListener(this);
  237. getContentPane().add(shift);
  238.  
  239. help = new JButton("help");
  240. help.setBounds(250 + insets.left, 300 + insets.top, size.width, size.height);
  241. help.setSize(70, 50);
  242.  
  243. help.setFont(textFont);
  244. help.addActionListener(this);
  245. getContentPane().add(help);
  246.  
  247. squared = new JButton("x^2");
  248. squared.setBounds(70 + insets.left, 240 + insets.top, size.width, size.height);
  249. squared.setSize(50, 50);
  250.  
  251. squared.setFont(textFont3);
  252. squared.addActionListener(this);
  253. getContentPane().add(squared);
  254.  
  255. cubed = new JButton("x^3");
  256. cubed.setBounds(130 + insets.left, 240 + insets.top, size.width, size.height);
  257. cubed.setSize(50, 50);
  258.  
  259. cubed.setFont(textFont3);
  260. cubed.addActionListener(this);
  261. getContentPane().add(cubed);
  262.  
  263. plusOrMinus = new JButton("+/-");
  264. plusOrMinus.setBounds(10 + insets.left, 300 + insets.top, size.width, size.height);
  265. plusOrMinus.setSize(50, 50);
  266.  
  267. plusOrMinus.setFont(textFont3);
  268. plusOrMinus.addActionListener(this);
  269. getContentPane().add(plusOrMinus);
  270.  
  271. answer = new JTextField();
  272.  
  273. answer.setBounds(10 + insets.left, 0 + insets.top, size.width, size.height);
  274. answer.setSize(310, 50);
  275. answer.setFont(new Font("Comic SANS MS", Font.PLAIN, 24));
  276.  
  277. answer.setEditable(false);
  278. answer.addActionListener(this);
  279. getContentPane().add(answer);
  280. multiply.setEnabled(false);
  281. divide.setEnabled(false);
  282. add.setEnabled(false);
  283. subtract.setEnabled(false);
  284. root.setEnabled(false);
  285. sin.setEnabled(false);
  286. cos.setEnabled(false);
  287. tan.setEnabled(false);
  288. }
  289.  
  290.  
  291.  
  292. public void actionPerformed(ActionEvent e){
  293. try{
  294. if(e.getSource() == b0){
  295. String currentText = answer.getText();
  296. answer.setText(currentText + "0");
  297.  
  298. }
  299. if(e.getSource() == b1){
  300. String currentText = answer.getText();
  301. answer.setText(currentText + "1");
  302.  
  303. }
  304.  
  305. if(e.getSource() == b2){
  306. String currentText = answer.getText();
  307. answer.setText(currentText + "2");
  308.  
  309.  
  310. }
  311.  
  312. if(e.getSource() == b3){
  313. String currentText = answer.getText();
  314. answer.setText(currentText +"3");
  315. }
  316. if(e.getSource() == b4){
  317. String currentText = answer.getText();
  318. answer.setText(currentText + "4");
  319.  
  320.  
  321. }
  322. if(e.getSource() == b5){
  323. String currentText = answer.getText();
  324. answer.setText(currentText + "5");
  325. }
  326. if(e.getSource() == b6){
  327. String currentText = answer.getText();
  328. answer.setText(currentText + "6");
  329. }
  330.  
  331. if(e.getSource() == b7){
  332. String currentText = answer.getText();
  333. answer.setText(currentText +"7");
  334. }
  335. if(e.getSource() == b8){
  336. String currentText = answer.getText();
  337. answer.setText(currentText + "8");
  338.  
  339. }
  340. if(e.getSource() == b9){
  341. String currentText = answer.getText();
  342. answer.setText(currentText + "9");
  343. }
  344. if(e.getSource() == shift){
  345. bShift = true;
  346. }
  347. if(e.getSource() == help){
  348. try
  349. {
  350. Process p=Runtime.getRuntime().exec("cmd /c start http://bit.ly/1bAHqo5");
  351.  
  352. }
  353. catch(IOException e1) {System.out.println(e1);
  354. }
  355. }
  356.  
  357.  
  358. if(e.getSource() == dot){
  359.  
  360. String currentText = answer.getText();
  361.  
  362.  
  363. answer.setText(currentText + ".");
  364.  
  365. }
  366. if(e.getSource() == plusOrMinus){
  367. String currentText = answer.getText();
  368. answer.setText(currentText + "-");
  369. }
  370. if(e.getSource() == multiply){
  371. denied();
  372. bMultiply = true;
  373.  
  374. sFNum = answer.getText();
  375. iFNum = Double.parseDouble(sFNum);
  376. setTextFalse();
  377. undenied();
  378. }
  379. if(e.getSource() == divide){
  380. denied();
  381. bDivide = true;
  382. sFNum = answer.getText();
  383. iFNum = Double.parseDouble(sFNum);
  384. setTextFalse();
  385. undenied();
  386. }
  387. if(e.getSource() == add){
  388. denied();
  389. bAdd = true;
  390. sFNum = answer.getText();
  391. iFNum = Double.parseDouble(sFNum);
  392. setTextFalse();
  393. undenied();
  394. }
  395. if(e.getSource() == subtract){
  396. denied();
  397. bSubtract = true;
  398. sFNum = answer.getText();
  399. iFNum = Double.parseDouble(sFNum);
  400. setTextFalse();
  401. undenied();
  402. }
  403. if(e.getSource() == root){
  404. bRoot = true;
  405. sFNum = answer.getText();
  406. iFNum = Double.parseDouble(sFNum);
  407. setTextFalse();
  408. undenied();
  409. }
  410.  
  411. if(e.getSource() == cos){
  412. if(bShift == true){
  413. bShiftCos = true;
  414. sFNum = answer.getText();
  415. iFNum = Double.parseDouble(sFNum);
  416. iFNumCos = Math.toRadians(iFNum);
  417. }
  418. else{
  419. bCos = true;
  420. sFNum = answer.getText();
  421. iFNum = Double.parseDouble(sFNum);
  422. iFNumCos = Math.toRadians(iFNum);
  423. }
  424. setTextFalse();
  425. undenied();
  426. }
  427.  
  428. if(e.getSource() == sin){
  429. if(bShift == true){
  430. bShiftSin = true;
  431. sFNum = answer.getText();
  432. iFNum = Double.parseDouble(sFNum);
  433. iFNumSin = Math.toRadians(iFNum);
  434. }else{
  435. bSin = true;
  436. sFNum = answer.getText();
  437. iFNum = Double.parseDouble(sFNum);
  438. iFNumSin = Math.toRadians(iFNum);
  439. }
  440. setTextFalse();
  441. undenied();
  442. }
  443. if(e.getSource() == tan){
  444. if(bShift == true){
  445. bShiftTan = true;
  446. sFNum = answer.getText();
  447. iFNum = Double.parseDouble(sFNum);
  448. iFNumTan = Math.toRadians(iFNum);
  449. }else{
  450. bTan = true;
  451. sFNum = answer.getText();
  452. iFNum = Double.parseDouble(sFNum);
  453. iFNumTan = Math.toRadians(iFNum);
  454. }
  455.  
  456. setTextFalse();
  457. undenied();
  458. }
  459.  
  460.  
  461.  
  462. if(e.getSource() == squared){
  463. bSquared= true;
  464. sFNum = answer.getText();
  465. iFNum = Double.parseDouble(sFNum);
  466.  
  467. setTextFalse();
  468. undenied();
  469. }
  470. if(e.getSource()== cubed){
  471. bCubed= true;
  472. sFNum = answer.getText();
  473. iFNum = Double.parseDouble(sFNum);
  474. setTextFalse();
  475. undenied();
  476. }
  477. if(e.getSource() == clear){
  478.  
  479. undenied();
  480. setTextFalse();
  481. setBooleanFalse();
  482. }
  483. }catch(NumberFormatException rr){
  484. answer.setText("Syntax error, get a brain.");
  485. }
  486. if(e.getSource() == equals){
  487.  
  488. if(bMultiply == true){
  489. sSNum = answer.getText();
  490. iSNum = Double.parseDouble(sSNum);
  491. iFinalAnswer = iFNum * iSNum;
  492. answer.setText(" " + iFinalAnswer);
  493.  
  494. }
  495. if(bSubtract == true){
  496. sSNum = answer.getText();
  497. iSNum = Double.parseDouble(sSNum);
  498. iFinalAnswer = iFNum - iSNum;
  499. answer.setText("" + iFinalAnswer);
  500. }
  501. if(bDivide == true){
  502. sSNum = answer.getText();
  503. iSNum = Double.parseDouble(sSNum);
  504. iFinalAnswer = iFNum / iSNum;
  505. answer.setText("" + iFinalAnswer);
  506. }
  507. if(bAdd == true){
  508. sSNum = answer.getText();
  509. iSNum = Double.parseDouble(sSNum);
  510. iFinalAnswer = iFNum + iSNum;
  511. answer.setText("" + iFinalAnswer);
  512.  
  513. }
  514. if(bRoot == true){
  515. iFinalAnswer = Math.sqrt(iFNum);
  516. answer.setText("" + iFinalAnswer);
  517. }
  518. if(bCos == true){
  519. iFinalAnswerCos = Math.cos(iFNumCos);
  520.  
  521. answer.setText("" + iFinalAnswerCos);
  522. }
  523. if(bSin == true){
  524. iFinalAnswerSin = Math.sin(iFNumSin) ;
  525.  
  526. answer.setText("" + iFinalAnswerSin);
  527. }
  528. if(bTan == true){
  529. iFinalAnswerTan = Math.tan(iFNumTan);
  530.  
  531. answer.setText("" + iFinalAnswerTan);
  532. }
  533. if(bShiftSin == true){
  534. iFinalAnswerSin = Math.asin(iFNumSin);
  535.  
  536. answer.setText("" + iFinalAnswerSin);
  537. }
  538. if(bShiftCos == true){
  539. iFinalAnswerCos = Math.acos(iFNumCos);
  540.  
  541. answer.setText("" + iFinalAnswerCos);
  542. }
  543. if(bShiftTan == true){
  544. iFinalAnswerTan = Math.atan(iFNumTan);
  545.  
  546. answer.setText("" + iFinalAnswerTan);
  547. }
  548. if(bSquared == true){
  549. iFinalAnswer = Math.pow(iFNum, 2);
  550.  
  551. answer.setText("" + iFinalAnswer);
  552. }
  553. if(bCubed == true){
  554. iFinalAnswer = Math.pow(iFNum, 3);
  555.  
  556. answer.setText("" + iFinalAnswer);
  557. }
  558.  
  559.  
  560. }
  561. undenied();
  562. }
  563.  
  564.  
  565.  
  566.  
  567.  
  568.  
  569.  
  570.  
  571. public void actionPerformedEvent(ActionEvent y){
  572.  
  573. }
  574. private static void denied(){
  575. b0.setEnabled(false);
  576. b1.setEnabled(false);
  577. b2.setEnabled(false);
  578. b3.setEnabled(false);
  579. b4.setEnabled(false);
  580. b5.setEnabled(false);
  581. b6.setEnabled(false);
  582. b7.setEnabled(false);
  583. b8.setEnabled(false);
  584. b9.setEnabled(false);
  585. multiply.setEnabled(false);
  586. divide.setEnabled(false);
  587. add.setEnabled(false);
  588. subtract.setEnabled(false);
  589. root.setEnabled(false);
  590. sin.setEnabled(false);
  591. cos.setEnabled(false);
  592. tan.setEnabled(false);
  593.  
  594. }
  595.  
  596. private static void undenied(){
  597.  
  598. b0.setEnabled(true);
  599. b1.setEnabled(true);
  600. b2.setEnabled(true);
  601. b3.setEnabled(true);
  602. b4.setEnabled(true);
  603. b5.setEnabled(true);
  604. b6.setEnabled(true);
  605. b7.setEnabled(true);
  606. b8.setEnabled(true);
  607. b9.setEnabled(true);
  608. multiply.setEnabled(true);
  609. divide.setEnabled(true);
  610. add.setEnabled(true);
  611. subtract.setEnabled(true);
  612. root.setEnabled(true);
  613. sin.setEnabled(true);
  614. cos.setEnabled(true);
  615. tan.setEnabled(true);
  616.  
  617. }
  618.  
  619. private static void setTextFalse(){
  620.  
  621. answer.setText(" ");
  622.  
  623.  
  624.  
  625. }
  626.  
  627. private static void setBooleanFalse(){
  628. bMultiply = false;
  629. bDivide = false;
  630. bAdd = false;
  631. bSubtract = false;
  632. bRoot = false;
  633. bSin = false;
  634. bCos = false;
  635. bTan = false;
  636. bSquared = false;
  637. bCubed = false;
  638. bShift = false;
  639. bShiftSin = false;
  640. bShiftCos = false;
  641. bShiftTan = false;
  642. }
  643.  
  644.  
  645. }
Advertisement
Add Comment
Please, Sign In to add comment