solution/Car.java
solution/Car.java
public class Car extends ManyShapes{
 Â
 Create a car object by assigning an origin point, clearance, x and y size (of the car body).Â
 Â
 wheel radius is set by clearance and wheels are insetÂ
  public Car(Point setOrigin, double clearance, double xSize, double ySize){
    Point origin =  setOrigin;
    double wheelInset = 0.03;
   Â
 Create rectangle corner points.Â
    Point rll = new Point (setOrigin.x,setOrigin.y + clearance);Â
    Point rtr = new Point (setOrigin.x + xSize, setOrigin.y + clearance + ySize);
   Â
 create the origin points for the wheels
    Point leftaxis = new Point(setOrigin.x + wheelInset, setOrigin.y + clearance);
    Point rightaxis = new Point(setOrigin.x + xSize - wheelInset, setOrigin.y + clearance);
   Â
 create the rectangle part of the car (the body)
    Rectangle rect = new Rectangle(rll, rtr);
   Â
 Create the wheel circles.Â
    Circle leftWheel = new Circle(leftaxis, clearance);
    Circle rightWheel = new Circle(rightaxis, clearance);
    addShape(rect);
    addShape(leftWheel);
    addShape(rightWheel);
  }
 Â
 No longer needed methods because they are being implemented in the ManyShapes class now!
 Â
 - increment origin
 Â
 - set position
 Â
 - set vel
}
solution/Circle.java
solution/Circle.java
public class Circle extends Shape {
 Â
 Center point
  private Point centre;
 Â
 the radius
  private double radius;
 Â
 Constructor accepts center point and radius
  public Circle(Point centerPoint, double radius) {
   Â
 For demonstration purposes, you should try to keep a consistent style if possible
    centre = centerPoint;Â
 not using this because the param is different
    this.radius = radius;Â
 using this because the name is the same as param
  }
 Â
 move the Circle by a given amount.Â
  public void move(double xOffset, double yOffset){
    centre.move(xOffset, yOffset);
  }
 Â
 Assign the velocity of this Shape
  public void setVel(double newX, double newY){
    centre.setVel(newX,newY);
  }
 Â
 update this Circles position
  public void update(){
    centre.update();
  }
 Â
 draw the circle
  public void draw( ) {
    StdDraw.circle(centre.x, centre.y, radius);
  }
draw method
}
Circle
solution/House.java
solution/House.java
public class House extends ManyShapes{
   Â
    public House(Point origin, double baseHeight, double baseWidth,Â
                double roofHeight, double roofWidth, Â
                double doorHeight, double doorWidth){
       Â
        super();Â
 just as a reminde
       Â
       Â
 the ManyShapes will provide all the rest of the functionality.Â
       Â
        Point rll = new Point (origin.x-0.2,origin.y);Â
        Point rtr = new Point (origin.x+baseWidth-0.2, origin.y+baseHeight);
        Rectangle rect = new Rectangle(rll, rtr);
        Â
         Point a = new Point (origin.x-0.3, origin.y+baseHeight);
         Point b = new Point (origin.x-0.3+(roofWidth)/2, origin.y+baseHeight+(roofHeight)/2+0.1);
         Point c = new Point (origin.x+roofWidth-0.3, origin.y+baseHeight);
        Â
         Triangle triangle = new Triangle(a, b, c);
        Â
         Point rll2 = new Point (origin.x-0.2 + (baseWidth/2)-(doorWidth/2),origin.y);Â
         Point rtr2 = new Point (origin.x-0.2 + (baseWidth/2)+doorWidth -(doorWidth/2), origin.y+doorHeight);
         Rectangle door = new Rectangle(rll2, rtr2);
        Â
         Point point = new Point (origin.x-0.2 + (baseWidth/2),origin.y);Â
        Â
         addShape(rect);
         addShape(triangle);
         addShape(door);
         addShape(point);
    }Â
}
solution/ManyShapes.java
solution/ManyShapes.java
public class ManyShapes extends Shape {
 Â
 Â
 --- Basic variables ---
 Â
 Max components
  private static final int MAX_PARTS = 25;
 Â
 a
ay of Shapes
  private Shape[] parts;
 Â
 cu
ent number of Shapes
  private int numParts;
 Â
 Â
 Constructor to create the empty shape.Â
  public ManyShapes() {
    parts = new Shape[MAX_PARTS];
    numParts = 0;Â
  }
 Â
 Â
 Add a given shape to the a
ay Â
  public void addShape(Shape newShape){
    parts[numParts++] = newShape;Â
  }
 Â
 Â
 Draw All the shapes stored in this a
ay.Â
  public void draw( ){
    for(int i = 0 ; i < numParts; i++)
      parts[i].draw();Â
 draw each Shape
  }
 Â
 Â
 Set the velocity of all shapes contained.Â
  public void setVel(double newX, double newY){
      for(int i = 0 ; i < numParts; i++)
        parts[i].setVel(newX, newY);
  }
 Â
 Update the position of the shape.
  public void update(){
    for(int i = 0 ; i < numParts; i++)
      parts[i].update();
  }
   Â
 Â
 Move all the Shapes held by this object.Â
  public void move(double xOffset, double yOffset){
    for(int i = 0 ; i < numParts; i++)
      parts[i].move(xOffset, yOffset); Â
  }
}
class Thing
solution/Point.java
solution/Point.java
public class Point extends Shape{
   Â
 Cu
ent position of the point
    public double x;
    public double y;
   Â
   Â
 cu
ent velocity of the point
    public double xVel;
    public double yVel;
   Â
   Â
 draw the point (for debugging)
    public void draw(){
        StdDraw.circle(x,y,.01);
    }
   Â
   Â
 create a new point at the given coordinates
    public Point(double initX, double initY){
          x = initX; y=initY;Â
    }
   Â
   Â
 update the velocity of the point.Â
    public void setVel(double newX, double newY){
        xVel = newX;
        yVel = newY;
    }
   Â
   Â
 update the point to a new position.Â
    public void update(){
        move(xVel, yVel);
    }
   Â
    public void move(double xOffset, double yOffset){
        x += xOffset;
        y += yOffset;
    }
}
solution/Rectangle.java
solution/Rectangle.java
 Rectangle draws a rectangular shape to the GUI
 It is defined by its lower left and upper right points
