Use Java to Draw a Brick Wall

11 Replies - 13740 Views - Last Post: 04 November 2012 - 07:31 PM Rate Topic: - - - - -

#1

  • New D.I.C Head

Reputation: 0

  • View blog
  • Posts: 6
  • Joined: 03-November 12

Building a brick wall with while loops.

Posted 03 November 2012 - 09:40 AM

I'm having trouble going to the next row after building a line of bricks.

Integer n = 0;         while (n<=rowLength) {             makeBrick(startX, startY);             System.out.println();             n=n+1;             n++;                

This code only builds one row of bricks even after telling it to build four rows. I am using BlueJ to write the code. I already have the other codes written but this is the code I'm having problems with.
My makeBrick code draws the rectangle/brick.


Is This A Good Question/Topic? 0

  • +

Replies To: Building a brick wall with while loops.

#2 GregBrannon User is offline

Reputation: 2250

  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Building a brick wall with while loops.

Posted 03 November 2012 - 09:50 AM

Please post enough code to show whole clauses and enough to see what's going on.

Why n = n + 1 AND n++?:


#3 L0V3E User is offline

  • New D.I.C Head

Reputation: 0

  • View blog
  • Posts: 6
  • Joined: 03-November 12

Re: Building a brick wall with while loops.

Posted 03 November 2012 - 10:03 AM

View PostGregBrannon, on 03 November 2012 - 09:50 AM, said:

Please post enough code to show whole clauses and enough to see what's going on.

Why n = n + 1 AND n++?:

I'm sorry for the lack of info.

this is my makebrick code:

private void makeBrick(int width, int height) {         brick = new Rectangle();         brick.makeVisible();         brick.changeSize(bWidth,bHeight);         brick.setPosition(startX, startY);         brick.changeColor(colors.get(0));         startX = startX + 54;         bricks.add(brick);         }                

For setting the number of rows

                  public void setNumRows(Integer rows) {         if (rows <= 0 || rows > 30) {             numRows = 30;         } else {             numRows = rows;         }     }                
public void setRowLength(Integer len) {         if (len <= 0 || len > 22) {             rowLength = 22;         } else {             rowLength = len;         }     }                

I can only get one line of bricks to draw.


#4 GregBrannon User is offline

Reputation: 2250

  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Building a brick wall with while loops.

Posted 03 November 2012 - 10:14 AM

And where is the number of rows used in the drawing of the wall?


#5 L0V3E User is offline

  • New D.I.C Head

Reputation: 0

  • View blog
  • Posts: 6
  • Joined: 03-November 12

Re: Building a brick wall with while loops.

Posted 03 November 2012 - 10:21 AM

View PostGregBrannon, on 03 November 2012 - 10:14 AM, said:

And where is the number of rows used in the drawing of the wall?

i've put up the code in which I set the number of rows and I have to input the specified number of rows after compiling the code before it draws so there isn't a specific number of rows in the code.


#6 GregBrannon User is offline

Reputation: 2250

  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Building a brick wall with while loops.

Posted 03 November 2012 - 10:25 AM

Show me the code that says:

for ( numberOfRows ) {     for ( thisRowLength )     {         // draw row     }      // move to next row }                


#7 L0V3E User is offline

  • New D.I.C Head

Reputation: 0

  • View blog
  • Posts: 6
  • Joined: 03-November 12

Re: Building a brick wall with while loops.

Posted 03 November 2012 - 10:29 AM

View PostGregBrannon, on 03 November 2012 - 10:25 AM, said:

Show me the code that says:

for ( numberOfRows ) {     for ( thisRowLength )     {         // draw row     }      // move to next row }                  

I'm using only while loops and this is what i did:

                  private void drawRow() {         Integer n = 0;          while (n<=rowLength) {             makeBrick(startX, startY);             System.out.println();             n=n+1;             n++;         }                


#8 GregBrannon User is offline

Reputation: 2250

  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Building a brick wall with while loops.

Posted 03 November 2012 - 11:07 AM

Assuming you've used Java conventions and X measures horizontal change (columns) and Y measures vertical change (rows) from a top left origin, startX varies by some deltaX to draw the entire row of bricks. I'm not sure how that happens either, but you say it is. At the end of the row (when n = rowLength in what you've posted), deltaX should become 0 and startY should vary by some deltaY to move to the next row. Where does that happen?


#9 L0V3E User is offline

  • New D.I.C Head

Reputation: 0

  • View blog
  • Posts: 6
  • Joined: 03-November 12

Re: Building a brick wall with while loops.

Posted 04 November 2012 - 08:33 AM

View PostGregBrannon, on 03 November 2012 - 11:07 AM, said:

Assuming you've used Java conventions and X measures horizontal change (columns) and Y measures vertical change (rows) from a top left origin, startX varies by some deltaX to draw the entire row of bricks. I'm not sure how that happens either, but you say it is. At the end of the row (when n = rowLength in what you've posted), deltaX should become 0 and startY should vary by some deltaY to move to the next row. Where does that happen?

I haven't done that because that is where my problem is coming from. Are you suggesting I change the starting positions to 0 to go to the next line after the first rows have been drawn? Sorry, I'm quite new to this so I'm having a lot of difficulty.


#10 GregBrannon User is offline

Reputation: 2250

  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Building a brick wall with while loops.

Posted 04 November 2012 - 09:08 AM

Yes, that's what I'm suggesting. While you can jump around and draw it any way you please, drawing it methodically in rows, each one after the previous, is probably the easiest to program.

Since the brick pattern is typically offset by 1/2 a brick, you'll have to adjust the first deltaX of each row. Other than that,

deltaX = the length dimension of the brick
deltaY = the height dimension of the brick

Good luck!


#11 d2r123 User is offline

  • New D.I.C Head

Reputation: 3

  • View blog
  • Posts: 17
  • Joined: 12-December 08

Re: Building a brick wall with while loops.

Posted 04 November 2012 - 07:21 PM

It appears as through your make brick function actualy alters the value of startX.
If startX is supposed to be the starting possition of x, it would be best to have that variable remain the same while building the wall. The same is true for startY. Your brick making function should insted place each brick using startX+theCurrentXOffset. The offset should be something like equal to the number of bricks you have made in the row - one * the x size of a brick.
Each y row should use an ofset of the y hight of a brick, other wise you will just print on top of your original row.

Something like

Startx=whatever Starty=whatever NumRows=whatever NumBricksPerRow=whatever ThisRow=1; while(ThisRow<=NumRows) {   if(ThisRow is even)   {     makeBricks(startx+HalfABrickx, Starty+ThisRow*ABricky)     ThisRow=ThisRow+1;   }    if(ThisRow is odd)   {     makeBricks(startx, Starty+ThisRow*ABricky)     ThisRow=ThisRow+1;   } }                

Exept with proper grammer.


#12 pbl User is offline

Reputation: 8381

  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Building a brick wall with while loops.

Posted 04 November 2012 - 07:31 PM

private void drawRow() {        Integer n = 0;         while (n<=rowLength) {            makeBrick(startX, startY);            System.out.println();            n=n+1;    // why n = n + 1            n++;      // foloowed by n++        }                

why not simply n += 2; ???
And why Integer n = 0; and not simply int n = 0 ???


  • ← Previous Topic
  • Java
  • Next Topic →

Use Java to Draw a Brick Wall

Source: https://www.dreamincode.net/forums/topic/298475-building-a-brick-wall-with-while-loops/

0 Response to "Use Java to Draw a Brick Wall"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel