[go: up one dir, main page]

0% found this document useful (0 votes)
35 views4 pages

Calca

TRUEHJD
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views4 pages

Calca

TRUEHJD
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

package calculator;

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;

class BackgroundPanel extends JPanel {


private BufferedImage backgroundImage;

public BackgroundPanel(String imagePath) {


try {
backgroundImage = ImageIO.read(new File(imagePath));
} catch (IOException e) {
e.printStackTrace();
}
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

if (backgroundImage != null) {
g.drawImage(backgroundImage, 0, 0, getWidth(), getHeight(), null);
}
}
}

public class Calculator implements ActionListener {


private JTextField txl;
private JButton[] numberButtons = new JButton[10];
private JButton cnl, bk, per, div, mul, sub, add, eql, dec, zz;
private double num1, num2, result;
private int operator;

public Calculator() {
JFrame frame = new JFrame("Hi-Calc911");
frame.setSize(365, 490);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);

BackgroundPanel backgroundPanel = new BackgroundPanel("C:\\Users\\


Administrator\\Downloads\\kuromi.jpg");
frame.setContentPane(backgroundPanel);
backgroundPanel.setLayout(null);

txl = new JTextField();


txl.setOpaque(true);
txl.setFont(new Font("Cascadia Code", Font.BOLD, 20));
txl.setBackground(Color.LIGHT_GRAY);
txl.setBorder(BorderFactory.createLineBorder(Color.WHITE));
txl.setHorizontalAlignment(JTextField.CENTER);
txl.setBounds(50, 50, 265, 60);
backgroundPanel.add(txl);

cnl = createButton("AC", Color.YELLOW, 50, 130, 50, 40, backgroundPanel);


per = createButton("%", new Color(229, 204, 255), 118, 130, 50, 40,
backgroundPanel);
bk = createButton("<", new Color(229, 204, 255), 190, 130, 50, 40,
backgroundPanel);
div = createButton("/", new Color(229, 204, 255), 260, 130, 50, 40,
backgroundPanel);

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);
}

private JButton createButton(String text, Color bgColor, int x, int y, int


width, int height, JPanel panel) {
JButton button = new JButton(text);
button.setFont(new Font("Cascadia Code", Font.BOLD, 18));
button.setBorder(BorderFactory.createLineBorder(Color.WHITE));
button.setBackground(bgColor);
button.setBounds(x, y, width, height);
button.addActionListener(this);
panel.add(button);
return button;
}

private void createNumberButtons(JPanel panel) {


int x = 50, y = 190;
for (int i = 7; i <= 9; i++) {
numberButtons[i] = createButton(String.valueOf(i), Color.WHITE, x, y,
50, 40, panel);
x += 68;
}
x = 50;
y += 60;
for (int i = 4; i <= 6; i++) {
numberButtons[i] = createButton(String.valueOf(i), Color.WHITE, x, y,
50, 40, panel);
x += 68;
}
x = 50;
y += 65;
for (int i = 1; i <= 3; i++) {
numberButtons[i] = createButton(String.valueOf(i), Color.WHITE, x, y,
50, 40, panel);
x += 68;
}
}
@Override
public void actionPerformed(ActionEvent ae) {
for (int i = 0; i < numberButtons.length; i++) {
if (ae.getSource() == numberButtons[i]) {
txl.setText(txl.getText() + i);
}
}

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));
}
}

private void performOperation(int op) {


try {
num1 = Double.parseDouble(txl.getText());
} catch (NumberFormatException e) {
txl.setText("Invalid Format");
return;
}
txl.setText("");
operator = op;
}

public static void main(String[] args) {


new Calculator();
}
}

You might also like