hw4: cleanup

This commit is contained in:
Yuri Tatishchev 2025-04-19 16:22:57 -07:00
parent 857802227d
commit 573498ddb4
Signed by: CaZzzer
SSH Key Fingerprint: SHA256:sqXB3fe0LMpfH+IeM/vlmxKdso52kssrIJBlwKXVe1U
6 changed files with 46 additions and 37 deletions

View File

@ -2,21 +2,25 @@ import java.awt.*;
import java.awt.geom.*; import java.awt.geom.*;
/** /**
* A car shape composed of primitive shapes. * A car shape.
* NOTE: This code structure is based on typical textbook examples for drawing composite shapes. * Partially taken from the textbook (Cay S. Horstmann - OO Design & Patterns, 2nd ed.)
* Specific implementation details might vary based on the exact textbook source.
*/ */
public class CarShape implements CompositeShape { public class CarShape implements CompositeShape {
private final int x; // Base x-coordinate (relative to drawing origin) private final int x; // Base x-coordinate (relative to drawing origin)
private final int y; // Base y-coordinate (relative to drawing origin) private final int y; // Base y-coordinate (relative to drawing origin)
private final int width; private final int width;
// Default constructor positions at (0,0) with default width
public CarShape() { public CarShape() {
this(0, 0, 60); // Default position and width this(0, 0, 60); // Default position and width
} }
// Constructor for position and width (not strictly needed if always drawn at origin for icon/panel) /**
* Constructs a car shape.
*
* @param x the left of the bounding rectangle
* @param y the top of the bounding rectangle
* @param width the width of the bounding rectangle
*/
public CarShape(int x, int y, int width) { public CarShape(int x, int y, int width) {
this.x = x; this.x = x;
this.y = y; this.y = y;
@ -36,31 +40,38 @@ public class CarShape implements CompositeShape {
@Override @Override
public void draw(Graphics2D g2) { public void draw(Graphics2D g2) {
// All drawing is relative to (0,0) because translation happens externally
// Car body
Rectangle2D.Double body = new Rectangle2D.Double(x, y + width / 6.0, width - 1, width / 6.0); Rectangle2D.Double body = new Rectangle2D.Double(x, y + width / 6.0, width - 1, width / 6.0);
Ellipse2D.Double frontTire = new Ellipse2D.Double(
x + width / 6.0,
y + width / 3.0,
width / 6.0,
width / 6.0
);
Ellipse2D.Double rearTire = new Ellipse2D.Double(
x + width * 2 / 3.0,
y + width / 3.0,
width / 6.0,
width / 6.0
);
// Roof // The bottom of the front windshield
Point2D.Double r1 = new Point2D.Double(x + width / 6.0, y + width / 6.0); Point2D.Double r1 = new Point2D.Double(x + width / 6.0, y + width / 6.0);
// The front of the roof
Point2D.Double r2 = new Point2D.Double(x + width / 3.0, y); Point2D.Double r2 = new Point2D.Double(x + width / 3.0, y);
Point2D.Double r3 = new Point2D.Double(x + width * 2.0 / 3.0, y); // The rear of the roof
Point2D.Double r4 = new Point2D.Double(x + width * 5.0 / 6.0, y + width / 6.0); Point2D.Double r3 = new Point2D.Double(x + width * 2 / 3.0, y);
Line2D.Double L1 = new Line2D.Double(r1,r2); // The bottom of the rear windshield
Line2D.Double L2 = new Line2D.Double(r2,r3); Point2D.Double r4 = new Point2D.Double(x + width * 5 / 6.0, y + width / 6.0);
Line2D.Double L3 = new Line2D.Double(r3,r4); Line2D.Double frontWindshield = new Line2D.Double(r1, r2);
Line2D.Double roofTop = new Line2D.Double(r2, r3);
Line2D.Double rearWindshield = new Line2D.Double(r3, r4);
// Wheels
Ellipse2D.Double frontTire = new Ellipse2D.Double(x + width / 6.0, y + width / 3.0, width / 6.0, width / 6.0);
Ellipse2D.Double rearTire = new Ellipse2D.Double(x + width * 2.0 / 3.0, y + width / 3.0, width / 6.0, width / 6.0);
// Draw the parts // Draw the parts
g2.draw(body); g2.draw(body);
g2.draw(L1);
g2.draw(L2);
g2.draw(L3);
g2.draw(frontTire); g2.draw(frontTire);
g2.draw(rearTire); g2.draw(rearTire);
g2.draw(frontWindshield);
g2.draw(roofTop);
g2.draw(rearWindshield);
} }
} }

View File

@ -7,8 +7,8 @@ import java.awt.geom.Rectangle2D;
* door (rectangle), and window (rectangle). * door (rectangle), and window (rectangle).
*/ */
public class HouseShape implements CompositeShape { public class HouseShape implements CompositeShape {
private int width; // Width of the house body private final int width; // Width of the house body
private int height; // Height of the house body (excluding roof) private final int height; // Height of the house body (excluding roof)
public HouseShape() { public HouseShape() {
this(50, 40); // Default width and body height this(50, 40); // Default width and body height

View File

@ -11,15 +11,15 @@ public class ShapeDisplayer extends JFrame {
private static final int ICON_WIDTH = 50; private static final int ICON_WIDTH = 50;
private static final int ICON_HEIGHT = 50; private static final int ICON_HEIGHT = 50;
private DrawingPanel drawingPanel; private final DrawingPanel drawingPanel;
private CompositeShape currentShape = null; // The shape currently selected private CompositeShape currentShape = null; // The shape currently selected
private JButton carButton; private final JButton carButton;
private JButton snowmanButton; private final JButton snowmanButton;
private JButton houseButton; private final JButton houseButton;
private ShapeIcon carIcon; private final ShapeIcon carIcon;
private ShapeIcon snowmanIcon; private final ShapeIcon snowmanIcon;
private ShapeIcon houseIcon; private final ShapeIcon houseIcon;
// Helper class to store drawn shapes and their positions // Helper class to store drawn shapes and their positions
private static class PlacedShape { private static class PlacedShape {
@ -66,7 +66,7 @@ public class ShapeDisplayer extends JFrame {
super.paintComponent(g); super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create(); // Work on a copy Graphics2D g2 = (Graphics2D) g.create(); // Work on a copy
// Enable anti-aliasing for smoother graphics // Enable antialiasing for smoother graphics
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

View File

@ -48,13 +48,12 @@ public class ShapeIcon implements Icon {
public void paintIcon(Component c, Graphics g, int x, int y) { public void paintIcon(Component c, Graphics g, int x, int y) {
Graphics2D g2 = (Graphics2D) g.create(); // Work on a copy Graphics2D g2 = (Graphics2D) g.create(); // Work on a copy
// Enable anti-aliasing for smoother graphics // Enable antialiasing for smoother graphics
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// Translate to the icon's drawing position // Translate to the icon's drawing position
g2.translate(x, y); g2.translate(x, y);
// --- Centering and Scaling Logic ---
double shapeWidth = shape.getWidth(); double shapeWidth = shape.getWidth();
double shapeHeight = shape.getHeight(); double shapeHeight = shape.getHeight();
@ -71,7 +70,6 @@ public class ShapeIcon implements Icon {
AffineTransform originalTransform = g2.getTransform(); AffineTransform originalTransform = g2.getTransform();
g2.translate(dx, dy); g2.translate(dx, dy);
g2.scale(scale, scale); g2.scale(scale, scale);
// --- End Centering and Scaling ---
// Draw the shape // Draw the shape
shape.draw(g2); shape.draw(g2);

View File

@ -5,7 +5,7 @@ import java.awt.geom.Ellipse2D;
* A snowman shape composed of three circles. * A snowman shape composed of three circles.
*/ */
public class SnowmanShape implements CompositeShape { public class SnowmanShape implements CompositeShape {
private int width; // Base diameter of the bottom circle private final int width; // Base diameter of the bottom circle
public SnowmanShape() { public SnowmanShape() {
this(40); // Default width this(40); // Default width