Draw a crate with Java

In this post we are going to draw a crate. This is a quiz that I had to solve for the course I am taking at Udacity.com; Introduction to programming. Thanks to professor Cay Horstman for making this assigment simple by constructing a simple graphic library that we will be using to solve this problem.

Download the java graphic library here:

Simple graphic library

 

We need to draw the following crate:

Image

Instructions:

// Draw a crate by combining a rectangle with five lines.
//
// The front face is a rectangle with top left corner (20, 30),
// width 100, and height 40.
//
// A diagonal line goes from the face’s top left corner to (50, 10).
// Another diagonal line goes from the face’s bottom right corner
// to (150, 50).
// A third diagonal starts at the face’s top right corner and is
// parallel to the others.
// The fourth and fifth line join the end points of the diagonals.
//
// Before programming, get out a pen and draw the crate on a
// sheet of paper! Label each of the points with their coordinates.
//
// Also, don’t forget to call draw() on your rectangle and line objects.

 

Solution:

public class DrawACrate
{
public static void main(String[] args)
       {
        Rectangle face = new Rectangle(20, 30, 100, 40);
        face.draw();
        Line line1 = new Line(20, 30, 50, 10);
        line1.draw();
        Line line2 = new Line(120, 30, 150, 10);
        line2.draw();
        Line line3 = new Line(50, 10, 150, 10);
        line3.draw();
        Line line4 = new Line(150, 50, 150, 10);
        line4.draw();
        Line line5 = new Line(120, 70, 150, 50);
        line5.draw();
    }
}

You can try it at Udacity: Here

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s