2 Attachment(s)

Why doesn't it work? CS106a Assignment 2 - Target Problem

Hi, today is my first day here, so I am sorry if I am braking any rules. Thanks for your help!

Attachment 5102

Below is my solution:

Code:

`/*`

* File: Target.java

* Name:

* Section Leader:

* -----------------

* This program draws a Target.

*/

import acm.graphics.*;

import acm.program.*;

import java.awt.*;

public class Target extends GraphicsProgram {

/* Radius of Outer Circle */

private static final double R1 = 72;

/* Radius of Middle Circle */

private static final double R2 = 0.65 * R1;

/* Radius of Inner Circle */

private static final double R3 = 0.3 * R1;

public void run() {

add(circle(getWidth()/2-R1, getHeight()/2-R1, R1, Color.RED));

add(circle(getWidth()/2-R2, getHeight()/2-R2, R2, Color.GREEN));

add(circle(getWidth()/2-R3, getHeight()/2-R3, R3, Color.RED));

}

private GOval circle(double x, double y, double r, Color c) {

GOval circle = new GOval(x-r, y-r, 2*r, 2*r);

circle.setFilled(true);

circle.setColor(c);

return circle;

}

}

However, when I run the program, the following result appears (I made the middle circle green so I can see its location better)

Attachment 5103

Re: Why doesn't it work? CS106a Assignment 2 - Target Problem

You're not using standardized graphics methods or classes from the JDK so it would be hard to say. However, the circles should be concentric so that means they need to have the same center coordinates.

Regards,

Jim

Re: Why doesn't it work? CS106a Assignment 2 - Target Problem

Jim thanks for your reply. You are right, I am not using JDK. Instead, I am using the ACM graphics package, that the professor of this class uses. The parameters to draw a circle are (x, y, width, height) where x and y are the coordinates of the upper left hand corner of a square circumscribed about the circle and width == height == 2 * r. Therefore, to find the coordinates of each circle, I need to calculate the center coordinates (in this case center of the applet) and subtract the radius from x and y. I hope this makes sense.

Re: Why doesn't it work? CS106a Assignment 2 - Target Problem

I think I see the problem. Don't subtract r again from x and y in your circle method.

Regards,

Jim

Re: Why doesn't it work? CS106a Assignment 2 - Target Problem

You are absolutely right! Thank you once again! :(grin):