Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.20 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Font;
  3. import java.awt.GridLayout;
  4. import java.awt.Image;
  5. import java.awt.Toolkit;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8.  
  9. import javax.swing.JButton;
  10. import javax.swing.JPanel;
  11. import javax.swing.JTextField;
  12. import javax.swing.event.ChangeEvent;
  13. import javax.swing.event.ChangeListener;
  14.  
  15. class Cal extends javax.swing.JFrame implements ActionListener {
  16.  
  17. private DigitButton plusminus, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, bpoint;
  18. private OpButton add, sub, mul, div, del, clear, inv, sqrt, equal;
  19. JTextField tf;
  20. static double a = 0, b = 0, result = 0;
  21. static int operator = 0;
  22. private int point;
  23. private Image icon;
  24.  
  25. Cal() {
  26.  
  27. super("Calculator");
  28. point = 0;
  29. icon = Toolkit.getDefaultToolkit().getImage("icon.png"); // Loading icon image into memory
  30.  
  31. /************* Initialising buttons ************/
  32. b1 = new DigitButton("1");
  33. b2 = new DigitButton("2");
  34. b3 = new DigitButton("3");
  35. b4 = new DigitButton("4");
  36. b5 = new DigitButton("5");
  37. b6 = new DigitButton("6");
  38. b7 = new DigitButton("7");
  39. b8 = new DigitButton("8");
  40. b9 = new DigitButton("9");
  41. b0 = new DigitButton("0");
  42. bpoint = new DigitButton(".");
  43. inv = new OpButton("1/x");
  44. sqrt = new OpButton("√");
  45. plusminus = new DigitButton("±");
  46. del = new OpButton("DEL");
  47.  
  48. add = new OpButton("+");
  49. sub = new OpButton("-");
  50. mul = new OpButton("×");
  51. div = new OpButton("÷");
  52. clear = new OpButton("C");
  53. equal = new OpButton("=");
  54.  
  55. // TextField in which result is to be displayed
  56. tf = new JTextField();
  57. tf.setEditable(false);
  58. tf.setFont(new Font("Segoi UI", Font.BOLD, 25)); // Setting font
  59.  
  60. // tf.setBackground(Color.DARK_GRAY.darker().darker());
  61. // tf.setForeground(Color.WHITE.brighter());
  62. tf.setBackground(Color.WHITE);
  63. tf.setForeground(Color.BLACK);
  64. tf.setText("0");
  65.  
  66. tf.setBorder(null);
  67.  
  68. /********** Setting the location of the buttons ***********/
  69.  
  70. JPanel panel = new JPanel();
  71. JPanel panel2 = new JPanel();
  72. JPanel panel3 = new JPanel();
  73. JPanel panel4 = new JPanel();
  74.  
  75. panel.add(b7);
  76. panel.add(b8);
  77. panel.add(b9);
  78.  
  79. panel.add(b4);
  80. panel.add(b5);
  81. panel.add(b6);
  82.  
  83. panel.add(b1);
  84. panel.add(b2);
  85. panel.add(b3);
  86.  
  87. panel2.add(plusminus);
  88. panel2.add(b0);
  89. panel2.add(bpoint);
  90.  
  91. panel3.add(div);
  92. panel3.add(mul);
  93. panel3.add(sub);
  94. panel3.add(add);
  95. panel3.add(equal);
  96.  
  97. panel4.add(clear);
  98. panel4.add(sqrt);
  99. panel4.add(inv);
  100. panel4.add(del);
  101.  
  102. GridLayout grid = new GridLayout(3, 3);
  103. GridLayout grid2 = new GridLayout(1, 2);
  104. GridLayout grid3 = new GridLayout(5, 1);
  105. GridLayout grid4 = new GridLayout(1, 4);
  106.  
  107. panel.setLayout(grid);
  108. panel.setBounds(0, 160, 240, 240);
  109.  
  110. panel2.setLayout(grid2);
  111. panel2.setBounds(0, 400, 240, 80);
  112.  
  113. panel3.setLayout(grid3);
  114. panel3.setBounds(240, 160, 80, 320);
  115.  
  116. panel4.setLayout(grid4);
  117. panel4.setBounds(0, 80, 320, 80);
  118.  
  119. tf.setBounds(0, 0, 320, 80);
  120.  
  121. tf.setHorizontalAlignment(JTextField.RIGHT); // Setting alignment
  122.  
  123. /******** Adding Panels to the Frame ***********/
  124. add(panel);
  125. add(panel2);
  126. add(panel3);
  127. add(panel4);
  128.  
  129. add(tf);
  130.  
  131. /* Adding ActionListeners to the buttons */
  132. b0.addActionListener(this);
  133. b1.addActionListener(this);
  134. b2.addActionListener(this);
  135. b3.addActionListener(this);
  136. b4.addActionListener(this);
  137. b5.addActionListener(this);
  138. b6.addActionListener(this);
  139. b7.addActionListener(this);
  140. b8.addActionListener(this);
  141. b9.addActionListener(this);
  142. bpoint.addActionListener(this);
  143. clear.addActionListener(this);
  144. equal.addActionListener(this);
  145. add.addActionListener(this);
  146. sub.addActionListener(this);
  147. mul.addActionListener(this);
  148. div.addActionListener(this);
  149. plusminus.addActionListener(this);
  150. sqrt.addActionListener(this);
  151. inv.addActionListener(this);
  152. del.addActionListener(this);
  153.  
  154. ////////////////////////////////////////////
  155.  
  156. setDefaultCloseOperation(EXIT_ON_CLOSE);
  157. setResizable(false);
  158. setIconImage(icon);
  159. setLocationRelativeTo(null);
  160. setSize(335, 515);
  161. setLayout(null);
  162. setVisible(true);
  163.  
  164. }
  165.  
  166. private boolean isZero(String Text) {
  167.  
  168. if (Text.equals("0"))
  169. return true;
  170. else
  171. return false;
  172. }
  173.  
  174. private void getInput(ActionEvent e) {
  175.  
  176. if (tf.getText().equals("∞")) {
  177.  
  178. } else if (tf.getText().equals("Invalid input")) {
  179.  
  180. } else if (e.getSource().equals(b0)) {
  181. if (isZero(tf.getText())) {
  182. tf.setText("0");
  183. } else {
  184. tf.setText(tf.getText().concat("0"));
  185. }
  186. } else if (e.getSource().equals(b1)) {
  187. if (isZero(tf.getText())) {
  188. tf.setText("1");
  189. } else {
  190. tf.setText(tf.getText().concat("1"));
  191. }
  192. } else if (e.getSource().equals(b2)) {
  193. if (isZero(tf.getText())) {
  194. tf.setText("2");
  195. } else {
  196. tf.setText(tf.getText().concat("2"));
  197. }
  198. } else if (e.getSource().equals(b3)) {
  199. if (isZero(tf.getText())) {
  200. tf.setText("3");
  201. } else {
  202. tf.setText(tf.getText().concat("3"));
  203. }
  204. } else if (e.getSource().equals(b4)) {
  205. if (isZero(tf.getText())) {
  206. tf.setText("4");
  207. } else {
  208. tf.setText(tf.getText().concat("4"));
  209. }
  210. } else if (e.getSource().equals(b5)) {
  211. if (isZero(tf.getText())) {
  212. tf.setText("5");
  213. } else {
  214. tf.setText(tf.getText().concat("5"));
  215. }
  216. } else if (e.getSource().equals(b6)) {
  217. if (isZero(tf.getText())) {
  218. tf.setText("6");
  219. } else {
  220. tf.setText(tf.getText().concat("6"));
  221. }
  222. } else if (e.getSource().equals(b7)) {
  223. if (isZero(tf.getText())) {
  224. tf.setText("7");
  225. } else {
  226. tf.setText(tf.getText().concat("7"));
  227. }
  228. } else if (e.getSource().equals(b8)) {
  229. if (isZero(tf.getText())) {
  230. tf.setText("8");
  231. } else {
  232. tf.setText(tf.getText().concat("8"));
  233. }
  234. } else if (e.getSource().equals(b9)) {
  235. if (isZero(tf.getText())) {
  236. tf.setText("9");
  237. } else {
  238. tf.setText(tf.getText().concat("9"));
  239. }
  240. } else if (e.getSource().equals(bpoint)) {
  241. if (point < 1) {
  242. tf.setText(tf.getText().concat("."));
  243. point = 1;
  244. } else {
  245.  
  246. }
  247. } else if (e.getSource().equals(sqrt)) {
  248.  
  249. double val = Double.parseDouble(tf.getText());
  250.  
  251. if (val < 0) {
  252. tf.setText("Invalid input");
  253. } else {
  254. point = 1;
  255. val = Math.sqrt(val);
  256. tf.setText(String.valueOf(val));
  257. }
  258. } else if (e.getSource().equals(inv)) {
  259.  
  260. double val = Double.valueOf(tf.getText());
  261.  
  262. if (val == 0) {
  263. tf.setText("∞");
  264. } else {
  265. point = 1;
  266. val = 1 / val;
  267. tf.setText(String.valueOf(val));
  268. }
  269. } else if (e.getSource().equals(plusminus)) {
  270. double val = Double.valueOf(tf.getText());
  271.  
  272. if (val == 0) {
  273. tf.setText("0");
  274. } else {
  275. point = 1;
  276. val = -val;
  277. tf.setText(String.valueOf(val));
  278. }
  279. }
  280.  
  281. }
  282.  
  283. public void actionPerformed(ActionEvent e) {
  284.  
  285. if (tf.getText().equals("+") || tf.getText().equals("-") || tf.getText().equals("×")
  286. || tf.getText().equals("÷")) {
  287.  
  288. tf.setText("");
  289. getInput(e);
  290.  
  291. } else if (e.getSource().equals(clear)) {
  292. point = 0;
  293. tf.setText("0");
  294. } else if (e.getSource().equals(del)) {
  295.  
  296. String s = tf.getText();
  297. tf.setText("");
  298.  
  299. if (s.equals("∞")) {
  300. tf.setText("0");
  301. point = 0;
  302. } else if (s.equals("Invalid input")) {
  303. tf.setText("0");
  304. point = 0;
  305. } else if (Double.valueOf(s) < 0) {
  306.  
  307. if (s.length() < 3) {
  308.  
  309. if (s.matches("-")) {
  310. point = 0;
  311. tf.setText("0");
  312. } else {
  313. point = 0;
  314. tf.setText("0");
  315. }
  316. } else {
  317.  
  318. for (int i = 0; i < s.length() - 1; i++) {
  319. tf.setText(tf.getText() + s.charAt(i));
  320. }
  321.  
  322. if (tf.getText().contains(".")) {
  323. point = 1;
  324. } else {
  325. point = 0;
  326. }
  327.  
  328. }
  329. } else {
  330.  
  331. if (s.length() < 2) {
  332.  
  333. /*
  334. * if (s.matches("-")) { Text = "0"; tf.setText(Text); } else {
  335. */
  336. point = 0;
  337. tf.setText("0");
  338. // }
  339. } else {
  340.  
  341. for (int i = 0; i < s.length() - 1; i++) {
  342. tf.setText(tf.getText() + s.charAt(i));
  343. }
  344.  
  345. if (tf.getText().contains(".")) {
  346. point = 1;
  347. } else {
  348. point = 0;
  349. }
  350.  
  351. }
  352. }
  353.  
  354. } else if (tf.getText().equals("∞")) {
  355.  
  356. } else if (tf.getText().equals("Invalid input")) {
  357.  
  358. } else if (e.getSource().equals(add)) {
  359.  
  360. a = Double.valueOf(tf.getText());
  361. operator = 1;
  362. tf.setText("+");
  363. } else if (e.getSource().equals(sub)) {
  364.  
  365. a = Double.valueOf(tf.getText());
  366. operator = 2;
  367. tf.setText("-");
  368. } else if (e.getSource().equals(mul)) {
  369.  
  370. a = Double.valueOf(tf.getText());
  371. operator = 3;
  372. tf.setText("×");
  373.  
  374. } else if (e.getSource().equals(div)) {
  375.  
  376. a = Double.valueOf(tf.getText());
  377. operator = 4;
  378. tf.setText("÷");
  379. } else if (e.getSource().equals(equal)) {
  380.  
  381. point = 1;
  382. tf.setText(calcInput(operator, a));
  383. } else {
  384. getInput(e);
  385. }
  386.  
  387. }
  388.  
  389. private String calcInput(int operator, double a) {
  390.  
  391. b = Double.valueOf(tf.getText());
  392. String Result;
  393.  
  394. switch (operator) {
  395.  
  396. case 1:
  397. result = a + b;
  398. break;
  399. case 2:
  400. result = a - b;
  401. break;
  402. case 3:
  403. result = a * b;
  404. break;
  405. case 4:
  406. if (b != 0)
  407. result = a / b;
  408. else {
  409. result = 0;
  410. tf.setText("∞");
  411. }
  412. }
  413.  
  414. if (b == 0)
  415. Result = "∞";
  416. else
  417. Result = String.valueOf(result);
  418.  
  419. operator = 0;
  420.  
  421. return Result;
  422. }
  423. }
  424.  
  425. public class Calc {
  426.  
  427. public static void main(String[] args) {
  428.  
  429. new Cal();
  430. }
  431. }
  432.  
  433. class OpButton extends JButton {
  434.  
  435. private Color bgcolor, frcolor;
  436.  
  437. OpButton(String op) {
  438.  
  439. super(op);
  440. setBorderPainted(false);
  441. setFocusPainted(false);
  442.  
  443. setFont(new Font("Segoi UI", Font.PLAIN, 22));
  444. bgcolor = new Color(0, 153, 255);
  445. frcolor = Color.WHITE;
  446.  
  447. setContentAreaFilled(false);
  448. setOpaque(true);
  449.  
  450. setBackground(bgcolor);
  451. setForeground(frcolor);
  452. setText(op);
  453.  
  454. addChangeListener(new ChangeListener() {
  455.  
  456. @Override
  457. public void stateChanged(ChangeEvent evt) {
  458. if (getModel().isPressed()) {
  459. setBackground(bgcolor.darker().darker());
  460. } else if (getModel().isRollover()) {
  461. setBackground(bgcolor.darker());
  462. } else {
  463. setBackground(bgcolor);
  464. }
  465. }
  466. });
  467. }
  468.  
  469. }
  470.  
  471. class DigitButton extends JButton {
  472.  
  473. private Color bgcolor, color2;
  474. final static long serialVersionUID = 2;
  475.  
  476. DigitButton(String digit) {
  477.  
  478. super(digit);
  479. setBorderPainted(false);
  480. setFocusPainted(false);
  481.  
  482. bgcolor = Color.WHITE;
  483. color2 = new Color(0, 153, 255);
  484.  
  485. setFont(new Font("Segoi UI", Font.BOLD, 22));
  486.  
  487. setContentAreaFilled(false);
  488. setOpaque(true);
  489.  
  490. // setForeground(Color.BLACK.darker().darker());
  491.  
  492. setText(digit);
  493.  
  494. setBackground(bgcolor);
  495. setForeground(color2);
  496.  
  497. addChangeListener(new ChangeListener() {
  498.  
  499. @Override
  500. public void stateChanged(ChangeEvent evt) {
  501. /*
  502. * if (getModel().isPressed()) { setBackground(bgcolor.darker().darker()); //
  503. * setForeground(Color.WHITE); } else if (getModel().isRollover()) {
  504. * setBackground(bgcolor.darker().darker()); // setForeground(Color.WHITE); }
  505. * else { // setForeground(color2); setBackground(bgcolor.darker()); }
  506. */
  507. if (getModel().isPressed()) {
  508. setBackground(bgcolor.darker().darker());
  509. setForeground(Color.WHITE);
  510. } else if (getModel().isRollover()) {
  511. setBackground(bgcolor.darker());
  512. setForeground(Color.WHITE);
  513. } else {
  514. setBackground(bgcolor);
  515. setForeground(color2);
  516. }
  517. }
  518. });
  519.  
  520. }
  521.  
  522. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement