Results 1 to 5 of 5
  1. #1
    ubaldas is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default Code shortening problem

    Hello, world!

    I started learning Java three days ago, watching Stanford's lectures and now I am starting to actually do some programming. So I have started with the assignment which is to draw a target on the screen. I succeeded in completing this task but the code is too long imo. I think I could have used for or if loops somewhere but I do not have the "sense of a programmer" yet to know when the code looks good. So what I am asking is to help me shorten this code in some ways if it is possible. Thank You.

    And maybe you will comment what I am doing wrong...

    Java Code:
    import java.awt.*;
    import acm.graphics.*;
    import acm.program.*;
    
    public class Target extends GraphicsProgram {
    	/**
    	 *Draws a target made from 3 circles
    	 *each circle has its own radius which
    	 *is stated in the beggining of the code.
    	 */
    	private static final long serialVersionUID = 1L;
    	private static final double RAD_1 = 1.0;
    	private static final double RAD_2 = 0.65;
    	private static final double RAD_3 = 0.3;
    	
    	public void run() {
    		int xC = getWidth()/2;
    		int yC = getHeight()/2;
    		double xD1 = getD1();
    		double xD2 = getD2();
    		double xD3 = getD3();
    		GOval circle1 = new GOval(xC-getPx1(),yC-getPx1(),xD1,xD1);
    		GOval circle2 = new GOval(xC-getPx2(),yC-getPx2(),xD2,xD2);
    		GOval circle3 = new GOval(xC-getPx3(),yC-getPx3(),xD3,xD3);
    		circle1.setFillColor(Color.RED);
    		circle1.setColor(Color.RED);
    		circle2.setFillColor(Color.WHITE);
    		circle2.setColor(Color.WHITE);
    		circle3.setFillColor(Color.RED);
    		circle3.setColor(Color.RED);
    		circle1.setFilled(true);
    		circle2.setFilled(true);
    		circle3.setFilled(true);
    		add(circle1);
    		add(circle2);
    		add(circle3);
    	}
    	
    	//gets the diameter of each circle
    	
    	private double getD1() {
    		double i = getPx1() * 2;
    		return i;
    	}
    
    	private double getD2() {
    		double i = getPx2() * 2;
    		return i;
    	}
    	
    	private double getD3() {
    		double i = getPx3() * 2;
    		return i;
    	}
    	
    	//converts inches to pixels
    	
    	private double getPx1() {
    		double i = RAD_1 * 72;
    		return i;
    	}
    	
    	private double getPx2() {
    		double i = RAD_2 * 72;
    		return i;
    	}
    	
    	private double getPx3() {
    		double i = RAD_3 * 72;
    		return i;
    	}
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default Re: Code shortening problem

    We don't have access to the code you haven't posted, or to you actual requirements, so this is just a guess, but you could create a method that draws a circle of a certain size and color at a specified location. Then just call that method several times with different values.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    ubaldas is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default Re: Code shortening problem

    Quote Originally Posted by KevinWorkman View Post
    We don't have access to the code you haven't posted, or to you actual requirements, so this is just a guess, but you could create a method that draws a circle of a certain size and color at a specified location. Then just call that method several times with different values.
    You do not see the code I added to the thread?

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default Re: Code shortening problem

    Quote Originally Posted by ubaldas View Post
    You do not see the code I added to the thread?
    Yes, I see the code. But we don't have access to things like GraphicsProgram or GOval, as they are not part of the standard API. So any suggestions you receive might go outside the bounds of your question because we can't take into account what those classes handle- for example, I don't actually see any code that actually draws anything. But I have to assume that things like that are handled by code we can't see.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    ubaldas is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default Re: Code shortening problem

    Wow, haven't thought about that really! Thanks!
    Also I will try to implement your suggestion to create a method!

Similar Threads

  1. Help Shortening Code
    By GregoryNeal in forum New To Java
    Replies: 1
    Last Post: 03-06-2012, 12:29 AM
  2. shortening a java program?
    By exeye0h in forum New To Java
    Replies: 3
    Last Post: 02-22-2012, 03:29 AM
  3. Replies: 0
    Last Post: 02-21-2011, 11:50 AM
  4. Replies: 9
    Last Post: 09-21-2010, 04:15 PM
  5. problem in my code
    By wannabe in forum New To Java
    Replies: 5
    Last Post: 04-12-2010, 04:38 AM

Posting Permissions

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