Skip to content
Open

done #17

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
40 changes: 40 additions & 0 deletions part1/Ex1/ButtonPanel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import javax.swing.*;
import java.awt.*;
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, 1, 1));
setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
ActionListener actionListener = e -> {
String cmd = ((JButton) e.getSource()).getText();
if (cmd.equals("=")) {
String Expr = display.getDisplay();
String result = EvalUtil.eval(Expr);
display.setDisplay(result);
} else if (cmd.equals("C")) {
display.setDisplay("");
} else if (cmd.equals("b")) {
if (display.getDisplay().length() > 0) {
display.setDisplay(display.getDisplay().substring(0, display.getDisplay().length() - 1));
}
} else {
display.setDisplay(display.getDisplay() + cmd);
}
};
for (String button : BUTTONS) {
JButton btn = new JButton(button);
btn.setFont(new Font("Arial", Font.PLAIN, 20));
btn.addActionListener(actionListener);
add(btn);
}

}

}
18 changes: 18 additions & 0 deletions part1/Ex1/CalculatorApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import javax.swing.*;
import java.awt.*;
public class CalculatorApp extends JFrame {
public CalculatorApp() {
setTitle("Graphic Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,400);
setLocationRelativeTo(null);
DisplayPanel displayPanel = new DisplayPanel();
ButtonPanel buttonPanel = new ButtonPanel(displayPanel);
setLayout(new BorderLayout());
add(displayPanel,BorderLayout.NORTH);
add(buttonPanel,BorderLayout.CENTER);
setVisible(true);
}


}
23 changes: 23 additions & 0 deletions part1/Ex1/DisplayPanel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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();
setBorder(BorderFactory.createEmptyBorder(5,10,5,10));
displayField.setFont(new Font("Arial",Font.PLAIN,24));
displayField.setHorizontalAlignment(JTextField.LEFT);
add(displayField,BorderLayout.CENTER);
}
public String getDisplay() {
return displayField.getText();
}
public void setDisplay(String display) {
displayField.setText(display);
}
}
87 changes: 87 additions & 0 deletions part1/Ex1/EvalUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
public class EvalUtil {
public static String eval(String expression) {
class Parser {
private final String s = expression;
private int pos = -1;
private int ch;

private void nextChar() {
pos++;
ch = (pos < s.length()) ? s.charAt(pos) : -1;
}

private boolean eat(int charToEat) {
while (ch == ' ') nextChar();
if (ch == charToEat) {
nextChar();
return true;
}
return false;
}

private double parse() {
nextChar();
double x = parseExpression();
if (pos < s.length()) {
throw new RuntimeException("Unexpected character: '" + (char) ch + "'");
}
return x;
}

private double parseExpression() {
double x = parseTerm();
while (true) {
if (eat('+')) x += parseTerm();
else if (eat('-')) x -= parseTerm();
else return x;
}
}

private double parseTerm() {
double x = parseFactor();
while (true) {
if (eat('*')) x *= parseFactor();
else if (eat('/')) {
double denominator = parseFactor();
if (denominator == 0) {
throw new ArithmeticException("Division by zero");
}
x /= denominator;
} else return x;
}
}

private double parseFactor() {
if (eat('+')) return parseFactor();
if (eat('-')) return -parseFactor();

double x;
int startPos = this.pos;

if (eat('(')) {
x = parseExpression();
if (!eat(')')) throw new RuntimeException("Missing closing parenthesis ')'");
} else if ((ch >= '0' && ch <= '9') || ch == '.') {
while ((ch >= '0' && ch <= '9') || ch == '.') nextChar();
String numberStr = s.substring(startPos, this.pos);
x = Double.parseDouble(numberStr);
} else {
throw new RuntimeException("Unexpected character: '" + (char) ch + "'");
}

return x;
}
}

try {
double result = new Parser().parse();
if (result == (long) result) {
return Long.toString((long) result);
} else {
return Double.toString(result);
}
} catch (Exception e) {
return "Error";
}
}
}
7 changes: 7 additions & 0 deletions part1/Ex1/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

import javax.swing.*;
public class Main {
public static void main(String[] args) {
SwingUtilities.invokeLater(()-> new CalculatorApp());
}
}
44 changes: 44 additions & 0 deletions part2/Ex2/ControlPanel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import javax.swing.*;
import java.awt.*;

public class ControlPanel extends JPanel {
private final JTextField widthField;
private final JTextField heightField;
private final JButton ScaleButton;
private final ShowPanel showPanel;

public ControlPanel(ShowPanel showPanel) {
this.showPanel = showPanel;
setLayout(new FlowLayout());
setBackground(Color.white);
setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
ScaleButton = new JButton("Scale Image");
widthField = new JTextField(5);
heightField = new JTextField(5);
add(new JLabel("Width:"));
add(widthField);
add(new JLabel("Height:"));
add(heightField);
add(ScaleButton);
ScaleButton.addActionListener(e -> {
try {

int width = Integer.parseInt(widthField.getText());
int height = Integer.parseInt(heightField.getText());
int maxWidth = showPanel.getWidth();
int maxHeight = showPanel.getHeight();
if (width > maxWidth || height > maxHeight) {
JOptionPane.showMessageDialog(this, "Width or height must be equal to max Width or Height");

} else {
showPanel.loadImage();
showPanel.scaleImage(width, height);
}
}
catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "Please enter valid numbers for width and height");
}
}
);
}
}
7 changes: 7 additions & 0 deletions part2/Ex2/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import javax.swing.*;

public class Main {
public static void main(String[] args) {
SwingUtilities.invokeLater(MainFrame::new);
}
}
19 changes: 19 additions & 0 deletions part2/Ex2/MainFrame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import javax.swing.*;
import java.awt.*;

public class MainFrame extends JFrame {
public MainFrame() {
setTitle("image Scaler");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 600);
setLocationRelativeTo(null);
setLayout(new BorderLayout());
ShowPanel showPanel = new ShowPanel();
ControlPanel controlPanel = new ControlPanel(showPanel);
add(showPanel,BorderLayout.CENTER);
add(controlPanel,BorderLayout.SOUTH);
setVisible(true);
}


}
59 changes: 59 additions & 0 deletions part2/Ex2/ShowPanel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.io.IOException;

public class ShowPanel extends JPanel {
private Image image;
private String imagePath = "sololeveling.jpg";

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

}
public Image getImage() {
return image;
}

public void setImage(Image image) {
this.image = image;
}

public String getImagePath() {
return imagePath;
}

public void setImagePath(String imagePath) {
this.imagePath = imagePath;
}
public void loadImage(){
try{
image = ImageIO.read(getClass().getResource(imagePath));
}
catch (IOException e)
{
e.printStackTrace();
}
repaint();
}
public void scaleImage(int width, int height){
if(image != null)
{
Image scaled = image.getScaledInstance(width, height, Image.SCALE_SMOOTH);
image = scaled;
repaint();
}
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if(image != null){
int x = (getWidth()-image.getWidth(null))/2;
int y = (getHeight()-image.getHeight(null))/2;
g.drawImage(image,x,y,this);
}

}

}
Binary file added part2/Ex2/sololeveling.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.