Advertisement
Guest User

Untitled

a guest
May 24th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.62 KB | None | 0 0
  1.  
  2. /** README
  3. * @author kali
  4. *
  5. # did you know that you can use both the numpad or the num keys for numbers and operations
  6. *
  7. # Hit P or HOME key to save text, .txt will be saved to documents folder on both OSX and Windows (name: calc print out.txt)
  8. # Hit C to copy to clipboard
  9. # Hit V to paste to clipboard
  10. *
  11. # known issues and notes
  12. *
  13. * 1. Can't do multiple operations is one line
  14. * 2. UI manager wont work on scroll bar
  15. * 3. bad Unicode for Pi key (lower-left corner) (only sometimes)
  16. * 4. 5.03+3 = 8.0300000001 (I blame java)
  17. * 5. fix PI key [ 2 + Pi = 3.14] fixed*
  18. *
  19. * a. back button is for term 1 or 2. not sum (same as TI-83)
  20. * b. Ans added, |x| hidden
  21. * c. under score key doubles as "-" and other unused keys double as used keys
  22. * d. CA key is to "clear all" including memory #1 & #2
  23. * e. program should ignor latters that are pasted in
  24. */
  25. import java.awt.BorderLayout;
  26. import java.awt.Color;
  27. import java.awt.Dimension;
  28. import java.awt.Font;
  29. import java.awt.GridLayout;
  30. import java.awt.Toolkit;
  31. import java.awt.datatransfer.Clipboard;
  32. import java.awt.datatransfer.DataFlavor;
  33. import java.awt.datatransfer.StringSelection;
  34. import java.awt.datatransfer.Transferable;
  35. import java.awt.event.ActionEvent;
  36. import java.awt.event.ActionListener;
  37. import java.awt.event.KeyAdapter;
  38. import java.awt.event.KeyEvent;
  39. import java.io.BufferedWriter;
  40. import java.io.File;
  41. import java.io.FileWriter;
  42. import java.io.IOException;
  43. import java.io.PrintWriter;
  44. import java.text.DecimalFormat;
  45.  
  46. import javax.swing.BorderFactory;
  47. import javax.swing.JButton;
  48. import javax.swing.JFrame;
  49. import javax.swing.JPanel;
  50. import javax.swing.JScrollPane;
  51. import javax.swing.JTextArea;
  52. import javax.swing.UIManager;
  53.  
  54. public class CalculatorV4 {
  55.  
  56. // --------------------------------------------
  57.  
  58. private static JFrame frame = new JFrame("Calculator");
  59. private static JTextArea top = new JTextArea();
  60. private static JScrollPane scroll = new JScrollPane(top);
  61.  
  62. private static String co = "";
  63. private static String numb = "";
  64. private static String str_copy;
  65.  
  66. private static double numb1 = 0;
  67. private static double numb2 = 0;
  68.  
  69. private static double x = 0;
  70. private static double y = 0;
  71.  
  72. private static double mem = 0;
  73. private static double mem1 = 0;
  74.  
  75. private static int unused = 0; // believe it or not but this is needed
  76.  
  77. private static DecimalFormat f = new DecimalFormat("##.00"); // decimal
  78. // points
  79.  
  80. // -------------------------------------------- not my work*
  81. private static class CalcButton extends JButton {
  82. public CalcButton(String label) {
  83. super(label);
  84. this.addActionListener(new ActionListener() {
  85.  
  86. @Override
  87. public void actionPerformed(ActionEvent arg0) {
  88. frame.requestFocusInWindow();
  89. }
  90. });
  91. }
  92. }
  93.  
  94. // --------------------------------------------
  95.  
  96. public static void main(String args[]) {
  97.  
  98. try {
  99. // UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
  100. // looks OK ^
  101. UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  102. // UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
  103. // too big ^
  104. } catch (Exception ex) {
  105. ex.printStackTrace();
  106. }
  107.  
  108. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  109.  
  110. JPanel contentPane = new JPanel();
  111. contentPane.setBackground(Color.LIGHT_GRAY); // edit color here
  112. contentPane.setLayout((new GridLayout(8, 5)));
  113. contentPane.setBorder(BorderFactory.createCompoundBorder(
  114. BorderFactory.createMatteBorder(1, 0, 0, 0, new Color(182, 182, 182)),
  115. BorderFactory.createMatteBorder(2, 1, 1, 1, contentPane.getBackground())));
  116. // i'm not even going to try take credit for this
  117.  
  118. // top.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
  119. top.setFont(new Font("Ubuntu", Font.PLAIN, 16));// change to 13 or 14
  120. // "Century Gothic"
  121. top.setEnabled(false);
  122. top.setDisabledTextColor(Color.black);
  123. top.setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2, contentPane.getBackground()));
  124. top.setLineWrap(true); // no X axis scroll bar
  125. top.setWrapStyleWord(true); // will not cut off mid work
  126.  
  127. scroll.setBorder(null);
  128. // scroll.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
  129. // scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
  130.  
  131. final ActionListener one_listener = new ActionListener() {
  132. public void actionPerformed(ActionEvent e) {
  133. numb = numb + '1';
  134. top.append("1");
  135. }
  136. };
  137. final ActionListener two_listener = new ActionListener() {
  138. public void actionPerformed(ActionEvent e) {
  139. numb = numb + '2';
  140. top.append("2");
  141. }
  142. };
  143. final ActionListener three_listener = new ActionListener() {
  144. @Override
  145. public void actionPerformed(ActionEvent e) {
  146. numb = numb + '3';
  147. top.append("3");
  148. }
  149. };
  150. final ActionListener four_listener = new ActionListener() {
  151. @Override
  152. public void actionPerformed(ActionEvent e) {
  153. numb = numb + '4';
  154. top.append("4");
  155. }
  156. };
  157. final ActionListener five_listener = new ActionListener() {
  158. public void actionPerformed(ActionEvent e) {
  159. numb = numb + '5';
  160. top.append("5");
  161. }
  162. };
  163. final ActionListener six_listener = new ActionListener() {
  164. @Override
  165. public void actionPerformed(ActionEvent e) {
  166. numb = numb + '6';
  167. top.append("6");
  168. }
  169. };
  170. final ActionListener seven_listener = new ActionListener() {
  171. @Override
  172. public void actionPerformed(ActionEvent e) {
  173. numb = numb + '7';
  174. top.append("7");
  175. }
  176. };
  177. final ActionListener eight_listener = new ActionListener() {
  178. @Override
  179. public void actionPerformed(ActionEvent e) {
  180. numb = numb + '8';
  181. top.append("8");
  182. }
  183. };
  184. final ActionListener nine_listener = new ActionListener() {
  185. @Override
  186. public void actionPerformed(ActionEvent e) {
  187. numb = numb + '9';
  188. top.append("9");
  189. }
  190. };
  191. final ActionListener zero_listener = new ActionListener() {
  192. @Override
  193. public void actionPerformed(ActionEvent e) {
  194. numb = numb + '0';
  195. top.append("0");
  196. }
  197. };
  198. final ActionListener multi_listener = new ActionListener() {
  199. @Override
  200. public void actionPerformed(ActionEvent e) {
  201. co = ((JButton) e.getSource()).getText();
  202. OPERATION();
  203. top.append(co);
  204. }
  205. };
  206. final ActionListener divide_listener = new ActionListener() {
  207. @Override
  208. public void actionPerformed(ActionEvent e) {
  209. co = ((JButton) e.getSource()).getText();
  210. OPERATION();
  211. top.append(co);
  212. }
  213. };
  214. final ActionListener minus_listener = new ActionListener() {
  215. @Override
  216. public void actionPerformed(ActionEvent e) {
  217. co = ((JButton) e.getSource()).getText();
  218. OPERATION();
  219. top.append(co);
  220. }
  221. };
  222. final ActionListener plus_listener = new ActionListener() {
  223. @Override
  224. public void actionPerformed(ActionEvent e) {
  225. co = ((JButton) e.getSource()).getText();
  226. OPERATION();
  227. top.append(co);
  228. }
  229. };
  230. final ActionListener equal_listener = new ActionListener() {
  231. @Override
  232. public void actionPerformed(ActionEvent e) {
  233. if (numb == null || numb.isEmpty())
  234. return;
  235. EQUAL();
  236. }
  237. };
  238. final ActionListener dec_listener = new ActionListener() {
  239.  
  240. @Override
  241. public void actionPerformed(ActionEvent e) {
  242. numb = numb + '.';
  243. top.append(".");
  244. }
  245. };
  246. final ActionListener ans_listener = new ActionListener() {
  247.  
  248. @Override
  249. public void actionPerformed(ActionEvent e) {
  250. if (x != 0) {
  251. numb = Double.toString(x);
  252. numb1 = x;
  253. numb2 = x;
  254. y = x;
  255. unused = 0;
  256. top.append("\nANS");
  257.  
  258. }
  259. }
  260. };
  261. final ActionListener print_listener = new ActionListener() {
  262. @Override
  263. public void actionPerformed(ActionEvent e) {
  264.  
  265. String path = System.getProperty("user.home") + File.separator + "Documents";
  266. File customDir = new File(path);
  267. PrintWriter out;
  268. try {
  269. out = new PrintWriter(new BufferedWriter(new FileWriter(customDir + "/calc print out.txt")));
  270. // out = new PrintWriter(new BufferedWriter(new
  271. // FileWriter("/Users/myself/Desktop/area1.txt")));
  272. out.write(top.getText());
  273.  
  274. out.close();
  275. } catch (IOException e1) {
  276. e1.printStackTrace();
  277. }
  278.  
  279. }
  280. };
  281.  
  282. final ActionListener copy_listener = new ActionListener() {
  283. public void actionPerformed(ActionEvent e) {
  284. if (numb == "") {
  285. str_copy = Double.toString(x);
  286. } else {
  287. str_copy = numb;
  288. }
  289. StringSelection selection = new StringSelection(str_copy);
  290. Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
  291. clipboard.setContents(selection, selection);
  292. System.out.println("clipboard is: " + clipboard);
  293. }
  294. };
  295.  
  296. final ActionListener paste_listener = new ActionListener() {
  297. public void actionPerformed(ActionEvent e) {
  298. unused = 1; // enable ans (almost forgot this
  299. Clipboard clipboard_paste = Toolkit.getDefaultToolkit().getSystemClipboard();
  300. Transferable transfer = clipboard_paste.getContents(this);
  301. // System.out.println(transfer);
  302. try {
  303. top.append((String) transfer.getTransferData(DataFlavor.stringFlavor) + "\n");
  304. } catch (Exception err) {
  305. err.printStackTrace();
  306. }
  307. numb = top.getText();
  308. System.out.println("numb is:" + numb);
  309. numb1 = Double.parseDouble(numb);
  310.  
  311. }
  312. };
  313.  
  314. final JButton paste = new CalcButton("paste");
  315. paste.addActionListener(paste_listener);
  316.  
  317. final JButton copy = new CalcButton("copy");
  318. copy.addActionListener(copy_listener);
  319.  
  320. final JButton print = new CalcButton("save");
  321. print.addActionListener(print_listener);
  322.  
  323. final JButton ans = new CalcButton("Ans");
  324. ans.addActionListener(ans_listener);
  325.  
  326. final JButton one = new CalcButton("1");
  327. one.addActionListener(one_listener);
  328.  
  329. final JButton two = new CalcButton("2");
  330. two.addActionListener(two_listener);
  331.  
  332. final JButton three = new CalcButton("3");
  333. three.addActionListener(three_listener);
  334.  
  335. final JButton four = new CalcButton("4");
  336. four.addActionListener(four_listener);
  337.  
  338. final JButton five = new CalcButton("5");
  339. five.addActionListener(five_listener);
  340.  
  341. final JButton six = new CalcButton("6");
  342. six.addActionListener(six_listener);
  343.  
  344. final JButton seven = new CalcButton("7");
  345. seven.addActionListener(seven_listener);
  346.  
  347. final JButton eight = new CalcButton("8");
  348. eight.addActionListener(eight_listener);
  349.  
  350. final JButton nine = new CalcButton("9");
  351. nine.addActionListener(nine_listener);
  352.  
  353. final JButton zero = new CalcButton("0");
  354. zero.addActionListener(zero_listener);
  355.  
  356. final JButton Dec = new CalcButton(".");
  357. Dec.addActionListener(dec_listener);
  358.  
  359. final JButton Equal = new CalcButton("=");
  360. Equal.addActionListener(equal_listener);
  361.  
  362. final JButton Plus = new CalcButton("+");
  363. Plus.addActionListener(plus_listener);
  364.  
  365. final JButton Multi = new CalcButton("*");
  366. Multi.addActionListener(multi_listener);
  367.  
  368. final JButton Minus = new CalcButton("-");
  369. Minus.addActionListener(minus_listener);
  370.  
  371. final JButton Divide = new CalcButton("/");
  372. Divide.addActionListener(divide_listener);
  373.  
  374. // \u03C0
  375.  
  376. final JButton Pi = new CalcButton("π");
  377. Pi.addActionListener(new ActionListener() {
  378. @Override
  379. public void actionPerformed(ActionEvent e) {
  380. // numb1 = 0;
  381. // numb2 = 0;
  382. numb = Double.toString(Math.PI);
  383. top.append("\n" + numb);
  384. }
  385. });
  386.  
  387. JButton Ce = new CalcButton("CA");
  388. Ce.addActionListener(new ActionListener() {
  389. @Override
  390. public void actionPerformed(ActionEvent e) {
  391. top.setText(null); // or " "
  392. co = "";
  393. numb = "";
  394. numb1 = 0;
  395. numb2 = 0;
  396. x = 0;
  397. y = 0;
  398. mem = 0;
  399. mem1 = 0;
  400. }
  401. });
  402.  
  403. JButton RootX = new CalcButton("√x");
  404. RootX.addActionListener(new ActionListener() {
  405. @Override
  406. public void actionPerformed(ActionEvent e) {
  407. try {
  408. x = Double.parseDouble(numb);
  409. } catch (Exception i) {
  410. System.out.println("root error caught");
  411. }
  412. x = Math.sqrt(x);
  413. numb = Double.toString(x);
  414. top.append("\n" + numb);
  415. }
  416. });
  417.  
  418. JButton C = new CalcButton("C");
  419. C.addActionListener(new ActionListener() {
  420. @Override
  421. public void actionPerformed(ActionEvent e) {
  422. numb = "";
  423. numb1 = 0;
  424. numb2 = 0;
  425. top.append("\n\n\n----------------------\n");
  426. }
  427. });
  428.  
  429. JButton Del = new CalcButton("←");
  430. Del.addActionListener(new ActionListener() {
  431.  
  432. @Override
  433. public void actionPerformed(ActionEvent e) {
  434.  
  435. if (numb.length() > 0) {
  436. numb = numb.substring(0, numb.length() - 1);
  437. top.append("\n" + numb);
  438. }
  439. /*
  440. * try { numb = numb.substring(0, numb.length() - 1); } catch
  441. * (Exception E) { } top.append("\n" + numb);
  442. */
  443.  
  444. }
  445. });
  446.  
  447. JButton Log = new CalcButton("LOG");
  448. Log.addActionListener(new ActionListener() {
  449. @Override
  450. public void actionPerformed(ActionEvent e) {
  451. // if (x != 0)
  452. try {
  453. x = Double.parseDouble(numb);
  454. } catch (Exception i) {
  455. System.out.println("log error caught");
  456. }
  457.  
  458. x = Math.log10(x);
  459. top.append("\nLOG(" + numb + ")");
  460. numb = Double.toString(x);
  461. top.append("\n=" + numb);
  462. }
  463. });
  464.  
  465. JButton Sin = new CalcButton("SIN");
  466. Sin.addActionListener(new ActionListener() {
  467. @Override
  468. public void actionPerformed(ActionEvent e) {
  469. try {
  470. x = Double.parseDouble(numb);
  471. } catch (Exception i) {
  472. }
  473. top.append("\nSIN(" + x + ")");
  474.  
  475. x = Math.sin(x);
  476.  
  477. numb = Double.toString(x);
  478. top.append("\n=" + numb);
  479. }
  480. });
  481.  
  482. JButton Cos = new CalcButton("COS");
  483. Cos.addActionListener(new ActionListener() {
  484. @Override
  485. public void actionPerformed(ActionEvent e) {
  486. try {
  487. x = Double.parseDouble(numb);
  488. } catch (Exception i) {
  489. }
  490. top.append("\nCOS(" + x + ")");
  491. x = Math.cos(x);
  492. numb = Double.toString(x);
  493. top.append("\n=" + numb);
  494. }
  495. });
  496.  
  497. JButton Tan = new CalcButton("TAN");
  498. Tan.addActionListener(new ActionListener() {
  499. @Override
  500. public void actionPerformed(ActionEvent e) {
  501. try {
  502. x = Double.parseDouble(numb);
  503. } catch (Exception i) {
  504. }
  505. top.append("\nTAN(" + x + ")");
  506. y = Math.tan(x);
  507.  
  508. numb = Double.toString(y);
  509. y = 0;
  510. top.append("\n=" + numb);
  511. }
  512. });
  513.  
  514. JButton AbsX = new CalcButton("|X|");
  515. AbsX.addActionListener(new ActionListener() {
  516. @Override
  517. public void actionPerformed(ActionEvent e) {
  518. // if (x != 0){
  519. try {
  520. x = Double.parseDouble(numb);
  521. } catch (Exception i) {
  522. }
  523. x = Math.abs(x);
  524. numb = Double.toString(x);
  525. top.append("\n=" + numb);
  526. }
  527. // }
  528. });
  529.  
  530. JButton TentoThe = new CalcButton("10^X");
  531. TentoThe.addActionListener(new ActionListener() {
  532.  
  533. @Override
  534. public void actionPerformed(ActionEvent e) {
  535. try {
  536. x = Double.parseDouble(numb);
  537. } catch (Exception i) {
  538. }
  539. x = Math.pow(10, x);
  540. numb = Double.toString(x);
  541. top.append("\n=" + numb);
  542. }
  543. });
  544.  
  545. JButton XtotheSecound = new CalcButton("X²");
  546. XtotheSecound.addActionListener(new ActionListener() {
  547.  
  548. @Override
  549. public void actionPerformed(ActionEvent e) {
  550. try {
  551. x = Double.parseDouble(numb);
  552. } catch (Exception i) {
  553. }
  554. // x = Math.pow(x, 2);
  555. x = x * x; // this is faster... i think
  556. numb = Double.toString(x);
  557. top.append("\n=" + numb);
  558. }
  559. });
  560.  
  561. JButton XtotheY = new CalcButton("X³");
  562. XtotheY.addActionListener(new ActionListener() {
  563.  
  564. @Override
  565. public void actionPerformed(ActionEvent e) {
  566. try {
  567. x = Double.parseDouble(numb);
  568. } catch (Exception i) {
  569. }
  570. x = Math.pow(x, 3);
  571. numb = Double.toString(x);
  572. top.append("\n=" + numb);
  573. }
  574. });
  575.  
  576. JButton OneoverX = new CalcButton("1/X");
  577. OneoverX.addActionListener(new ActionListener() {
  578. @Override
  579. public void actionPerformed(ActionEvent e) {
  580. try {
  581. x = Double.parseDouble(numb);
  582. } catch (Exception i) {
  583. }
  584. x = 1 / x;
  585. numb = Double.toString(x);
  586. top.append("\n=" + numb);
  587. }
  588. });
  589.  
  590. JButton Mc = new CalcButton("MC");
  591. Mc.addActionListener(new ActionListener() {
  592.  
  593. @Override
  594. public void actionPerformed(ActionEvent e) {
  595. mem = 0;
  596. mem1 = 0;
  597. top.append("\nmem 1 now is: " + mem);
  598. top.append("\nmem 2 now is: " + mem1 + "\n");
  599.  
  600. }
  601. });
  602.  
  603. JButton Mr = new CalcButton("MR");
  604. Mr.addActionListener(new ActionListener() {
  605.  
  606. @Override
  607. public void actionPerformed(ActionEvent e) {
  608. x = mem;
  609. numb = Double.toString(x);
  610. top.append("\n" + numb);
  611. }
  612. });
  613.  
  614. JButton Ms = new CalcButton("MS");
  615. Ms.addActionListener(new ActionListener() {
  616.  
  617. @Override
  618. public void actionPerformed(ActionEvent e) {
  619. if (numb != null || numb.equals(""))
  620. try {
  621. x = Double.parseDouble(numb);
  622. } catch (Exception i) {
  623. System.out.println("numb is null, if seen i need it prevent error from happening");
  624. }
  625.  
  626. // unoptimized code
  627. // {
  628. mem = x;
  629. numb = Double.toString(mem);
  630. top.append("\n" + numb + "\n");
  631.  
  632. // }
  633. }
  634. });
  635.  
  636. JButton MPlus = new CalcButton("M²R");
  637. MPlus.addActionListener(new ActionListener() {
  638.  
  639. @Override
  640. public void actionPerformed(ActionEvent e) {
  641. try {
  642. x = Double.parseDouble(numb);
  643. } catch (Exception i) {
  644. System.out.println("numb was null");
  645. }
  646. x = mem1;
  647. numb = Double.toString(x);
  648. top.append("\n" + numb);
  649. }
  650. });
  651.  
  652. JButton MMin = new CalcButton("M²S");
  653. MMin.addActionListener(new ActionListener() {
  654.  
  655. @Override
  656. public void actionPerformed(ActionEvent e) {
  657. try {
  658. x = Double.parseDouble(numb);
  659. } catch (Exception i) {
  660. }
  661. mem1 = x;
  662. numb = Double.toString(mem1);
  663. top.append("\n" + numb + "\n");
  664. }
  665. });
  666.  
  667. JButton RootXtotheY = new CalcButton("1/MR");
  668. RootXtotheY.addActionListener(new ActionListener() {
  669. @Override
  670. public void actionPerformed(ActionEvent e) {
  671. try {
  672. x = Double.parseDouble(numb);
  673. } catch (Exception i) {
  674. }
  675. x = 1 / mem;
  676. numb = Double.toString(x);
  677. top.append("\n=" + numb);
  678. }
  679. });
  680. /*
  681. * .addActionListener(new ActionListener() {
  682. *
  683. * @Override public void actionPerformed(ActionEvent e) {
  684. *
  685. * } });
  686. */
  687.  
  688. JButton PlusorMin = new CalcButton("+/-");
  689. PlusorMin.addActionListener(new ActionListener() {
  690.  
  691. @Override
  692. public void actionPerformed(ActionEvent e) {
  693. try {
  694. x = Double.parseDouble(numb);
  695. } catch (Exception i) {
  696. }
  697. // ----
  698. x = x * -1;
  699. numb = Double.toString(x);
  700. top.append("\n" + numb);
  701. /**
  702. * int temp = numb.length(); if (numb2 != 0){ for (int i = 0; i
  703. * < temp; i++) { numb = numb.substring(0, numb.length() - 1);
  704. * top.append("\n" + x); } // numb = Double.toString(x); //
  705. * top.append("\n" + numb); }
  706. */
  707. }
  708. });
  709. JButton SinMin1 = new CalcButton("SIN-¹ ");
  710. SinMin1.addActionListener(new ActionListener() {
  711.  
  712. @Override
  713. public void actionPerformed(ActionEvent e) {
  714. try {
  715. x = Double.parseDouble(numb);
  716. } catch (Exception i) {
  717. }
  718. top.append("\nSIN-1(" + x + ")");
  719. x = Math.asin(x);
  720. numb = Double.toString(x);
  721. top.append("\n=" + numb);
  722.  
  723. }
  724. });
  725. JButton CosMin1 = new CalcButton("COS-¹ ");
  726. CosMin1.addActionListener(new ActionListener() {
  727.  
  728. @Override
  729. public void actionPerformed(ActionEvent e) {
  730. try {
  731. x = Double.parseDouble(numb);
  732. } catch (Exception i) {
  733. }
  734. top.append("\nCOS-1(" + x + ")");
  735. x = Math.acos(x);
  736. numb = Double.toString(x);
  737. top.append("\n=" + numb);
  738.  
  739. }
  740. });
  741. JButton TanMin1 = new CalcButton("TAN-¹ ");
  742. TanMin1.addActionListener(new ActionListener() {
  743.  
  744. @Override
  745. public void actionPerformed(ActionEvent e) {
  746. try {
  747. x = Double.parseDouble(numb);
  748. } catch (Exception i) {
  749. }
  750. top.append("\nTAN-1(" + x + ")");
  751. x = Math.asin(x);
  752. numb = Double.toString(x);
  753. top.append("\n=" + numb);
  754.  
  755. }
  756. });
  757.  
  758. contentPane.add(Mc);
  759. contentPane.add(Mr);
  760. contentPane.add(Ms);
  761. contentPane.add(MPlus); // M²r
  762. contentPane.add(MMin); // m²s
  763.  
  764. contentPane.add(RootXtotheY);
  765. // contentPane.add(AbsX);
  766. contentPane.add(ans);
  767. contentPane.add(SinMin1);
  768. contentPane.add(CosMin1);
  769. contentPane.add(TanMin1);
  770.  
  771. contentPane.add(RootX);
  772. contentPane.add(Log);
  773. contentPane.add(Sin);
  774. contentPane.add(Cos);
  775. contentPane.add(Tan);
  776.  
  777. contentPane.add(XtotheY);
  778. contentPane.add(Ce);
  779. contentPane.add(C);
  780. contentPane.add(Del);
  781. contentPane.add(Divide);
  782.  
  783. contentPane.add(XtotheSecound);
  784. contentPane.add(seven);
  785. contentPane.add(eight);
  786. contentPane.add(nine);
  787. contentPane.add(Multi);
  788.  
  789. contentPane.add(OneoverX);
  790. contentPane.add(four);
  791. contentPane.add(five);
  792. contentPane.add(six);
  793. contentPane.add(Minus);
  794.  
  795. contentPane.add(TentoThe);
  796. contentPane.add(one);
  797. contentPane.add(two);
  798. contentPane.add(three);
  799. contentPane.add(Plus);
  800.  
  801. contentPane.add(Pi);
  802. contentPane.add(PlusorMin);
  803. contentPane.add(zero);
  804. contentPane.add(Dec);
  805. contentPane.add(Equal);
  806.  
  807. frame.setFocusable(true);
  808. frame.setFocusTraversalKeysEnabled(false);
  809.  
  810. frame.addKeyListener(new KeyAdapter() {
  811. @Override
  812. public void keyReleased(KeyEvent e) {
  813. switch (e.getKeyCode()) {
  814. case KeyEvent.VK_V:
  815. paste_listener.actionPerformed(new ActionEvent(paste, 99990, null));
  816. break;
  817.  
  818. case KeyEvent.VK_C:
  819. copy_listener.actionPerformed(new ActionEvent(copy, 99990, null));
  820. break;
  821.  
  822. case KeyEvent.VK_HOME:
  823. case KeyEvent.VK_P:
  824. print_listener.actionPerformed(new ActionEvent(print, 99990, null));
  825. break;
  826.  
  827. case KeyEvent.VK_CLEAR:
  828. ans_listener.actionPerformed(new ActionEvent(ans, 99990, null));
  829. break;
  830.  
  831. case KeyEvent.VK_NUMPAD0:
  832. case KeyEvent.VK_0:
  833. zero_listener.actionPerformed(new ActionEvent(zero, 99990, null));
  834. break;
  835.  
  836. case KeyEvent.VK_NUMPAD1:
  837. case KeyEvent.VK_1:
  838. one_listener.actionPerformed(new ActionEvent(one, 99990, null));
  839. break;
  840.  
  841. case KeyEvent.VK_NUMPAD2:
  842. case KeyEvent.VK_2:
  843. two_listener.actionPerformed(new ActionEvent(two, 99990, null));
  844. break;
  845.  
  846. case KeyEvent.VK_NUMPAD3:
  847. case KeyEvent.VK_3:
  848. three_listener.actionPerformed(new ActionEvent(three, 99990, null));
  849. break;
  850.  
  851. case KeyEvent.VK_NUMPAD4:
  852. case KeyEvent.VK_4:
  853. four_listener.actionPerformed(new ActionEvent(four, 99990, null));
  854. break;
  855.  
  856. case KeyEvent.VK_NUMPAD5:
  857. case KeyEvent.VK_5:
  858. five_listener.actionPerformed(new ActionEvent(five, 99990, null));
  859. break;
  860.  
  861. case KeyEvent.VK_NUMPAD6:
  862. case KeyEvent.VK_6:
  863. six_listener.actionPerformed(new ActionEvent(six, 99990, null));
  864. break;
  865.  
  866. case KeyEvent.VK_NUMPAD7:
  867. case KeyEvent.VK_7:
  868. seven_listener.actionPerformed(new ActionEvent(seven, 99990, null));
  869. break;
  870.  
  871. case KeyEvent.VK_NUMPAD8:
  872. case KeyEvent.VK_8:
  873.  
  874. if ((e.getModifiers() & KeyEvent.SHIFT_MASK) != 0) {
  875. multi_listener.actionPerformed(new ActionEvent(Multi, 99990, null));
  876. } else {
  877. eight_listener.actionPerformed(new ActionEvent(eight, 99990, null));
  878. }
  879.  
  880. break;
  881.  
  882. case KeyEvent.VK_NUMPAD9:
  883. case KeyEvent.VK_9:
  884. nine_listener.actionPerformed(new ActionEvent(nine, 99990, null));
  885. break;
  886.  
  887. case KeyEvent.VK_ADD:
  888. plus_listener.actionPerformed(new ActionEvent(Plus, 99990, null));
  889. break;
  890.  
  891. case KeyEvent.VK_DIVIDE:
  892. divide_listener.actionPerformed(new ActionEvent(Divide, 99990, null));
  893. break;
  894.  
  895. case KeyEvent.VK_SLASH:
  896. divide_listener.actionPerformed(new ActionEvent(Divide, 99990, null));
  897. break;
  898.  
  899. case KeyEvent.VK_MULTIPLY:
  900. multi_listener.actionPerformed(new ActionEvent(Multi, 99990, null));
  901. break;
  902.  
  903. case KeyEvent.VK_SUBTRACT:
  904. minus_listener.actionPerformed(new ActionEvent(Minus, 99990, null));
  905. break;
  906.  
  907. case KeyEvent.VK_MINUS:
  908. minus_listener.actionPerformed(new ActionEvent(Minus, 99990, null));
  909. break;
  910.  
  911. case KeyEvent.VK_ENTER:
  912. equal_listener.actionPerformed(new ActionEvent(Equal, 99990, null));
  913. break;
  914.  
  915. case KeyEvent.VK_DECIMAL:
  916. case KeyEvent.VK_PERIOD:
  917. dec_listener.actionPerformed(new ActionEvent(Dec, 99990, null));
  918. break;
  919.  
  920. case KeyEvent.VK_EQUALS:
  921. if ((e.getModifiers() & KeyEvent.SHIFT_MASK) != 0) {
  922. plus_listener.actionPerformed(new ActionEvent(Plus, 99990, null));
  923. } else {
  924. equal_listener.actionPerformed(new ActionEvent(Equal, 99990, null));
  925. }
  926.  
  927. }
  928. }
  929. });
  930.  
  931. frame.setSize(390, 490);
  932. Toolkit TK = Toolkit.getDefaultToolkit();
  933. Dimension dimen = TK.getScreenSize();
  934. int xPos = (dimen.width / 2) - (frame.getWidth() / 2);
  935. int yPos = (dimen.height / 2) - (frame.getHeight() / 2);
  936. frame.setLocation(xPos, yPos);
  937. frame.setLocationRelativeTo(null); // was not working property
  938. frame.add(contentPane, BorderLayout.SOUTH);
  939. frame.add(scroll);
  940. frame.setVisible(true);
  941. }
  942.  
  943. // combine PLUS,MINUS,DIVIDE and MULTI into OPERATION
  944. public static void OPERATION() {
  945. frame.requestFocus();
  946. System.out.println(unused);
  947.  
  948. // 0 means unused
  949. if (unused == 1 && co != null) {
  950. top.append("\nAns"); // "If It's Stupid But It Works... It's Not
  951. // Stupid!"
  952. }
  953. if (numb == null || numb.isEmpty() || numb == " ") {
  954. return;
  955. }
  956. numb1 = Double.parseDouble(numb);
  957. numb = "";
  958. }
  959.  
  960. public static void EQUAL() {
  961.  
  962. if (numb == null || numb.isEmpty() || co == null || co.isEmpty())
  963. return;
  964.  
  965. numb2 = Double.parseDouble(numb);
  966. if (numb2 == 0) {
  967. top.append(" = " + numb1);
  968. return;
  969. }
  970.  
  971. switch (co) {
  972.  
  973. case "+":
  974. System.out.println("is PLUS");
  975. x = numb1 + numb2;
  976. break;
  977.  
  978. case "-":
  979. System.out.println("is MINUS");
  980. x = numb1 - numb2;
  981. break;
  982.  
  983. case "*":
  984. System.out.println("is MULTI");
  985. x = numb1 * numb2;
  986. break;
  987.  
  988. case "/":
  989. System.out.println("is DIVIDE");
  990. x = numb1 / numb2;
  991. break;
  992.  
  993. }
  994.  
  995. top.append(" = " + f.format(x) + " \n" + x + " \n");
  996. // top.append(" = " + f.format(x) + "\n" + x);
  997. co = null; // needed
  998. numb = "";
  999. numb1 = x; // the new x
  1000. numb2 = 0; // unneeded
  1001. unused = 1; // now its been used
  1002.  
  1003. // top.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
  1004. // scroll.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
  1005. } // end of EQUAL();
  1006. }
  1007. // /////////// TO_DO /////////////////////////////// (done)
  1008. // rcCOSR
  1009. // ASIN
  1010. // INV SIN
  1011. // arcSIN
  1012. // switch abs value and log. Then replace abs value replace with ans DONE*
  1013. // switch C and Ce
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement