1
22
23 package com.liferay.applets.calculator;
24
25 import com.liferay.lawt.AppletFrame;
26
27 import java.applet.Applet;
28
29 import java.awt.Button;
30 import java.awt.Color;
31 import java.awt.Dimension;
32 import java.awt.Event;
33 import java.awt.Font;
34 import java.awt.Graphics;
35 import java.awt.GridBagConstraints;
36 import java.awt.GridBagLayout;
37 import java.awt.Insets;
38 import java.awt.Label;
39 import java.awt.Panel;
40 import java.awt.event.ActionEvent;
41 import java.awt.event.ActionListener;
42
43
49 public class Calculator extends Applet {
50
51 public static void main(String args[]) {
52 new AppletFrame(new Calculator(), 600, 400);
53 }
54
55 public void init() {
56 GridBagLayout gbl = new GridBagLayout();
57
58 GridBagConstraints c = new GridBagConstraints();
59 c.fill = GridBagConstraints.BOTH;
60 c.weightx = 1.0;
61 c.weighty = 1.0;
62
63 c.insets = new Insets(2, 2, 2, 2);
64
65 setLayout(gbl);
66
67 _inputLabel = new Label("0", Label.RIGHT);
68 _inputLabel.setBackground(Color.white);
69
70 c.gridwidth = GridBagConstraints.REMAINDER;
71 gbl.setConstraints(_inputLabel, c);
72 c.gridwidth = 1;
73
74 add(_inputLabel);
75
76 ActionListener al = new ActionListener() {
77
78 public void actionPerformed(ActionEvent e) {
79 String s = e.getActionCommand();
80
81 if ("0123456789.".indexOf(s) != -1) {
82 if (_firstDigit) {
83 _firstDigit = false;
84 _inputLabel.setText(s);
85 }
86 else {
87 _inputLabel.setText(_inputLabel.getText() + s);
88 }
89 }
90 else if (s.equals("C")) {
91 _inputLabel.setText("0.0");
92 _value = 0.0f;
93 _firstDigit = true;
94 }
96 else {
97 if (!_firstDigit) {
98 _compute(_inputLabel.getText());
99 _firstDigit = true;
100 }
101
102 _operator = s;
103 }
104 }
105
106 };
107
108 _createButton(al, "7", Color.blue, gbl, c, this);
109 _createButton(al, "8", Color.blue, gbl, c, this);
110 _createButton(al, "9", Color.blue, gbl, c, this);
111
112 c.gridwidth = GridBagConstraints.REMAINDER;
113 _createButton(al, "/", Color.red, gbl, c, this);
114 c.gridwidth = 1;
115
116 _createButton(al, "4", Color.blue, gbl, c, this);
117 _createButton(al, "5", Color.blue, gbl, c, this);
118 _createButton(al, "6", Color.blue, gbl, c, this);
119
120 c.gridwidth = GridBagConstraints.REMAINDER;
121 _createButton(al, "*", Color.red, gbl, c, this);
122 c.gridwidth = 1;
123
124 _createButton(al, "1", Color.blue, gbl, c, this);
125 _createButton(al, "2", Color.blue, gbl, c, this);
126 _createButton(al, "3", Color.blue, gbl, c, this);
127
128 c.gridwidth = GridBagConstraints.REMAINDER;
129 _createButton(al, "-", Color.red, gbl, c, this);
130 c.gridwidth = 1;
131
132 _createButton(al, ".", Color.blue, gbl, c, this);
133 _createButton(al, "0", Color.blue, gbl, c, this);
134 _createButton(al, "=", Color.red, gbl, c, this);
135
136 c.gridwidth = GridBagConstraints.REMAINDER;
137 _createButton(al, "+", Color.red, gbl, c, this);
138
139 _createButton(al, "C", Color.red, gbl, c, this);
140 }
141
142 public Insets getInsets() {
143 return new Insets(5, 5, 5, 5);
144 }
145
146 public boolean keyDown(Event e, int key) {
147 String s = (new Character((char)key)).toString();
148
149 if (key == Event.ENTER) {
150 key = '=';
151 }
152
153 if ((key == Event.ESCAPE) || (key == 'c')) {
154 key = 'C';
155 }
156
157 if ((key == '1') || (key == '2') || (key == '3') || (key == '4') ||
158 (key == '5') || (key == '6') || (key == '7') || (key == '8') ||
159 (key == '9') || (key == '0')) {
160
161 if (_firstDigit) {
162 _firstDigit = false;
163 _inputLabel.setText(s);
164 }
165 else {
166 _inputLabel.setText(_inputLabel.getText() + s);
167 }
168 }
169 else if (key == 'C') {
170 _inputLabel.setText("0.0");
171 _value = 0.0f;
172 _firstDigit = true;
173 }
174 else if ((key == '=') || (key == '+') || (key == '-') ||
175 (key == '*') || (key == '/')) {
176
177 if (!_firstDigit) {
178 _compute(_inputLabel.getText());
179 _firstDigit = true;
180 }
181
182 _operator = s;
183 }
184
185 repaint();
186
187 return true;
188 }
189
190 public void paint(Graphics g) {
191 Dimension d = getSize();
192 g.drawRect(0, 0, d.width - 1, d.height - 1);
193 g.drawLine(0, 0, 0, d.height);
194 }
195
196 private void _createButton(ActionListener al, String label, Color color,
197 GridBagLayout gbl, GridBagConstraints c,
198 Panel panel) {
199
200 Button button = new Button(label);
201 button.setFont(new Font("Arial", Font.PLAIN, 12));
202 button.setForeground(color);
203 button.addActionListener(al);
204
205 gbl.setConstraints(button, c);
206
207 panel.add(button);
208 }
209
210 private void _compute(String s) {
211 float value = Float.valueOf(s).floatValue();
212 char c = _operator.charAt(0);
213
214 if (c == '=') {
215 _value = value;
216 }
217 else if (c == '+') {
218 _value += value;
219 }
220 else if (c == '-') {
221 _value -= value;
222 }
223 else if (c == '*') {
224 _value *= value;
225 }
226 else if (c == '/') {
227 _value /= value;
228 }
229
230 _inputLabel.setText(Float.toString(_value));
231 }
232
233 private Label _inputLabel;
234 private boolean _firstDigit = true;
235 private float _value = 0.0f;
236 private String _operator = "=";
237
238 }