From c1a95904d6396c9a570115bd55d36b7a0570e938 Mon Sep 17 00:00:00 2001 From: Notebook Date: Sat, 17 May 2025 01:51:40 +0330 Subject: [PATCH 1/3] part 1 completed! --- part1/Launcher.java | 4 +- part1/app/ButtonPanel.java | 49 +++++++++++++- part1/app/CalculatorApp.java | 27 +++++++- part1/app/DisplayPanel.java | 32 +++++++++- part1/app/EvalUtil.java | 120 +++++++++++++++++------------------ 5 files changed, 165 insertions(+), 67 deletions(-) diff --git a/part1/Launcher.java b/part1/Launcher.java index 24e9283..033065b 100644 --- a/part1/Launcher.java +++ b/part1/Launcher.java @@ -7,8 +7,8 @@ public class Launcher { public static void main(String[] args) { SwingUtilities.invokeLater(() -> { - new CalculatorApp(); + new CalculatorApp(); }); } - + } diff --git a/part1/app/ButtonPanel.java b/part1/app/ButtonPanel.java index 7986fb3..ab17562 100644 --- a/part1/app/ButtonPanel.java +++ b/part1/app/ButtonPanel.java @@ -1,8 +1,53 @@ package part1.app; import javax.swing.JPanel; +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" + }; + 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(); + + if ("=".equals(cmd)) { + // Evaluate expression and display result + String expr = display.getExpression(); + String result = EvalUtil.eval(expr); + display.setExpression(result); + } else if ("C".equals(cmd)) { + // Clear the display + display.setExpression(""); + } else if ("b".equals(cmd)){ + if (display.getExpression().length() > 0){ + display.setExpression(display.getExpression().substring(0, display.getExpression().length() - 1)); + } + } else { + // Append digit/operator to display + display.setExpression(display.getExpression() + cmd); + } + }; + + // Create and add buttons + for (String label : BUTTONS) { + JButton button = new JButton(label); + button.setFont(new Font("Arial", Font.PLAIN, 18)); + button.addActionListener(listener); + add(button); + } + } + } diff --git a/part1/app/CalculatorApp.java b/part1/app/CalculatorApp.java index d18238e..e2eef9c 100644 --- a/part1/app/CalculatorApp.java +++ b/part1/app/CalculatorApp.java @@ -1,7 +1,32 @@ package part1.app; import javax.swing.JFrame; +import java.awt.BorderLayout; + public class CalculatorApp extends JFrame{ + + public CalculatorApp() { + // Create main window + setTitle("Graphical Calculator"); + + // Configure window + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setSize(300, 400); + setLocationRelativeTo(null); + + // Initialize components + DisplayPanel display = new DisplayPanel(); + ButtonPanel buttons = new ButtonPanel(display); + + // Layout setup + setLayout(new BorderLayout()); + add(display, BorderLayout.NORTH); + add(buttons, BorderLayout.CENTER); + + // Show window + setVisible(true); + } + } -} + diff --git a/part1/app/DisplayPanel.java b/part1/app/DisplayPanel.java index 77a48a5..70dba64 100644 --- a/part1/app/DisplayPanel.java +++ b/part1/app/DisplayPanel.java @@ -1,7 +1,35 @@ package part1.app; import javax.swing.JPanel; +import javax.swing.BorderFactory; +import javax.swing.JPanel; +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(); + + // Configure text field + setPreferredSize(new Dimension(280, 70)); + setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 10)); + displayField.setFont(new Font("Arial", Font.PLAIN, 24)); + displayField.setHorizontalAlignment(JTextField.LEFT); + add(displayField, BorderLayout.CENTER); + } + + // Returns the current expression from the display + public String getExpression() { + return displayField.getText(); + } + + // Updates the display with the provided expression + public void setExpression(String expr) { + displayField.setText(expr); + } +} \ No newline at end of file diff --git a/part1/app/EvalUtil.java b/part1/app/EvalUtil.java index b8a4d55..0167773 100644 --- a/part1/app/EvalUtil.java +++ b/part1/app/EvalUtil.java @@ -5,76 +5,76 @@ public class EvalUtil { // Evaluates the expression or returns "Error" on failure public static String eval(String expression) { // Local parser class inside the method - class Parser { - final String s = expression; - int pos = -1, ch; + class Parser { + final String s = expression; + int pos = -1, ch; - void nextChar() { - pos++; - ch = (pos < s.length() ? s.charAt(pos) : -1); - } + void nextChar() { + pos++; + ch = (pos < s.length() ? s.charAt(pos) : -1); + } - boolean eat(int charToEat) { - while (ch == ' ') nextChar(); - if (ch == charToEat) { - nextChar(); - return true; + boolean eat(int charToEat) { + while (ch == ' ') nextChar(); + if (ch == charToEat) { + nextChar(); + return true; + } + return false; } - return false; - } - double parse() { - nextChar(); - double x = parseExpression(); - if (pos < s.length()) - throw new RuntimeException("Unexpected: " + (char)ch); - return x; - } + double parse() { + nextChar(); + double x = parseExpression(); + if (pos < s.length()) + throw new RuntimeException("Unexpected: " + (char)ch); + return x; + } - double parseExpression() { - double x = parseTerm(); - for (;;) { - if (eat('+')) x += parseTerm(); - else if (eat('-')) x -= parseTerm(); - else return x; + double parseExpression() { + double x = parseTerm(); + for (;;) { + if (eat('+')) x += parseTerm(); + else if (eat('-')) x -= parseTerm(); + else return x; + } } - } - double parseTerm() { - double x = parseFactor(); - for (;;) { - if (eat('*')) x *= parseFactor(); - else if (eat('/')) x /= parseFactor(); - else return x; + double parseTerm() { + double x = parseFactor(); + for (;;) { + if (eat('*')) x *= parseFactor(); + else if (eat('/')) x /= parseFactor(); + else return x; + } } - } - double parseFactor() { - if (eat('+')) return parseFactor(); // unary + - if (eat('-')) return -parseFactor(); // unary – - - double x; - int start = this.pos; - if (eat('(')) { - x = parseExpression(); - if (!eat(')')) throw new RuntimeException("Missing )"); - } else if ((ch >= '0' && ch <= '9') || ch == '.') { - while ((ch >= '0' && ch <= '9') || ch == '.') nextChar(); - x = Double.parseDouble(s.substring(start, this.pos)); - } else { - throw new RuntimeException("Unexpected: " + (char)ch); + double parseFactor() { + if (eat('+')) return parseFactor(); // unary + + if (eat('-')) return -parseFactor(); // unary – + + double x; + int start = this.pos; + if (eat('(')) { + x = parseExpression(); + if (!eat(')')) throw new RuntimeException("Missing )"); + } else if ((ch >= '0' && ch <= '9') || ch == '.') { + while ((ch >= '0' && ch <= '9') || ch == '.') nextChar(); + x = Double.parseDouble(s.substring(start, this.pos)); + } else { + throw new RuntimeException("Unexpected: " + (char)ch); + } + return x; } - return x; } - } - try { - double result = new Parser().parse(); - // strip trailing .0 for integers - if (result == (long)result) return Long.toString((long)result); - else return Double.toString(result); - } catch (Exception e) { - return "Error"; - } + try { + double result = new Parser().parse(); + // strip trailing .0 for integers + if (result == (long)result) return Long.toString((long)result); + else return Double.toString(result); + } catch (Exception e) { + return "Error"; + } } -} +} \ No newline at end of file From a8c54d69ba9900ee2abb31f35ed6c8f61d34ea3b Mon Sep 17 00:00:00 2001 From: Notebook Date: Sat, 17 May 2025 14:16:06 +0330 Subject: [PATCH 2/3] part 2 completed --- part2/Launcher.java | 7 +++-- part2/app/ControlPanel.java | 56 +++++++++++++++++++++++++++++++++++-- part2/app/MainFrame.java | 25 +++++++++++++++-- part2/app/ShowPanel.java | 46 ++++++++++++++++++++++++++++-- 4 files changed, 122 insertions(+), 12 deletions(-) diff --git a/part2/Launcher.java b/part2/Launcher.java index 2a36cb6..a312aef 100644 --- a/part2/Launcher.java +++ b/part2/Launcher.java @@ -1,13 +1,14 @@ package part2; +// Import necessary classes for Swing utilities and MainFrame import javax.swing.SwingUtilities; - import part2.app.MainFrame; public class Launcher { public static void main(String[] args) { + // Launch the MainFrame on the Event Dispatch Thread SwingUtilities.invokeLater(() -> { - new MainFrame(); + new MainFrame(); // Create and show the main frame }); } -} +} \ No newline at end of file diff --git a/part2/app/ControlPanel.java b/part2/app/ControlPanel.java index 7765db0..e9852e3 100644 --- a/part2/app/ControlPanel.java +++ b/part2/app/ControlPanel.java @@ -1,7 +1,57 @@ package part2.app; -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 + + // Create text fields for width and height + widthField = new JTextField(5); + heightField = new JTextField(5); + scaleButton = new JButton("Scale Image"); + + // Add components to the panel + add(new JLabel("Width:")); + add(widthField); + add(new JLabel("Height:")); + add(heightField); + add(scaleButton); + +// Button action listener to scale the image + scaleButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + try { + int width = Integer.parseInt(widthField.getText()); // Get the entered width + int height = Integer.parseInt(heightField.getText()); // Get the entered height + + // 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 + } + } + }); + } +} \ No newline at end of file diff --git a/part2/app/MainFrame.java b/part2/app/MainFrame.java index 9a56612..d7c6a28 100644 --- a/part2/app/MainFrame.java +++ b/part2/app/MainFrame.java @@ -1,8 +1,27 @@ package part2.app; -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); // Set window size + setLocationRelativeTo(null); // Center the frame + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Close on exit + setLayout(new BorderLayout()); // Use BorderLayout for panels + + // Initialize the panels + showPanel = new ShowPanel(); + controlPanel = new ControlPanel(showPanel); + + // Add panels to the frame + add(showPanel, BorderLayout.CENTER); // Add the ShowPanel to the center + add(controlPanel, BorderLayout.SOUTH); // Add the ControlPanel to the bottom + + setVisible(true); // Make the frame visible + } +} \ No newline at end of file diff --git a/part2/app/ShowPanel.java b/part2/app/ShowPanel.java index a4309ad..de216ec 100644 --- a/part2/app/ShowPanel.java +++ b/part2/app/ShowPanel.java @@ -1,9 +1,49 @@ package part2.app; -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; // Image object to hold the loaded image + private String imagePath = "./images/img.png"; // Path to the image file + public ShowPanel() { + setLayout(new BorderLayout()); // Set layout to BorderLayout + setBorder(BorderFactory.createLineBorder(Color.BLACK, 2)); // Add border to the panel + loadImage(); // Load the image initially + } + + // Method to load the image from the specified path + public void loadImage() { + try { + // Load the image from the file system + image = ImageIO.read(getClass().getResource(imagePath)); + } catch (IOException e) { + e.printStackTrace(); // Print any error encountered during loading + } + repaint(); // Redraw the panel + } + + // Method to scale the image to a specified width and height + 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 the paintComponent method to center the image on the panel + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); // Call the superclass method + 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 + } + } +} \ No newline at end of file From 883424c665faafb40b23729c42ac69426da40193 Mon Sep 17 00:00:00 2001 From: Notebook Date: Sat, 17 May 2025 14:29:02 +0330 Subject: [PATCH 3/3] part 3 completed! --- part3/app/components/GameBoard.java | 1 + part3/app/components/MainFrame.java | 9 ++++++++- part3/app/components/ScorePanel.java | 1 + 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/part3/app/components/GameBoard.java b/part3/app/components/GameBoard.java index 1382a9c..d67c123 100644 --- a/part3/app/components/GameBoard.java +++ b/part3/app/components/GameBoard.java @@ -1,3 +1,4 @@ +// GameBoard.java package part3.app.components; import javax.swing.*; diff --git a/part3/app/components/MainFrame.java b/part3/app/components/MainFrame.java index ac30027..28e29a6 100644 --- a/part3/app/components/MainFrame.java +++ b/part3/app/components/MainFrame.java @@ -1,7 +1,14 @@ +// MainFrame.java package part3.app.components; import javax.swing.*; import java.awt.*; +import java.awt.event.*; +import java.awt.image.*; +import javax.swing.*; +import javax.swing.event.*; +import javax.swing.filechooser.*; + public class MainFrame extends JFrame{ public MainFrame() { @@ -31,4 +38,4 @@ public MainFrame() { setVisible(true); }); } -} +} \ No newline at end of file diff --git a/part3/app/components/ScorePanel.java b/part3/app/components/ScorePanel.java index 5efe35d..984d60a 100644 --- a/part3/app/components/ScorePanel.java +++ b/part3/app/components/ScorePanel.java @@ -1,3 +1,4 @@ +// ScorePanel.java package part3.app.components; import java.awt.Dimension;