/** * * * * $RCSfile: CalcApplet.java,v $ * $Date: 2007/01/28 23:15:22 $ * $Revision: 1.6 $ * $State: Exp $ * * 高度情報処理基礎II 授業用プログラム * */ import java.awt.*; import java.awt.event.*; import javax.swing.*; public class CalcApplet extends JApplet implements ActionListener { CalcEngine engine; String digits; Display display; JPanel kp; // key panel JButton plus, minus, mul, div, mod, enter, point; JButton clear, allclear, pi, e, E, BS, sqrt, sin, cos, tan, plmi; JButton[] tenkey; public static void main(String[] args) { JFrame frame = new JFrame("Calulator"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JApplet applet = new CalcApplet(); applet.init(); frame.add(applet); frame.setSize(300, 300); frame.setVisible(true); //frame.show(); applet.start(); } public void init() { engine = new CalcEngine(); digits = ""; display = new Display(); kp = new JPanel(); //Container pane = getContentPane(); //pane.setLayout(new BorderLayout()); //pane.add(display, BorderLayout.NORTH); //pane.add(kp, BorderLayout.CENTER); this.setLayout(new BorderLayout()); this.add(display, BorderLayout.NORTH); this.add(kp, BorderLayout.CENTER); tenkey = new JButton[10]; for (int i = 0; i < 10; i++) { tenkey[i] = new JButton("" + i); tenkey[i].addActionListener(this); } plus = new JButton("+"); plus.addActionListener(this); minus = new JButton("-"); minus.addActionListener(this); mul = new JButton("*"); mul.addActionListener(this); div = new JButton("/"); div.addActionListener(this); mod = new JButton("(mod)"); mod.addActionListener(this); enter = new JButton("="); enter.addActionListener(this); point = new JButton("."); point.addActionListener(this); pi = new JButton("π(PI)"); pi.addActionListener(this); clear = new JButton("C"); clear.addActionListener(this); allclear = new JButton("AC"); allclear.addActionListener(this); e = new JButton("e"); e.addActionListener(this); E = new JButton("E"); E.addActionListener(this); BS = new JButton("BS"); BS.addActionListener(this); sqrt = new JButton("√"); sqrt.addActionListener(this); sin = new JButton("sin°"); sin.addActionListener(this); cos = new JButton("cos°"); cos.addActionListener(this); tan = new JButton("tan°"); tan.addActionListener(this); plmi = new JButton("±"); plmi.addActionListener(this); kp.setLayout(new GridLayout(8, 4)); kp.add(allclear); kp.add(clear) ; kp.add(plmi); kp.add(BS); kp.add(pi); kp.add(e); kp.add(sqrt); kp.add(mod); kp.add(sin); kp.add(cos); kp.add(tan); kp.add(div); kp.add(new JButton("")); kp.add(new JButton("")); kp.add(new JButton("")); kp.add(new JButton("")); kp.add(tenkey[7]); kp.add(tenkey[8]); kp.add(tenkey[9]); kp.add(mul); kp.add(tenkey[4]); kp.add(tenkey[5]); kp.add(tenkey[6]); kp.add(minus); kp.add(tenkey[1]); kp.add(tenkey[2]); kp.add(tenkey[3]); kp.add(plus); kp.add(tenkey[0]); kp.add(point); kp.add(E); kp.add(enter); } public void actionPerformed(ActionEvent evt) { // まず、数字と小数点の場合の処理 for (int i = 0; i < 10; i++) { if (evt.getSource() == tenkey[i]) { digits += i; // 文字列 digits の後ろに追加 display.setText(digits); return; } } if (evt.getSource() == point) { digits += "."; display.setText(digits); return; } else if (evt.getSource() == pi) { // 円周率の入力 digits = "" +Math.PI; display.setText(digits); return; } else if (evt.getSource() == e) { // 自然対数の入力 digits = "" + Math.E; display.setText(digits); return; } else if (evt.getSource() == clear) { digits = ""; display.setText(digits); return; } else if (evt.getSource() == allclear) { engine.reset(); digits = ""; display.setText(digits); return; } else if (evt.getSource() == E) { digits += "E"; display.setText(digits); return; } else if (evt.getSource() == BS){ if(digits.length() == 0){ return; } digits = digits.substring(0,digits.length() -1); display.setText(digits); return; } else if (evt.getSource() == plmi){ if(digits.length() == 0){ return; } if(digits.charAt(digits.length() -1) == 'E') { digits = digits + "-"; } else if(digits.charAt(0) == '-'){ digits = digits.substring(1); } else { digits = "-" + digits; } display.setText(digits); return; } // そうでなければ、演算ボタンか "=" // その演算処理をする前に、数が入力されていたら、レジスタに登録 if (! digits.equals("")) { try { engine.register(Double.parseDouble(digits)); digits = ""; } catch (NumberFormatException e) { display.setText("?"); digits = ""; return; } } double ans = 0.0; if (evt.getSource() == enter) { ans = engine.getAnswer(); } else if (evt.getSource() == plus) { ans = engine.operate(CalcEngine.ADD); } else if (evt.getSource() == minus) { ans = engine.operate(CalcEngine.SUB); } else if (evt.getSource() == mul) { ans = engine.operate(CalcEngine.MUL); } else if (evt.getSource() == div) { ans = engine.operate(CalcEngine.DIV); } else if (evt.getSource() == mod) { ans = engine.operate(CalcEngine.MOD); } else if (evt.getSource() == sqrt) { ans = engine.function(CalcEngine.sqrt); } else if (evt.getSource() == sin) { ans = engine.function(CalcEngine.sin); } else if (evt.getSource() == cos) { ans = engine.function(CalcEngine.cos); } else if (evt.getSource() == tan) { ans = engine.function(CalcEngine.tan); } display.setText("" + ans); } } class Display extends JLabel { Display() { super("0", RIGHT); setFont(new Font("Monospaced", Font.PLAIN, 24)); } public void setText(String text) { if (text.equals("")) { super.setText("0"); } else { super.setText(text); } } }