hw4: cleanup
This commit is contained in:
parent
857802227d
commit
573498ddb4
@ -2,21 +2,25 @@ import java.awt.*;
|
||||
import java.awt.geom.*;
|
||||
|
||||
/**
|
||||
* A car shape composed of primitive shapes.
|
||||
* NOTE: This code structure is based on typical textbook examples for drawing composite shapes.
|
||||
* Specific implementation details might vary based on the exact textbook source.
|
||||
* A car shape.
|
||||
* Partially taken from the textbook (Cay S. Horstmann - OO Design & Patterns, 2nd ed.)
|
||||
*/
|
||||
public class CarShape implements CompositeShape {
|
||||
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 width;
|
||||
|
||||
// Default constructor positions at (0,0) with default width
|
||||
public CarShape() {
|
||||
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) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
@ -36,31 +40,38 @@ public class CarShape implements CompositeShape {
|
||||
|
||||
@Override
|
||||
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);
|
||||
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);
|
||||
// The front of the roof
|
||||
Point2D.Double r2 = new Point2D.Double(x + width / 3.0, y);
|
||||
Point2D.Double r3 = new Point2D.Double(x + width * 2.0 / 3.0, y);
|
||||
Point2D.Double r4 = new Point2D.Double(x + width * 5.0 / 6.0, y + width / 6.0);
|
||||
Line2D.Double L1 = new Line2D.Double(r1,r2);
|
||||
Line2D.Double L2 = new Line2D.Double(r2,r3);
|
||||
Line2D.Double L3 = 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);
|
||||
// The rear of the roof
|
||||
Point2D.Double r3 = new Point2D.Double(x + width * 2 / 3.0, y);
|
||||
// The bottom of the rear windshield
|
||||
Point2D.Double r4 = new Point2D.Double(x + width * 5 / 6.0, y + width / 6.0);
|
||||
Line2D.Double frontWindshield = new Line2D.Double(r1, r2);
|
||||
Line2D.Double roofTop = new Line2D.Double(r2, r3);
|
||||
Line2D.Double rearWindshield = new Line2D.Double(r3, r4);
|
||||
|
||||
// Draw the parts
|
||||
g2.draw(body);
|
||||
g2.draw(L1);
|
||||
g2.draw(L2);
|
||||
g2.draw(L3);
|
||||
g2.draw(frontTire);
|
||||
g2.draw(rearTire);
|
||||
g2.draw(frontWindshield);
|
||||
g2.draw(roofTop);
|
||||
g2.draw(rearWindshield);
|
||||
}
|
||||
}
|
||||
|
@ -24,4 +24,4 @@ public interface CompositeShape {
|
||||
* @return the height
|
||||
*/
|
||||
int getHeight();
|
||||
}
|
||||
}
|
||||
|
@ -7,8 +7,8 @@ import java.awt.geom.Rectangle2D;
|
||||
* door (rectangle), and window (rectangle).
|
||||
*/
|
||||
public class HouseShape implements CompositeShape {
|
||||
private int width; // Width of the house body
|
||||
private int height; // Height of the house body (excluding roof)
|
||||
private final int width; // Width of the house body
|
||||
private final int height; // Height of the house body (excluding roof)
|
||||
|
||||
public HouseShape() {
|
||||
this(50, 40); // Default width and body height
|
||||
|
@ -11,15 +11,15 @@ public class ShapeDisplayer extends JFrame {
|
||||
private static final int ICON_WIDTH = 50;
|
||||
private static final int ICON_HEIGHT = 50;
|
||||
|
||||
private DrawingPanel drawingPanel;
|
||||
private final DrawingPanel drawingPanel;
|
||||
private CompositeShape currentShape = null; // The shape currently selected
|
||||
|
||||
private JButton carButton;
|
||||
private JButton snowmanButton;
|
||||
private JButton houseButton;
|
||||
private ShapeIcon carIcon;
|
||||
private ShapeIcon snowmanIcon;
|
||||
private ShapeIcon houseIcon;
|
||||
private final JButton carButton;
|
||||
private final JButton snowmanButton;
|
||||
private final JButton houseButton;
|
||||
private final ShapeIcon carIcon;
|
||||
private final ShapeIcon snowmanIcon;
|
||||
private final ShapeIcon houseIcon;
|
||||
|
||||
// Helper class to store drawn shapes and their positions
|
||||
private static class PlacedShape {
|
||||
@ -66,7 +66,7 @@ public class ShapeDisplayer extends JFrame {
|
||||
super.paintComponent(g);
|
||||
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);
|
||||
|
||||
|
||||
|
@ -48,13 +48,12 @@ public class ShapeIcon implements Icon {
|
||||
public void paintIcon(Component c, Graphics g, int x, int y) {
|
||||
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);
|
||||
|
||||
// Translate to the icon's drawing position
|
||||
g2.translate(x, y);
|
||||
|
||||
// --- Centering and Scaling Logic ---
|
||||
double shapeWidth = shape.getWidth();
|
||||
double shapeHeight = shape.getHeight();
|
||||
|
||||
@ -71,7 +70,6 @@ public class ShapeIcon implements Icon {
|
||||
AffineTransform originalTransform = g2.getTransform();
|
||||
g2.translate(dx, dy);
|
||||
g2.scale(scale, scale);
|
||||
// --- End Centering and Scaling ---
|
||||
|
||||
// Draw the shape
|
||||
shape.draw(g2);
|
||||
|
@ -5,7 +5,7 @@ import java.awt.geom.Ellipse2D;
|
||||
* A snowman shape composed of three circles.
|
||||
*/
|
||||
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() {
|
||||
this(40); // Default width
|
||||
|
Loading…
x
Reference in New Issue
Block a user