/* * * Title: The Calculator (Basic AWT) * written by: Arthur Y. Gapusan * Date Finished: May 2, 2004 * email: ajgapusan@hotmail.com, * * + No Layouts! Bunch of setBounds methods... * * http://www.planet-java.biz.ly */ import java.awt.*; import java.applet.*; import java.awt.event.*; import java.util.*; class Calculator extends Frame implements ActionListener { TextField tf; Button b[] = new Button[10]; Button dot, equals; Button add, sub, mul, div, sqrt, sqr, inverse; Button back, clear, m_in, m_out, m_clear; Button ok; String str; String op, lastOperation; double memory = 0; double ans, num1 = 0, num2 = 0; boolean ready = false, performed = false; Calculator(String s) { setLayout(null); setTitle(s); tf = new TextField(); tf.setFont(new Font("Dialog", Font.BOLD, 25)); tf.setBackground(new Color(90,90,90)); tf.setForeground(Color.white); tf.setBounds(30, 50, 269, 35); add(tf); tf.addActionListener(this); clear = new Button("C"); change(clear, 20); add(clear); clear.setForeground(new Color(245, 50, 50)); clear.setBounds(305, 53, 50, 30); clear.addActionListener(this); for (int i=0;i<10;i++) { b[i] = new Button("" + i); change(b[i], 20); add(b[i]); b[i].addActionListener(this); } dot = new Button("."); change(dot, 20); add(dot); dot.addActionListener(this); equals = new Button("="); change(equals, 20); add(equals); equals.addActionListener(this); add = new Button("+"); change(add, 20); add(add); add.addActionListener(this); sub = new Button("-"); change(sub, 20); add(sub); sub.addActionListener(this); mul = new Button("*"); change(mul, 20); add(mul); mul.addActionListener(this); div = new Button("/"); change(div, 20); add(div); div.addActionListener(this); sqr = new Button("x^2"); change(sqr, 15); add(sqr); sqr.addActionListener(this); sqrt = new Button("sqrt"); change(sqrt, 15); add(sqrt); sqrt.addActionListener(this); inverse = new Button("1/x"); change(inverse, 15); add(inverse); inverse.addActionListener(this); back = new Button("Backspace"); change(back, 15); back.setForeground(new Color(230, 230, 230)); add(back); back.addActionListener(this); m_in = new Button("M+"); change(m_in, 20); add(m_in); m_in.addActionListener(this); m_out = new Button("MR"); change(m_out, 20); add(m_out); m_out.addActionListener(this); m_clear = new Button("MC"); change(m_clear, 20); add(m_clear); m_clear.addActionListener(this); b[7].setBounds(30, 100, 50, 30); b[8].setBounds(85, 100, 50, 30); b[9].setBounds(140, 100, 50, 30); add.setBounds(195, 100, 50, 30); sub.setBounds(250, 100, 50, 30); m_clear.setBounds(305, 100, 50, 30); b[4].setBounds(30, 135, 50, 30); b[5].setBounds(85, 135, 50, 30); b[6].setBounds(140, 135, 50, 30); mul.setBounds(195, 135, 50, 30); div.setBounds(250, 135, 50, 30); m_in.setBounds(305, 135, 50, 30); b[1].setBounds(30, 170, 50, 30); b[2].setBounds(85, 170, 50, 30); b[3].setBounds(140, 170, 50, 30); sqr.setBounds(195, 170, 50, 30); sqrt.setBounds(250, 170, 50, 30); m_out.setBounds(305, 170, 50, 30); b[0].setBounds(30, 205, 50, 30); dot.setBounds(85, 205, 50, 30); equals.setBounds(140, 205, 50, 30); inverse.setBounds(195, 205, 50, 30); back.setBounds(250, 205, 105, 30); Label l[] = new Label[2]; l[0] = new Label("by: Arthur Y. Gapusan"); l[1] = new Label("Copyright(C) 2004"); for (int i=0;i<2;i++) { l[i].setFont(new Font("TimesRoman", Font.BOLD, 10)); l[i].setForeground(new Color(35, 35, 35)); l[i].setBackground(new Color(65, 65, 65)); add(l[i]); } l[0].setBounds(30, 238, 120, 10); l[1].setBounds(273, 238, 85, 10); addWindowListener(new Window()); } public void change(Button b, int size) { b.setBackground(new Color(40, 40, 40)); b.setForeground(new Color(150, 150, 150)); b.setFont(new Font("SansSerif", Font.BOLD, size)); } // |----------------------| // | Action Event | // |----------------------| public void actionPerformed(ActionEvent ae) { if (ae.getSource() == clear) { tf.setText(""); str = ""; op = ""; lastOperation = ""; ans = 0; num1 = 0; num2 = 0; ready = false; performed = false; } str = tf.getText(); int len = str.length(); if (ae.getSource() == back) { String newString = ""; for (int i=0;i3;k--, i++) { g.setColor(new Color(25+k*20,25+k*20,25+k*20)); g.fillRoundRect(startX + i, startY + i, 360, 225, 15, 15); } g.setColor(new Color(65,65,65)); g.fillRoundRect((startX + i) - 1, (startY + i) - 1, 360, 225, 15, 15); } public static void main(String args[]) { Calculator c = new Calculator("The Calculator"); c.setSize(378, 261); c.setVisible(true); c.setResizable(false); } } class Window extends WindowAdapter { public void windowClosing(WindowEvent we) { System.exit(0); } }