Results 1 to 12 of 12
 05122013, 01:36 PM #1Member
 Join Date
 May 2013
 Posts
 6
 Rep Power
 0
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:
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.
Java 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); } } }
 05122013, 01:47 PM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,390
 Blog Entries
 7
 Rep Power
 25
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,
JosThe only person who got everything done by Friday was Robinson Crusoe.
 05122013, 02:27 PM #3Member
 Join Date
 May 2013
 Posts
 6
 Rep Power
 0
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:
Java Code:circle.setFilled(true); if (i % 2 != 0) { circle.setFillColor(Color.red); } else { circle.setFillColor(Color.white); }
 05122013, 02:43 PM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,390
 Blog Entries
 7
 Rep Power
 25
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,
JosThe only person who got everything done by Friday was Robinson Crusoe.
 05122013, 03:47 PM #5Member
 Join Date
 May 2013
 Posts
 6
 Rep Power
 0
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:
Java 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 forloop. 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); } } }
 05122013, 04:05 PM #6
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,390
 Blog Entries
 7
 Rep Power
 25
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,
JosThe only person who got everything done by Friday was Robinson Crusoe.
 05122013, 04:33 PM #7Member
 Join Date
 May 2013
 Posts
 6
 Rep Power
 0
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.
 05122013, 04:52 PM #8
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,390
 Blog Entries
 7
 Rep Power
 25
Re: Why doesn't this work?  Stanford cs106a Assignment 2.2 (target)
Look at how you calculate the radius:
Java Code:double radius = (i / 3) * INITIAL_RADIUS;
Java Code:double radius = (i * INITIAL_RADIUS)/3;
JosThe only person who got everything done by Friday was Robinson Crusoe.
 05122013, 04:56 PM #9Member
 Join Date
 May 2013
 Posts
 6
 Rep Power
 0
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?
 05122013, 05:38 PM #10
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,390
 Blog Entries
 7
 Rep Power
 25
 05122013, 05:50 PM #11Member
 Join Date
 May 2013
 Posts
 6
 Rep Power
 0
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!
 05122013, 06:20 PM #12
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,390
 Blog Entries
 7
 Rep Power
 25
Re: Why doesn't this work?  Stanford cs106a Assignment 2.2 (target)
The only person who got everything done by Friday was Robinson Crusoe.
Similar Threads

Stanford CS106a breakout assignment
By Newbieprogrammer in forum New To JavaReplies: 3Last Post: 08142012, 10:18 AM 
CheckerBoardKarel  Stanford CS106A Assignment 1
By mattdotmac in forum New To JavaReplies: 6Last Post: 07152012, 01:58 PM 
Stanford cs106a
By D.good in forum IntroductionsReplies: 1Last Post: 02042012, 07:18 PM 
CS106A Stanford University
By Learning Java in forum New To JavaReplies: 116Last Post: 07092011, 04:43 PM 
Stanford CS106A, my work your thoughts?
By chmza in forum New To JavaReplies: 2Last Post: 12022010, 06:53 PM
Bookmarks