NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

Graphics Calculator
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Calculator extends JFrame implements ActionListener {
private JTextField display;
private double num1, num2, result;
private char operator;

public Calculator() {
setUndecorated(true);
setSize(350, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);


JPanel titleBar = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
GradientPaint gradient = new GradientPaint(0, 0, Color.PINK, getWidth(), 0, Color.ORANGE);
g2d.setPaint(gradient);
g2d.fillRect(0, 0, getWidth(), getHeight());
}
};
titleBar.setPreferredSize(new Dimension(getWidth(), 40));
titleBar.setLayout(new BorderLayout());


JLabel titleLabel = new JLabel(" Calculator");
titleLabel.setFont(new Font("Arial", Font.BOLD, 18));
titleLabel.setForeground(Color.WHITE);
titleBar.add(titleLabel, BorderLayout.WEST);


JButton closeButton = new JButton("X");
closeButton.setForeground(Color.WHITE);
closeButton.setBackground(Color.RED);
closeButton.setFocusPainted(false);
closeButton.setBorderPainted(false);
closeButton.addActionListener(e -> System.exit(0));
titleBar.add(closeButton, BorderLayout.EAST);

display = new JTextField();
display.setEditable(false);
display.setFont(new Font("Arial", Font.BOLD, 28));
display.setHorizontalAlignment(JTextField.RIGHT);
display.setBackground(Color.WHITE);
display.setForeground(Color.BLACK);

JPanel mainPanel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
GradientPaint gradient = new GradientPaint(0, 0, Color.CYAN, getWidth(), getHeight(), Color.BLUE);
g2d.setPaint(gradient);
g2d.fillRect(0, 0, getWidth(), getHeight());
}
};
mainPanel.setLayout(new BorderLayout(10, 10));


JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(5, 4, 10, 10));
buttonPanel.setOpaque(false);

String[] buttons = {
"C", "√", "x²", "/",
"7", "8", "9", "x",
"4", "5", "6", "-",
"1", "2", "3", "+",
"0", ".", "=",
};

for (String text : buttons) {
JButton button = new JButton(text);
button.setFont(new Font("Arial", Font.BOLD, 18));
button.setBackground(Color.LIGHT_GRAY);
button.setForeground(Color.DARK_GRAY);
button.setFocusPainted(false);
button.addActionListener(this);
buttonPanel.add(button);
}


mainPanel.add(display, BorderLayout.NORTH);
mainPanel.add(buttonPanel, BorderLayout.CENTER);


getContentPane().setLayout(new BorderLayout());
getContentPane().add(titleBar, BorderLayout.NORTH);
getContentPane().add(mainPanel, BorderLayout.CENTER);

setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();

switch (command) {
case "C":
display.setText("");
num1 = num2 = result = 0;
operator = '';
break;
case "√":
num1 = Double.parseDouble(display.getText());
result = Math.sqrt(num1);
display.setText(String.valueOf(result));
break;
case "x²":
num1 = Double.parseDouble(display.getText());
result = Math.pow(num1, 2);
display.setText(String.valueOf(result));
break;
case "+":
case "-":
case "x":
case "/":
num1 = Double.parseDouble(display.getText());
operator = command.charAt(0);
display.setText("");
break;
case "=":
num2 = Double.parseDouble(display.getText());
calculateResult();
display.setText(String.valueOf(result));
break;
case ".":
if (!display.getText().contains(".")) {
display.setText(display.getText() + ".");
}
break;
default:
display.setText(display.getText() + command);
break;
}
}

private void calculateResult() {
switch (operator) {
case '+': result = num1 + num2; break;
case '-': result = num1 - num2; break;
case 'x': result = num1 * num2; break;
case '/': result = num2 != 0 ? num1 / num2 : 0; break;
}
}

public static void main(String[] args) {
new Calculator();
}
}



Normal Calculator
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Calculator implements ActionListener {

JFrame frame;
JTextField textfield;
JButton[] numberButtons = new JButton[10];
JButton[] functionButtons = new JButton[9];
JButton addButton, subButton, mulButton, divButton;
JButton decButton, equButton, delButton, clrButton, negButton;
JPanel panel;

Font myFont = new Font("Ink Free", Font.BOLD, 30);

double num1 = 0, num2 = 0, result = 0;
char operator;

Calculator() {
frame = new JFrame("Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(420, 550);
frame.setLayout(null);

textfield = new JTextField();
textfield.setBounds(50, 25, 300, 50);
textfield.setFont(myFont);
textfield.setEditable(false);

addButton = new JButton("+");
subButton = new JButton("-");
mulButton = new JButton("*");
divButton = new JButton("/");
decButton = new JButton(".");
equButton = new JButton("=");
delButton = new JButton("Delete");
negButton = new JButton("(-)");
clrButton = new JButton("Clear");

functionButtons[0] = addButton;
functionButtons[1] = subButton;
functionButtons[2] = mulButton;
functionButtons[3] = divButton;
functionButtons[4] = decButton;
functionButtons[5] = equButton;
functionButtons[6] = delButton;
functionButtons[7] = clrButton;
functionButtons[8] = negButton;

for (int i = 0; i < 9; i++) {
functionButtons[i].addActionListener(this);
functionButtons[i].setFont(myFont);
functionButtons[i].setFocusable(false);
}

for (int i = 0; i < 10; i++) {
numberButtons[i] = new JButton(String.valueOf(i));
numberButtons[i].addActionListener(this);
numberButtons[i].setFont(myFont);
numberButtons[i].setFocusable(false);
}

negButton.setBounds(50, 430, 100, 50);
delButton.setBounds(150, 430, 100, 50);
clrButton.setBounds(250, 430, 100, 50);

panel = new JPanel();
panel.setBounds(50, 100, 300, 300);
panel.setLayout(new GridLayout(4, 4, 10, 10));

panel.add(numberButtons[1]);
panel.add(numberButtons[2]);
panel.add(numberButtons[3]);
panel.add(addButton);
panel.add(numberButtons[4]);
panel.add(numberButtons[5]);
panel.add(numberButtons[6]);
panel.add(subButton);
panel.add(numberButtons[7]);
panel.add(numberButtons[8]);
panel.add(numberButtons[9]);
panel.add(mulButton);
panel.add(decButton);
panel.add(numberButtons[0]);
panel.add(equButton);
panel.add(divButton);

frame.add(panel);
frame.add(negButton);
frame.add(delButton);
frame.add(clrButton);
frame.add(textfield);

frame.setVisible(true);
}

public static void main(String[] args) {
Calculator c = new Calculator();
}

@Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < 10; i++) {
if (e.getSource() == numberButtons[i]) {
textfield.setText(textfield.getText().concat(String.valueOf(i)));
}
}
if (e.getSource() == decButton) {
textfield.setText(textfield.getText().concat("."));
}
if (e.getSource() == addButton) {
num1 = Double.parseDouble(textfield.getText());
operator = '+';
textfield.setText("");
}
if (e.getSource() == subButton) {
num1 = Double.parseDouble(textfield.getText());
operator = '-';
textfield.setText("");
}
if (e.getSource() == mulButton) {
num1 = Double.parseDouble(textfield.getText());
operator = '*';
textfield.setText("");
}
if (e.getSource() == divButton) {
num1 = Double.parseDouble(textfield.getText());
operator = '/';
textfield.setText("");
}
if (e.getSource() == equButton) {
num2 = Double.parseDouble(textfield.getText());
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
}
textfield.setText(String.valueOf(result));
num1 = result;
}
if (e.getSource() == clrButton) {
textfield.setText("");
}
if (e.getSource() == delButton) {
String string = textfield.getText();
textfield.setText(string.length() > 0 ? string.substring(0, string.length() - 1) : "");
}
if (e.getSource() == negButton) {
double temp = Double.parseDouble(textfield.getText());
temp *= -1;
textfield.setText(String.valueOf(temp));
}
}
}

     
 
what is notes.io
 

Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 14 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.