cs106a – Assignment #2 – Task #2

Suppose that you’ve been hired to produce a program that draws an image of an archery target—or, if you prefer commercial applications, a logo for a national department store chain—that looks like this:

cs106a – assignment #2 – task #2

This figure is simply three GOval objects, two red and one white, drawn in the correct order. The outer circle should have a radius of one inch (72 pixels), the white circle has a radius of 0.65 inches, and the inner red circle has a radius of 0.3 inches. The figure should be centered in the window of a GraphicsProgram subclass.

To draw a circle create an oval with using the diameter (twice the radius), and place it at the given position:

	private void drawCircle(double cx, double cy, double r, Color c) {
		double x = cx - r;
		double y = cy - r;
		GOval circle = new GOval(2*r, 2*r);
		circle.setColor(c);
		circle.setFilled(true);
		add(circle, x, y);
	}

Finally draw the three circles (largest first) using the parameters as constants:

	public void run() {
		double x = getWidth() / 2;
		double y = getHeight() /2;
		drawCircle(x, y, RADIUS_OUTER_CIRCLE * PIXELS_PER_INCH, Color.RED);
		drawCircle(x, y, RADIUS_WHITE_CIRCLE * PIXELS_PER_INCH, Color.WHITE);
		drawCircle(x, y, RADIUS_INNER_CIRCLE * PIXELS_PER_INCH, Color.RED);
	}

The code for this assignment is available on github.

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

Leave a Reply

Your email address will not be published.