Friday, April 16, 2010

კალკულატორი

package ge.tsu.nikag.calc;
//file: main.java
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

/**
 * @author ნიკოლოზ გოჩიაშვილი
 *
 */
public class Main implements ActionListener {
    /*
     * კლასის ველები
     */
    static String buffer = null;
    static String operacia = null;
    JButton add, sub, mul, div;
    JButton point, equals, C, p, s, r;
    JButton[] digits = new JButton[10];
    String current = new String();
    JTextField display = new JTextField();

    /*
     * ფანჯრის ელემენტების ინიციალიზება
     */
    private static void createAndShowGUI() {
        Main app = new Main();
        /*
         * lamazi konturebi
         */
        JFrame.setDefaultLookAndFeelDecorated(true);

        /*
         * chveulebrivi java fanjara
         */
        // JFrame.isDefaultLookAndFeelDecorated();
        JFrame frame = new JFrame("Mtvleli");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        frame.setBounds(400, 300, 190, 170);

        JPanel topPanel = new JPanel();
        topPanel.setBounds(0, 0, 200, 28);
        topPanel.setLayout(null);

        app.display.setBounds(0, 0, 200, 28);
        app.display.setBackground(Color.black);
        app.display.setForeground(Color.green);
        app.display.setFont(new Font("Palatino", Font.PLAIN, 14));
        topPanel.add(app.display);

        frame.getContentPane().add(topPanel);

        JPanel lowerPanel = new JPanel();
        frame.getContentPane().add(lowerPanel);
        lowerPanel.setBounds(0, 32, 180, 100);
        lowerPanel.setLayout(new GridLayout(0, 4, 2, 2));

        // main :-nika

        for (int i = 0; i < 10; i++) {
            app.digits[i] = new JButton("" + i);
            app.digits[i].addActionListener(app);
            lowerPanel.add(app.digits[i]);
            if (i == 2) {
                app.equals = new JButton("=");
                lowerPanel.add(app.equals);
                app.equals.addActionListener(app);
            }
            if (i == 5) {
                app.sub = new JButton("-");
                lowerPanel.add(app.sub);
                app.sub.addActionListener(app);
            }
            if (i == 8) {
                app.div = new JButton("/");
                lowerPanel.add(app.div);
                app.div.addActionListener(app);
            }
        }
        app.point = new JButton(".");
        lowerPanel.add(app.point);
        app.point.addActionListener(app);
        app.C = new JButton("C");
        lowerPanel.add(app.C);
        app.C.addActionListener(app);
        app.add = new JButton("+");
        lowerPanel.add(app.add);
        app.add.addActionListener(app);
        app.r = new JButton("r");
        lowerPanel.add(app.r);
        app.r.addActionListener(app);
        app.p = new JButton("p");
        lowerPanel.add(app.p);
        app.p.addActionListener(app);
        frame.setVisible(true);
        app.s = new JButton("s");
        lowerPanel.add(app.s);
        app.s.addActionListener(app);
        app.mul = new JButton("x");
        lowerPanel.add(app.mul);
        app.mul.addActionListener(app);
        frame.setVisible(true);
    }

    /*
     * (MAin.java:actionPerformed) აქტიური ღილაკის დამუშავება
     *
     * @see
     * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    public void actionPerformed(ActionEvent e) {
        for (int i = 0; i < 10; i++) {
            if (e.getSource() == digits[i])
                current += "" + i;
        }
        if (e.getSource() == point) {
            current += ".";
        }
        if (e.getSource() == equals) {
            String tmp = Main.operacia(Main.buffer, current, Main.operacia);
            current = tmp;
            display.setText(tmp);
            return;
        }
        if (e.getSource() == add) {
            Main.buffer = current;
            current = "";
            Main.operacia = "add";
            display.setText("");
            return;
        }
        if (e.getSource() == mul) {
            display.setText("");
            Main.buffer = current;
            current = "";
            Main.operacia = "mul";
            return;
        }
        if (e.getSource() == sub) {
            Main.buffer = current;
            current = "";
            Main.operacia = "sub";
            display.setText("");
            return;
        }

        if (e.getSource() == div) {
            Main.buffer = current;
            current = "";
            Main.operacia = "div";
            display.setText("");
            return;
        }
        if (e.getSource() == C) {
            StringBuilder sb = new StringBuilder();
            sb.append(current);
            sb.delete(sb.length() - 1, sb.length());
            current = sb.toString();
            display.setText(current);
            return;
        }
        if (e.getSource() == p) {
            Main.buffer = current;
            current = "";
            Main.operacia = "p";
            display.setText("");
            return;
        }
        if (e.getSource() == s) {
            Double dou = new Double(current);
            double du = Math.sqrt(dou);
            current = du + "";
            display.setText(du + "");
            return;
        }
        if (e.getSource() == r) {
            double du = Math.random();
            current = du + "";
            display.setText(du + "");
            return;
        }
        display.setText(current);
    }

    /**
     * @param main
     */
    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
        } catch (Exception e) {
            System.out.println("Cant get laf");
        }
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

    /*
     * ტოლობით შესასრულებელი ოპერაციები
     */
    public static String operacia(String op1, String op2, String operacia) {
        double d = 0;
        Double d1 = new Double(op1);
        Double d2 = new Double(op2);
        double a1 = d1;
        double a2 = d2;
        if (operacia.equals("add")) {
            d = a1 + a2;
            return d + "";
        }
        if (operacia.equals("sub")) {
            d = a1 - a2;
            return d + "";
        }
        if (operacia.equals("div")) {
            d = a1 / a2;
            return d + "";
        }
        if (operacia.equals("mul")) {
            d = a1 * a2;
            return d + "";
        }
        if (operacia.equals("p")) {
            d = Math.pow(a1, a2);
            return d + "";
        }
        return "mocemuli operacia ar gvaqvs!";
    }
}