Results 1 to 12 of 12
  1. #1
    rasta166 is offline Member
    Join Date
    May 2013
    Posts
    6
    Rep Power
    0

    Default 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:

    Why doesn't this work? - Stanford cs106a Assignment 2.2 (target)-captura-de-pantalla-2013-05-12-la-s-21.01.37.png

    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);
    			}
    				
    		}
    		
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    rasta166 is offline Member
    Join Date
    May 2013
    Posts
    6
    Rep Power
    0

    Default 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);
    }
    Am I on the right track?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    rasta166 is offline Member
    Join Date
    May 2013
    Posts
    6
    Rep Power
    0

    Default 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 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.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    rasta166 is offline Member
    Join Date
    May 2013
    Posts
    6
    Rep Power
    0

    Default 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.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default 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;
    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:

    Java Code:
    double radius = (i * INITIAL_RADIUS)/3;
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    rasta166 is offline Member
    Join Date
    May 2013
    Posts
    6
    Rep Power
    0

    Default 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?

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

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

    Quote Originally Posted by rasta166 View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    rasta166 is offline Member
    Join Date
    May 2013
    Posts
    6
    Rep Power
    0

    Default 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!

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

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

    Quote Originally Posted by rasta166 View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Stanford CS106a breakout assignment
    By Newbieprogrammer in forum New To Java
    Replies: 3
    Last Post: 08-14-2012, 11:18 AM
  2. CheckerBoardKarel - Stanford CS106A Assignment 1
    By mattdotmac in forum New To Java
    Replies: 6
    Last Post: 07-15-2012, 02:58 PM
  3. Stanford cs106a
    By D.good in forum Introductions
    Replies: 1
    Last Post: 02-04-2012, 07:18 PM
  4. CS106A Stanford University
    By Learning Java in forum New To Java
    Replies: 116
    Last Post: 07-09-2011, 05:43 PM
  5. Stanford CS106A, my work- your thoughts?
    By chmza in forum New To Java
    Replies: 2
    Last Post: 12-02-2010, 06:53 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •