Calca
Calca
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (backgroundImage != null) {
g.drawImage(backgroundImage, 0, 0, getWidth(), getHeight(), null);
}
}
}
public Calculator() {
JFrame frame = new JFrame("Hi-Calc911");
frame.setSize(365, 490);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
createNumberButtons(backgroundPanel);
zz = createButton("00", Color.WHITE, 50, 370, 50, 40, backgroundPanel);
b0 = createButton("0", Color.WHITE, 118, 370, 50, 40, backgroundPanel);
dec = createButton(".", Color.WHITE, 190, 370, 50, 40, backgroundPanel);
eql = createButton("=", new Color(229, 204, 255), 260, 370, 50, 40,
backgroundPanel);
mul = createButton("*", new Color(229, 204, 255), 260, 190, 50, 40,
backgroundPanel);
sub = createButton("-", new Color(229, 204, 255), 260, 250, 50, 40,
backgroundPanel);
add = createButton("+", new Color(229, 204, 255), 260, 315, 50, 40,
backgroundPanel);
frame.setVisible(true);
}
if (ae.getSource() == dec) {
txl.setText(txl.getText() + ".");
}
if (ae.getSource() == zz) {
txl.setText(txl.getText() + "00");
}
if (ae.getSource() == bk) {
String text = txl.getText();
if (text.length() > 0) {
txl.setText(text.substring(0, text.length() - 1));
}
}
if (ae.getSource() == cnl) {
txl.setText("");
num1 = num2 = result = 0;
operator = 0;
}
if (ae.getSource() == add) {
performOperation(1);
}
if (ae.getSource() == sub) {
performOperation(2);
}
if (ae.getSource() == mul) {
performOperation(3);
}
if (ae.getSource() == div) {
performOperation(4);
}
if (ae.getSource() == per) {
performOperation(5);
}
if (ae.getSource() == eql) {
try {
num2 = Double.parseDouble(txl.getText());
} catch (NumberFormatException e) {
txl.setText("Enter number first.");
return;
}
switch (operator) {
case 1 -> result = num1 + num2;
case 2 -> result = num1 - num2;
case 3 -> result = num1 * num2;
case 4 -> result = num1 / num2;
case 5 -> result = num1 % num2;
}
txl.setText(String.valueOf(result));
}
}