1 Attachment(s)

Why doesn't this work? - Stanford cs106a Assignment 2.2 (target)

Hi,

This is my attempt at answer for the 2nd question of assignment 2 from stanfords cs106a course which is available online. The question is as follows:

Attachment 4965

I am aware that there are other ways to solve this problem (see here: Stanford CS106A Assignment 2 Target Solution) but I feel that my approach has the potential to be more "elegant" if I can get it to work.

At the moment when I run this code I get a window with only the big red circle. Can anyone tell me why it's not working as I hoped? I'm at a loss.

Thanks.

Code:

`/*`

import acm.graphics.*;

import acm.program.*;

import java.awt.*;

public class Target extends GraphicsProgram {

// Sets length of radius to one inch (72 pixels)

private static final int INITIAL_RADIUS = 72;

public void run() {

// For loop runs 3 times. 'i' begins at 3 because I want to use i to calculate the diameter of the circles,

// beginning with the largest. So at i = 3 the circle is full size, at i = 2 the circle is 2/3 of the full size

// and at i = 1 the circle is 1/3 of the full size.

for (int i = 3; i > 0; i--) {

// The x and y values ensure that the circle is centered in the window.

double x = (getWidth() / 2) - INITIAL_RADIUS;

double y = (getHeight() / 2) - INITIAL_RADIUS;

// Variable to use for circle dimensions, as explained earlier.

double diameter = (i / 3) * INITIAL_RADIUS * 2;

// Create circle.

GOval circle = new GOval(x, y, diameter, diameter);

// My intention here is for setFilled to be true for odd numbers so that the first, big circle and the third,

// smallest circle are filled red.

circle.setFilled(i % 2 != 0);

// Sets fill color to red

circle.setFillColor(Color.red);

// Draws circle

add(circle);

}

}

}

Re: Why doesn't this work? - Stanford cs106a Assignment 2.2 (target)

You fill the red circles and you set all the circles to be drawn in red; so you end up with one big red circle. Draw the second circle in white so that it paints over the first (largest) circle, drawn in red.

kind regards,

Jos

Re: Why doesn't this work? - Stanford cs106a Assignment 2.2 (target)

Thanks for the advice. I was thinking (mistakenly) that I only needed to specify the red filling because it would default to white, but of course it defaults to no filling at all. My attempt to rectify this was as follows, however I still get the same result:

Code:

`circle.setFilled(true);`

if (i % 2 != 0) {

circle.setFillColor(Color.red);

} else {

circle.setFillColor(Color.white);

}

Am I on the right track?

Re: Why doesn't this work? - Stanford cs106a Assignment 2.2 (target)

I think so, I can't really see what you're attempting to do given that small code snippet. Basically, you want to fill all circles and draw the largest one first; the colours are respectively red, white and red again.

kind regards,

Jos

Re: Why doesn't this work? - Stanford cs106a Assignment 2.2 (target)

Yes. Sorry I should have given it more context, but you have it right. I'm trying to draw three circles on top of each other, a big red one first, then a white one two thirds the size, and lastly another red one one third the size, so that it ends up looking like the target in the picture.

I realised I had another error in my code, where the x and y variables used to centre the circles were not adjusted according to the diminishing size of the circles. I've rectified this by creating a 'radius' variable that I use in the definitions of the x and y variably rather than the 'INITIAL_RADIUS' constant.

Unfortunately I'm still not getting the desired result. Here's the code as I have it now:

Code:

`import acm.graphics.*;`

import acm.program.*;

import java.awt.*;

public class Target extends GraphicsProgram {

// Sets length of radius to one inch (72 pixels)

private static final int INITIAL_RADIUS = 72;

public void run() {

// For loop runs 3 times. 'i' begins at 3 because I want to use i to calculate the diameter of the circles,

// beginning with the largest. So at i = 3 the circle is full size, at i = 2 the circle is 2/3 of the full size

// and at i = 1 the circle is 1/3 of the full size.

for (int i = 3; i > 0; i--) {

// 'radius' variable decreases by a third for each iteration of for-loop. Drawing circles from biggest to

// smallest allows layering to take place.

double radius = (i / 3) * INITIAL_RADIUS;

// 'diameter' variable used for defining size of circle

double diameter = radius * 2;

// The x and y values ensure that the circle is centered in the window. 'radius' variable is used so that

// circle stays centered despite changing size.

double x = (getWidth() / 2) - radius;

double y = (getHeight() / 2) - radius;

// Create circle.

GOval circle = new GOval(x, y, diameter, diameter);

// 'setFilled' is always true.

circle.setFilled(true);

// If 'i' is an odd number the circle will be painted red, if it is an even number it will be painted white.

if (i % 2 != 0) {

circle.setFillColor(Color.red);

}

else {

circle.setFillColor(Color.white);

}

add(circle);

}

}

}

Any advice much appreciated.

Re: Why doesn't this work? - Stanford cs106a Assignment 2.2 (target)

After a quick skimming over your code, it looks ok; what does the result look like now?

kind regards,

Jos

Re: Why doesn't this work? - Stanford cs106a Assignment 2.2 (target)

With the code as posted I get a big red circle that has a thin black outline and a black dot in the middle of the circle. I don't know where the problem is coming from.

Re: Why doesn't this work? - Stanford cs106a Assignment 2.2 (target)

Look at how you calculate the radius:

Code:

`double radius = (i / 3) * INITIAL_RADIUS;`

while mathematically ok, Java uses integer division because both 'i' and '3' are ints; integer division chops off the fraction part of the result so the radius ends up equal to zero for i < 3. Do this instead:

Code:

`double radius = (i * INITIAL_RADIUS)/3;`

kind regards,

Jos

Re: Why doesn't this work? - Stanford cs106a Assignment 2.2 (target)

That did it! Thank you!

I'm still left wondering why my circles have thin black outlines. Do you know why that would be?

Re: Why doesn't this work? - Stanford cs106a Assignment 2.2 (target)

Quote:

Originally Posted by

**rasta166** That did it! Thank you!

I'm still left wondering why my circles have thin black outlines. Do you know why that would be?

Nope, maybe your circle drawing method draws an outline? Check the manuals.

kind regards,

Jos

Re: Why doesn't this work? - Stanford cs106a Assignment 2.2 (target)

Strangely the black outlines dont appear when I use the approach linked to in my first post. Not a big deal really. Thanks again for the help. I would have been banging my head on the table for hours to come without it!

Re: Why doesn't this work? - Stanford cs106a Assignment 2.2 (target)

Quote:

Originally Posted by

**rasta166** Strangely the black outlines dont appear when I use the approach linked to in my first post. Not a big deal really. Thanks again for the help. I would have been banging my head on the table for hours to come without it!

I don't see much difference between the two code versions (except for the incorrect '(i/3)' expression); I have no idea where the black outlines come from ...

kind regards,

Jos