Results 1 to 3 of 3
  1. #1
    styerfoam is offline Member
    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0

    Default Trying to stagger GRect like stairs... Math issue

    I am working with the acm.jar library to help learn Java. I am working on an application for school that creates an IO Dialog asking users how many stairs they would like to draw.

    Then I am supposed to use a loop to draw those stairs. The first half to be blue the second half to be red.

    I am lost in the math of how to get the rectangles to stagger. I have tried several things. The code below includes my latest attempt which is still failing.

    If there is anyone who can help with just the math part I would be eternally grateful:

    Java Code:
    import acm.program.GraphicsProgram;
    import acm.io.IODialog;
    import acm.graphics.GRect;
    //import acm.graphics.GLabel;
    //import java.awt.Color;
    
    public class StairsGraphical extends GraphicsProgram {
    
    		public static final int APP_WIDTH = 500;
    		public static final int APP_HEIGHT = 300;
    		public static final double BOX_WIDTH = 75;
    		public static final double BOX_HEIGHT = 25;
    	
    		public void init()
    		{
    			//setting app size to display stairs
    			this.setSize(APP_WIDTH, APP_HEIGHT);
    			
    			//asking user for number of stairs
    			IODialog dialog = new IODialog();
    			int stairsValue = dialog.readInt("Number of Stairs: between 5 and 10");
    			
    			//GRect stairRect = new GRect (BOX_WIDTH, BOX_HEIGHT);
    			//for loop to display the stairs
    			double x = 1;
    			double y = 1;
    			
    			//add(stairRect);
    			
    			for (int i = 0; i <= stairsValue; i++)
    			{
    
    				GRect stairRect = new GRect(x, y, BOX_WIDTH, BOX_HEIGHT);
    				add(stairRect);
    				System.out.println("count is " + i);
    				/*x = 0;
    				y = stairRect.getHeight()*2;*/
    				stairRect.move(BOX_WIDTH/2,BOX_HEIGHT);
    			}
    		}
    		
    }
    Last edited by pbrockway2; 01-12-2012 at 09:47 PM. Reason: code tags added

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Trying to stagger GRect like stairs... Math issue

    I am lost in the math of how to get the rectangles to stagger
    Take a piece of graph paper (with a grid) and draw what you want the program to draw.
    Look at the x,y points for each rectangle. Make a list of those x,y points. There must be an easy equation that will allow you to compute the next x,y given the current x,y.

    If you need help finding the pattern, list the x,y points for the rectangles here.

  3. #3
    styerfoam is offline Member
    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0

    Default Re: Trying to stagger GRect like stairs... Math issue

    Thanks for the thought!!! I figured it out. I used i to help change the location of each rectangle

    x = BOX_WIDTH/2 * i;
    y = BOX_HEIGHT*i;
    stairRect.setLocation(x,y);

Similar Threads

  1. Math issue
    By Trevor in forum New To Java
    Replies: 10
    Last Post: 12-06-2011, 05:58 AM
  2. Math.cos()
    By ravi1 in forum New To Java
    Replies: 5
    Last Post: 03-27-2011, 02:52 PM
  3. Create Math.sin without math.sin
    By vudoo in forum New To Java
    Replies: 11
    Last Post: 12-07-2010, 06:23 AM
  4. GRect help
    By bokchoi in forum New To Java
    Replies: 2
    Last Post: 10-26-2010, 08:05 PM
  5. Moving GRect: Bad Code or Bug?
    By adamg106 in forum New To Java
    Replies: 11
    Last Post: 05-02-2010, 12:35 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
  •