Results 1 to 5 of 5
Like Tree2Likes
  • 1 Post By jim829
  • 1 Post By jim829

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

  1. #1
    Sasha is offline Member
    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0

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

    Why doesn't it work? CS106a Assignment 2 - Target Problem-target-problem.png

    Below is my solution:

    Java 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)
    Why doesn't it work? CS106a Assignment 2 - Target Problem-target-applet.jpg

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,525
    Rep Power
    5

    Default 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
    Sasha likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Sasha is offline Member
    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0

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

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,525
    Rep Power
    5

    Default 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
    Sasha likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    Sasha is offline Member
    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0

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

    You are absolutely right! Thank you once again!

Similar Threads

  1. CS106a assignment 3 help
    By top7gunn in forum New To Java
    Replies: 7
    Last Post: 05-22-2013, 08:49 PM
  2. Replies: 11
    Last Post: 05-12-2013, 06:20 PM
  3. Stanford CS106a breakout assignment
    By Newbieprogrammer in forum New To Java
    Replies: 3
    Last Post: 08-14-2012, 10:18 AM
  4. CheckerBoardKarel - Stanford CS106A Assignment 1
    By mattdotmac in forum New To Java
    Replies: 6
    Last Post: 07-15-2012, 01:58 PM
  5. Breakout Assignment from CS106a
    By tech8462 in forum New To Java
    Replies: 13
    Last Post: 10-09-2011, 03:38 AM

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
  •