public class Rectangle extends Shape {
    private Point lowerLeft;
    private Point upperRight;
   Â
   Â
 Initialize the Rectangle with lower left and upper right points
    public Rectangle(Point ll, Point ur) {
        lowerLeft= ll;Â
        upperRight= ur;Â
    }
   Â
   Â
 Move the Rectangle by the given offset
    public void move(double xOffset, double yOffset){
       lowerLeft.move(xOffset, yOffset);
       upperRight.move(xOffset, yOffset);
    }
   Â
   Â
 Set the velocity of this Shape
    public void setVel(double newX, double newY){
        lowerLeft.setVel(newX,newY);
        upperRight.setVel(newX,newY);
    }
   Â
   Â
 update the Rectangle position by updating the position by
   Â
 the velocity.
    public void update(){
        lowerLeft.update();
        upperRight.update();
    }
   Â
   Â
 Draw the rectangle with cu
ent settings.Â
    public void draw( ) {
         StdDraw.rectangle((lowerLeft.x+upperRight.x)/2,
               (lowerLeft.y+upperRight.y)/2,
                    (upperRight.x-lowerLeft.x)/2,
                    (upperRight.y-lowerLeft.y)/2);
      }
draw method
    }
Rectangle class
solution/screenshot.PNG
solution/Shape.java
solution/Shape.java
import java.awt.Color;
 Cant create a Shape Directly
