Results 1 to 3 of 3
- 01-12-2012, 09:24 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 2
- Rep Power
- 0
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
- 01-13-2012, 01:50 AM #2
Re: Trying to stagger GRect like stairs... Math issue
Take a piece of graph paper (with a grid) and draw what you want the program to draw.I am lost in the math of how to get the rectangles to stagger
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.
- 01-13-2012, 03:35 AM #3
Member
- Join Date
- Jan 2012
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Math issue
By Trevor in forum New To JavaReplies: 10Last Post: 12-06-2011, 05:58 AM -
Math.cos()
By ravi1 in forum New To JavaReplies: 5Last Post: 03-27-2011, 02:52 PM -
Create Math.sin without math.sin
By vudoo in forum New To JavaReplies: 11Last Post: 12-07-2010, 06:23 AM -
GRect help
By bokchoi in forum New To JavaReplies: 2Last Post: 10-26-2010, 08:05 PM -
Moving GRect: Bad Code or Bug?
By adamg106 in forum New To JavaReplies: 11Last Post: 05-02-2010, 12:35 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks