Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 63 additions & 2 deletions part1/app/ButtonPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,68 @@

import javax.swing.JPanel;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionListener;


public class ButtonPanel extends JPanel{


private static final String[] BUTTONS = {
"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", ".", "=", "+",
"C", "(", ")", "b"
};

private boolean justEvaluated = false;

public ButtonPanel(DisplayPanel display) {
setLayout(new GridLayout(5, 4, 5, 5));
setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

ActionListener listener = e -> {
String cmd = ((JButton) e.getSource()).getText();

switch (cmd) {
case "=":
String expr = display.getExpression();
String result = EvalUtil.eval(expr);
display.setExpression(result);
justEvaluated = true;
break;
case "C":
display.setExpression("");
justEvaluated = false;
break;
case "b":
if (!display.getExpression().isEmpty()){
display.setExpression(display.getExpression().substring(0, display.getExpression().length() - 1));
}
justEvaluated = false;
break;
default:
if (justEvaluated && (
cmd.matches("[0-9]") || ".".equals(cmd) || "(".equals(cmd) || ")".equals(cmd)))
{
display.setExpression("");
}
justEvaluated = false;

display.setExpression(display.getExpression() + cmd);
break;
}
};

for (String label : BUTTONS) {
JButton button = new JButton(label);
button.setFont(new Font("Arial", Font.PLAIN, 18));
button.addActionListener(listener);
add(button);
}
}
}


18 changes: 17 additions & 1 deletion part1/app/CalculatorApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@

import javax.swing.JFrame;

import java.awt.BorderLayout;
public class CalculatorApp extends JFrame{


public CalculatorApp() {
setTitle("Graphical Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 400);
setLocationRelativeTo(null);

DisplayPanel display = new DisplayPanel();
ButtonPanel buttons = new ButtonPanel(display);

setLayout(new BorderLayout());
add(display, BorderLayout.NORTH);
add(buttons, BorderLayout.CENTER);

setVisible(true);
}
}
24 changes: 23 additions & 1 deletion part1/app/DisplayPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,28 @@

import javax.swing.JPanel;

import javax.swing.BorderFactory;
import javax.swing.JTextField;
import java.awt.Font;
import java.awt.BorderLayout;
import java.awt.Dimension;

public class DisplayPanel extends JPanel{

private JTextField displayField;

public DisplayPanel() {
setLayout(new BorderLayout());
displayField = new JTextField();
setPreferredSize(new Dimension(280, 70));
displayField.setFont(new Font("Arial", Font.PLAIN, 24));
setBorder(BorderFactory.createEmptyBorder(5,10,5,10));
displayField.setHorizontalAlignment(JTextField.LEFT);
add(displayField, BorderLayout.CENTER);
}
public String getExpression() {
return displayField.getText();
}
public void setExpression(String expr) {
displayField.setText(expr);
}
}
53 changes: 52 additions & 1 deletion part2/app/ControlPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,57 @@

import javax.swing.JPanel;

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


public class ControlPanel extends JPanel {


private JTextField widthField, heightField; // Input fields for width and height
private JButton scaleButton; // Button to trigger the scaling action
private ShowPanel showPanel; // Reference to the ShowPanel

public ControlPanel(ShowPanel showPanel) {
this.showPanel = showPanel; // Set the reference to the ShowPanel
setLayout(new FlowLayout()); // Use FlowLayout for arranging components
setBackground(new Color(240, 240, 240)); // Set background color for the panel
setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); // Add padding around the panel

widthField = new JTextField(5);
heightField = new JTextField(5);
scaleButton = new JButton("Scale Image");

add(new JLabel("Width:"));
add(widthField);
add(new JLabel("Height:"));
add(heightField);
add(scaleButton);

scaleButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
int width = Integer.parseInt(widthField.getText());
int height = Integer.parseInt(heightField.getText());

// Get the dimensions of the ShowPanel
int maxWidth = showPanel.getWidth();
int maxHeight = showPanel.getHeight();

// Check if the entered size is larger than the panel's size
if (width > maxWidth || height > maxHeight) {
JOptionPane.showMessageDialog(null, "Entered size cannot be larger than the panel size!");
} else {
// Reload the image and scale it
showPanel.loadImage(); // Reload the image
showPanel.scaleImage(width, height); // Scale the image
}
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "Please enter valid numbers for width and height"); // Show error message
}
}
});
}
}
23 changes: 22 additions & 1 deletion part2/app/MainFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,28 @@

import javax.swing.JFrame;

import javax.swing.*;
import java.awt.*;


public class MainFrame extends JFrame {

private ShowPanel showPanel; // Panel for displaying the image
private ControlPanel controlPanel; // Panel for controlling the image size

public MainFrame() {
setTitle("Image Scaler");
setSize(600, 600);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());

showPanel = new ShowPanel();
controlPanel = new ControlPanel(showPanel);

add(showPanel, BorderLayout.CENTER);
add(controlPanel, BorderLayout.SOUTH);

setVisible(true);
}
}

43 changes: 42 additions & 1 deletion part2/app/ShowPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,49 @@

import javax.swing.JPanel;

import javax.swing.*;
import java.awt.*;
import java.io.*;
import javax.imageio.ImageIO;

public class ShowPanel extends JPanel {

private Image image;
private String imagePath = "./images/img.png";

public ShowPanel() {
setLayout(new BorderLayout());
setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));

loadImage();
}

public void loadImage() {
try {
image = ImageIO.read(getClass().getResource(imagePath));
} catch (IOException e) {
e.printStackTrace();
}
repaint(); // Redraw the panel
}

public void scaleImage(int width, int height) {
if (image != null) {
Image scaledImage = image.getScaledInstance(width, height, Image.SCALE_SMOOTH); // Scale image
image = scaledImage;
repaint(); // Redraw the panel
}
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (image != null) {
// Calculate the x and y coordinates to center the image
int x = (getWidth() - image.getWidth(null)) / 2;
int y = (getHeight() - image.getHeight(null)) / 2;
g.drawImage(image, x, y, this); // Draw the image at the calculated position
}
}
}