diff --git a/hw4/.gitignore b/hw4/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/hw4/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/hw4/.idea/.gitignore b/hw4/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/hw4/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/hw4/.idea/codeStyles/codeStyleConfig.xml b/hw4/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..a55e7a1 --- /dev/null +++ b/hw4/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/hw4/.idea/inspectionProfiles/Project_Default.xml b/hw4/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..03d9549 --- /dev/null +++ b/hw4/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/hw4/.idea/misc.xml b/hw4/.idea/misc.xml new file mode 100644 index 0000000..81a8e88 --- /dev/null +++ b/hw4/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/hw4/.idea/modules.xml b/hw4/.idea/modules.xml new file mode 100644 index 0000000..60c3a11 --- /dev/null +++ b/hw4/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/hw4/.idea/vcs.xml b/hw4/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/hw4/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/hw4/README.md b/hw4/README.md new file mode 100644 index 0000000..d1ce3cb --- /dev/null +++ b/hw4/README.md @@ -0,0 +1,24 @@ +# Programming Assignment 4 - Shape Displayer + +## Requirements + +The user interacts with the application as follows: + +- Initially, the program displays three buttons on the top of the frame and a + blank large area of a user-defined JPanel below the buttons. The buttons + display a car, a snow man, and a composite shape of your choice, respectively. + You are allowed to borrow the CarShape.java from the text book. Make sure to + acknowledge that the code is from the text book. The composite shape of your + choice should consist of at least 4 primitive shapes, but doesn't have to be + fancy. +- When the user clicks on one of the buttons, the shape displayed on the clicked + button becomes the current shape. The icon representing the current shape + should outline the shape as shown below in the sample output. When a mouse is + clicked on the user-defined JPanel, the current shape is drawn on the position + which the mouse was pressed on. +- The current shape can be changed by clicking one of the buttons. +- The application should be reusable by any CompositeShape. +- Your design should be object-oriented including ShapeDisplayer(with main + method), CarShape, SnowMan, and Your_own_shape classes. Also, it should + include interface CompositeShape.java and a concrete class ShapeIcon. You may + include more classes to your design. diff --git a/hw4/hw4.iml b/hw4/hw4.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/hw4/hw4.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/hw4/src/ShapeDisplayer.java b/hw4/src/ShapeDisplayer.java new file mode 100644 index 0000000..4dd76b8 --- /dev/null +++ b/hw4/src/ShapeDisplayer.java @@ -0,0 +1,71 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.geom.Rectangle2D; + +public class ShapeDisplayer { + public static void main(String[] args) { + // Create and configure the main frame on the Event Dispatch Thread + SwingUtilities.invokeLater(() -> { + JFrame frame = new JFrame("Mancala"); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setSize(800, 400); + + // Create buttons panel + JPanel buttonPanel = new JPanel(); + buttonPanel.setLayout(new FlowLayout()); + + // Create placeholder shapes + JButton carButton = new JButton(new PlaceholderIcon(Color.RED)); + JButton snowmanButton = new JButton(new PlaceholderIcon(Color.BLUE)); + JButton compositeButton = new JButton(new PlaceholderIcon(Color.GREEN)); + + + // Add buttons to the panel + buttonPanel.add(carButton); + buttonPanel.add(snowmanButton); + buttonPanel.add(compositeButton); + + // Add buttons panel to the top of the frame + frame.add(buttonPanel, BorderLayout.NORTH); + + // Add drawing panel + JPanel drawingPanel = new JPanel(); + drawingPanel.setBackground(Color.WHITE); + frame.add(drawingPanel, BorderLayout.CENTER); + + // Center the frame on the screen and make it visible + frame.setLocationRelativeTo(null); + frame.setVisible(true); + }); + } +} + +class PlaceholderIcon implements Icon { + private final Color color; + private static final int WIDTH = 60; + private static final int HEIGHT = 40; + + public PlaceholderIcon(Color color) { + this.color = color; + } + + @Override + public void paintIcon(Component c, Graphics g, int x, int y) { + Graphics2D g2 = (Graphics2D) g; + Rectangle2D.Double rect = new Rectangle2D.Double(x + 5, y + 5, WIDTH - 10, HEIGHT - 10); + g2.setColor(color); + g2.fill(rect); + g2.setColor(Color.BLACK); + g2.draw(rect); + } + + @Override + public int getIconWidth() { + return WIDTH; + } + + @Override + public int getIconHeight() { + return HEIGHT; + } +}