public abstract class Shape {
       Â
   Â
 Color of the shape
    protected Color colour;
   Â
   Â
 Draws the Shape, ove
idden by subclasses
    public void draw(){ };
   Â
 set the velocity of this Shape.Â
    public void setVel(double newX, double newY){}
   Â
   Â
 update the position of the Shape byt the velocity
    public void update(){}
   Â
   Â
 update the cu
ent position of the Shape by the given offset
    public void move(double xOffset, double yOffset){}
}
solution/StdDraw.java
solution/StdDraw.java
*************************************************************************
 *  Compilation:  javac StdDraw.java
 *  Execution:    java StdDraw
 *
 *  Standard drawing li
ary. This class provides a basic capability fo
 *  creating drawings with your programs. It uses a simple graphics model that
 *  allows you to create drawings consisting of points, lines, and curves
 *  in a window on your computer and to save the drawings to a file.
 *
 *  Todo
 *  ----
 *    -  Add support for gradient fill, etc.
 *    -  Fix setCanvasSize() so that it can only be called once.
 *    -  On some systems, drawing a line (or other shape) that extends way
 *       beyond canvas (e.g., to infinity) dimensions does not get drawn.
 *
 *  Remarks
 *  -------
 *    -  don't use AffineTransform for rescaling since it inverts
 *       images and strings
 *    -  careful using setFont in inner loop within an animation -
 *       it can cause flicke
 *
 *************************************************************************
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.io.*;
import java.net.*;
import java.util.LinkedList;
import java.util.TreeSet;
import javax.imageio.ImageIO;
import javax.swing.*;
**
 * Â
Standard draw
i>. This class provides a basic capability fo
 *  creating drawings with your programs. It uses a simple graphics model that
 *  allows you to create drawings consisting of points, lines, and curves
 *  in a window on your computer and to save the drawings to a file.
 *   *  For additional documentation, seeÂ
introcs.cs.princeton.edu/15inout">Section 1.5
a>Â of
 * Â
Introduction to Programming in Java: An Interdisciplinary Approach
i> by Robert Sedgewick and Kevin Wayne.
 *
 *  @author Robert Sedgewick
 *  @author Kevin Wayne
 *
