001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.applets.calculator;
022:
023: import com.liferay.lawt.AppletFrame;
024:
025: import java.applet.Applet;
026:
027: import java.awt.Button;
028: import java.awt.Color;
029: import java.awt.Dimension;
030: import java.awt.Event;
031: import java.awt.Font;
032: import java.awt.Graphics;
033: import java.awt.GridBagConstraints;
034: import java.awt.GridBagLayout;
035: import java.awt.Insets;
036: import java.awt.Label;
037: import java.awt.Panel;
038: import java.awt.event.ActionEvent;
039: import java.awt.event.ActionListener;
040:
041: /**
042: * <a href="Calculator.java.html"><b><i>View Source</i></b></a>
043: *
044: * @author Brian Wing Shun Chan
045: *
046: */
047: public class Calculator extends Applet {
048:
049: public static void main(String args[]) {
050: new AppletFrame(new Calculator(), 600, 400);
051: }
052:
053: public void init() {
054: GridBagLayout gbl = new GridBagLayout();
055:
056: GridBagConstraints c = new GridBagConstraints();
057: c.fill = GridBagConstraints.BOTH;
058: c.weightx = 1.0;
059: c.weighty = 1.0;
060:
061: c.insets = new Insets(2, 2, 2, 2);
062:
063: setLayout(gbl);
064:
065: _inputLabel = new Label("0", Label.RIGHT);
066: _inputLabel.setBackground(Color.white);
067:
068: c.gridwidth = GridBagConstraints.REMAINDER;
069: gbl.setConstraints(_inputLabel, c);
070: c.gridwidth = 1;
071:
072: add(_inputLabel);
073:
074: ActionListener al = new ActionListener() {
075:
076: public void actionPerformed(ActionEvent e) {
077: String s = e.getActionCommand();
078:
079: if ("0123456789.".indexOf(s) != -1) {
080: if (_firstDigit) {
081: _firstDigit = false;
082: _inputLabel.setText(s);
083: } else {
084: _inputLabel.setText(_inputLabel.getText() + s);
085: }
086: } else if (s.equals("C")) {
087: _inputLabel.setText("0.0");
088: _value = 0.0f;
089: _firstDigit = true;
090: //_operator = "=";
091: } else {
092: if (!_firstDigit) {
093: _compute(_inputLabel.getText());
094: _firstDigit = true;
095: }
096:
097: _operator = s;
098: }
099: }
100:
101: };
102:
103: _createButton(al, "7", Color.blue, gbl, c, this );
104: _createButton(al, "8", Color.blue, gbl, c, this );
105: _createButton(al, "9", Color.blue, gbl, c, this );
106:
107: c.gridwidth = GridBagConstraints.REMAINDER;
108: _createButton(al, "/", Color.red, gbl, c, this );
109: c.gridwidth = 1;
110:
111: _createButton(al, "4", Color.blue, gbl, c, this );
112: _createButton(al, "5", Color.blue, gbl, c, this );
113: _createButton(al, "6", Color.blue, gbl, c, this );
114:
115: c.gridwidth = GridBagConstraints.REMAINDER;
116: _createButton(al, "*", Color.red, gbl, c, this );
117: c.gridwidth = 1;
118:
119: _createButton(al, "1", Color.blue, gbl, c, this );
120: _createButton(al, "2", Color.blue, gbl, c, this );
121: _createButton(al, "3", Color.blue, gbl, c, this );
122:
123: c.gridwidth = GridBagConstraints.REMAINDER;
124: _createButton(al, "-", Color.red, gbl, c, this );
125: c.gridwidth = 1;
126:
127: _createButton(al, ".", Color.blue, gbl, c, this );
128: _createButton(al, "0", Color.blue, gbl, c, this );
129: _createButton(al, "=", Color.red, gbl, c, this );
130:
131: c.gridwidth = GridBagConstraints.REMAINDER;
132: _createButton(al, "+", Color.red, gbl, c, this );
133:
134: _createButton(al, "C", Color.red, gbl, c, this );
135: }
136:
137: public Insets getInsets() {
138: return new Insets(5, 5, 5, 5);
139: }
140:
141: public boolean keyDown(Event e, int key) {
142: String s = (new Character((char) key)).toString();
143:
144: if (key == Event.ENTER) {
145: key = '=';
146: }
147:
148: if ((key == Event.ESCAPE) || (key == 'c')) {
149: key = 'C';
150: }
151:
152: if ((key == '1') || (key == '2') || (key == '3')
153: || (key == '4') || (key == '5') || (key == '6')
154: || (key == '7') || (key == '8') || (key == '9')
155: || (key == '0')) {
156:
157: if (_firstDigit) {
158: _firstDigit = false;
159: _inputLabel.setText(s);
160: } else {
161: _inputLabel.setText(_inputLabel.getText() + s);
162: }
163: } else if (key == 'C') {
164: _inputLabel.setText("0.0");
165: _value = 0.0f;
166: _firstDigit = true;
167: } else if ((key == '=') || (key == '+') || (key == '-')
168: || (key == '*') || (key == '/')) {
169:
170: if (!_firstDigit) {
171: _compute(_inputLabel.getText());
172: _firstDigit = true;
173: }
174:
175: _operator = s;
176: }
177:
178: repaint();
179:
180: return true;
181: }
182:
183: public void paint(Graphics g) {
184: Dimension d = getSize();
185: g.drawRect(0, 0, d.width - 1, d.height - 1);
186: g.drawLine(0, 0, 0, d.height);
187: }
188:
189: private void _createButton(ActionListener al, String label,
190: Color color, GridBagLayout gbl, GridBagConstraints c,
191: Panel panel) {
192:
193: Button button = new Button(label);
194: button.setFont(new Font("Arial", Font.PLAIN, 12));
195: button.setForeground(color);
196: button.addActionListener(al);
197:
198: gbl.setConstraints(button, c);
199:
200: panel.add(button);
201: }
202:
203: private void _compute(String s) {
204: float value = Float.valueOf(s).floatValue();
205: char c = _operator.charAt(0);
206:
207: if (c == '=') {
208: _value = value;
209: } else if (c == '+') {
210: _value += value;
211: } else if (c == '-') {
212: _value -= value;
213: } else if (c == '*') {
214: _value *= value;
215: } else if (c == '/') {
216: _value /= value;
217: }
218:
219: _inputLabel.setText(Float.toString(_value));
220: }
221:
222: private Label _inputLabel;
223: private boolean _firstDigit = true;
224: private float _value = 0.0f;
225: private String _operator = "=";
226:
227: }
|