Just complete the bronze part

1 answer below »
Just complete the bronze part
Answered Same DayMar 09, 2021

Answer To: Just complete the bronze part

Mohd answered on Mar 09 2021
136 Votes
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 reminder

        // 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;
  // array of Shapes
  private Shape[] parts;
  // current 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 array
  public void addShape(Shape newShape){
    parts[numParts++] = newShape; 
  }

  // Draw All the shapes stored in this array. 
  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{
    // Current position of the point
    public double x;
    public double y;

    // current 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 current 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, overridden 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 current 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 library. This class provides a basic capability for
 *  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 flicker
 *
 *************************************************************************/
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. This class provides a basic capability for
 *  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 Section 1.5 of
 *Introduction to Programming in Java: An Interdisciplinary Approach 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;
    // current pen color
    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;
    // current 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% border
    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);
    // current 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 currently pressed down
    private static TreeSet keysDown = new TreeSet();

    // singleton pattern: client can't instantiate
    private StdDraw() { }
    // static initializer
    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 current 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...

SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here