public final class StdDraw implements ActionListener, MouseListener, MouseMotionListener, KeyListener {
   Â
 pre-defined colors
    public static final Color BLACK      = Color.BLACK;
    public static final Color BLUE       = Color.BLUE;
    public static final Color CYAN       = Color.CYAN;
    public static final Color DARK_GRAY  = Color.DARK_GRAY;
    public static final Color GRAY       = Color.GRAY;
    public static final Color GREEN      = Color.GREEN;
    public static final Color LIGHT_GRAY = Color.LIGHT_GRAY;
    public static final Color MAGENTA    = Color.MAGENTA;
    public static final Color ORANGE     = Color.ORANGE;
    public static final Color PINK       = Color.PINK;
    public static final Color RED        = Color.RED;
    public static final Color WHITE      = Color.WHITE;
    public static final Color YELLOW     = Color.YELLOW;
    /**
     * HI MOM!!
     * Shade of blue used in Introduction to Programming in Java.
     * It is Pantone 300U. The RGB values are approximately (9, 90, 166).
     *
    public static final Color BOOK_BLUE       = new Color(  9,  90, 166);
    public static final Color BOOK_LIGHT_BLUE = new Color(103, 198, 243);
    /**
     * Shade of red used in Algorithms 4th edition.
     * It is Pantone 1805U. The RGB values are approximately (150, 35, 31).
     *
    public static final Color BOOK_RED = new Color(150, 35, 31);
   Â
 default colors
    private static final Color DEFAULT_PEN_COLOR   = BLACK;
    private static final Color DEFAULT_CLEAR_COLOR = WHITE;
   Â
 cu
ent pen colo
    private static Color penColor;
   Â
 default canvas size is DEFAULT_SIZE-by-DEFAULT_SIZE
    private static final int DEFAULT_SIZE = 512;
    private static int width  = DEFAULT_SIZE;
    private static int height = DEFAULT_SIZE;
   Â
 default pen radius
    private static final double DEFAULT_PEN_RADIUS = 0.002;
   Â
 cu
ent pen radius
    private static double penRadius;
   Â
 show we draw immediately or wait until next show?
    private static boolean defer = false;
   Â
 boundary of drawing canvas, 5% borde
    private static final double BORDER = 0.05;
    private static final double DEFAULT_XMIN = 0.0;
    private static final double DEFAULT_XMAX = 1.0;
    private static final double DEFAULT_YMIN = 0.0;
    private static final double DEFAULT_YMAX = 1.0;
    private static double xmin, ymin, xmax, ymax;
   Â
 for synchronization
    private static Object mouseLock = new Object();
    private static Object keyLock = new Object();
   Â
 default font
    private static final Font DEFAULT_FONT = new Font("SansSerif", Font.PLAIN, 16);
   Â
 cu
ent font
    private static Font font;
   Â
 double buffered graphics
    private static BufferedImage offscreenImage, onscreenImage;
    private static Graphics2D offscreen, onscreen;
   Â
 singleton for callbacks: avoids generation of extra .class files
    private static StdDraw std = new StdDraw();
   Â
 the frame for drawing to the screen
    private static JFrame frame;
   Â
 mouse state
    private static boolean mousePressed = false;
    private static double mouseX = 0;
    private static double mouseY = 0;
   Â
 queue of typed key characters
    private static LinkedList keysTyped = new LinkedList();
   Â
 set of key codes cu
ently pressed down
    private static TreeSet keysDown = new TreeSet();
 Â
   Â
 singleton pattern: client can't instantiate
    private StdDraw() { }
   Â
 static initialize
    static { init(); }
    /**
     * Set the window size to the default size 512-by-512 pixels.
     * This method must be called before any other commands.
     *
    public static void setCanvasSize() {
        setCanvasSize(DEFAULT_SIZE, DEFAULT_SIZE);
    }
    /**
     * Set the window size to w-by-h pixels.
     * This method must be called before any other commands.
     *
     * @param w the width as a number of pixels
     * @param h the height as a number of pixels
     * @throws a IllegalArgumentException if the width or height is 0 or negative
     *
    public static void setCanvasSize(int w, int h) {
        if (w < 1 || h < 1) throw new IllegalArgumentException("width and height must be positive");
        width = w;
        height = h;
        init();
    }
   Â
 init
    private static void init() {
        if (frame != null) frame.setVisible(false);
        frame = new JFrame();
        offscreenImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        onscreenImage  = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        offscreen = offscreenImage.createGraphics();
        onscreen  = onscreenImage.createGraphics();
        setXscale();
        setYscale();
        offscreen.setColor(DEFAULT_CLEAR_COLOR);
        offscreen.fillRect(0, 0, width, height);
        setPenColor();
        setPenRadius();
        setFont();
        clear();
       Â
 add antialiasing
        RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
                                                  RenderingHints.VALUE_ANTIALIAS_ON);
        hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        offscreen.addRenderingHints(hints);
       Â
 frame stuff
        ImageIcon icon = new ImageIcon(onscreenImage);
        JLabel draw = new JLabel(icon);
        draw.addMouseListener(std);
        draw.addMouseMotionListener(std);
        frame.setContentPane(draw);
        frame.addKeyListener(std);   Â
 JLabel cannot get keyboard focus
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);           Â
 closes all windows
       Â
 frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);     Â
 closes only cu
ent window
        frame.setTitle("Standard Draw");
        frame.setJMenuBar(createMenuBar());
        frame.pack();
        frame.requestFocusInWindow();
        frame.setVisible(true);
    }
   Â
 create the menu bar (changed to private)
    private static JMenuBar createMenuBar() {
        JMenuBar menuBar = new JMenuBar();
        JMenu menu = new JMenu("File");
        menuBar.add(menu);
        JMenuItem menuItem1 = new JMenuItem(" Save...   ");
        menuItem1.addActionListener(std);
        menuItem1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
                                Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
        menu.add(menuItem1);
        return menuBar;
    }
   /*************************************************************************
    *  User and screen coordinate systems
    *************************************************************************
    /**
     * Se...