Skip to content

Lab 6: Shapes and Houses

For this lab, we will make use StdDraw.java developed by Robert Sedgewick and Kevin Wayne at Princeton University. This class provides a set of methods for creating simple drawings. You can find the complete documentation online, but today we will focus on the following methods:

StdDraw reference

  • circle​(double x, double y, double radius)
    Draws a circle of the specified radius, centered at (x, y).

  • filledCircle​(double x, double y, double radius)
    Draws a filled circle of the specified radius, centered at (x, y).

  • rectangle​(double x, double y, double halfWidth, double halfHeight)
    Draws a rectangle of the specified size, centered at (x, y).

  • filledRectangle​(double x, double y, double halfWidth, double halfHeight)
    Draws a filled rectangle of the specified size, centered at (x, y).

  • line​(double x0, double y0, double x1, double y1)
    Draws a line segment between (x0, y0) and (x1, y1).

  • setPenColor(int red, int green, int blue)
    Sets the pen color to the specified RGB color.

Part 1: Getting Started

  1. Download and move the following JAR file into your lib folder:

  2. Create a lab06 folder (under src/labs) and download the following files:

  3. Open DrawDemo.java and take a minute to read over the code. Then run the program.

  4. At the end of main(), set the pen color to 69 red, 0 green, and 132 blue.

    • These are the official RGB values for JMU Purple.
  5. Modify DrawDemo.java to draw a small, purple filled rectangle in the lower-right corner.

    • The location (0, 0) is in the lower-left corner of the StdDraw window.
    • The location (1, 1) is in the upper-right corner of the StdDraw window.

Please show your modified DrawDemo.java to the instructor or TA before moving on.

Part 2: Drawing Houses

  1. Open Houses.java and take a minute to read over the code. Then run the program.

  2. Add a new method drawHouse(double x, double y) that draws the following shape:

    simple drawing of a house

    • The total width of the house should be 0.2.
    • The total height of the house should be 0.3. The rectangle at the bottom should have a height of 0.2, and the roof should have a height of 0.1.
    • The rectangle that forms the bottom part of the house should be centered at (x, y). In other words, the location of the house should be determined by the parameter values.
    • The method should include a call to drawDoor(). DO NOT COPY THE CONTENTS OF drawDoor() INTO drawHouse()!
  3. Modify your main() method to include three calls to drawHouse(). The houses should be drawn at (0.2, 0.5), (0.5, 0.5), and (0.8, 0.5). The result should look like this:

    drawing with three houses

If You Have Time

  1. Add a drawWindow() method that you can call from your drawHouse() method. Your house should have at least two windows.

  2. Add a drawStreet() method that you can call from your main() method. The street should be drawn in front of the houses.

Submission

Upload the following two files to Canvas:

  • Houses.java – Your code should use methods properly to receive full credit. No javadocs or checkstyle required, but your code should be easy to read.

  • Houses.png – Save the image generated by your program, even if it is empty. You can save the image from the "File" menu of the StdDraw drawing window.