Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Make brick wall using BlueJ

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Make brick wall using BlueJ

    Hi everybody,

    I have the following question which has to do with my homework for Java.

    It`s about this question: "You will probably find it easier to write the code to
    draw your wall if you first write a private method called drawRow which draws a row of n
    bricks starting at coordinates (x, y). Another private method called makeBrick
    should create a new Rectangle object at the position (X,Y) and make it visible. Each
    brick should be created as a Rectangle object, 54 pixels wide and 16 pixels high. The
    rectangle object representing the brick should be added to the ArrayList called
    bricks."


    I have written the following code for the above two methods. My question is how can I create a row of 8 bricks. I have managed to get one brick to be shown but they need to be placed next to eachother. So how do I place them next to each other? See my two methods down here. Beneath the methods you will find the rest of the code of the whole class.

    /**
    * Create a new rectangle object representing a brick and add it to the arraylist.
    */
    private void makeBrick() {
    brick = new Rectangle();
    brick.makeVisible();
    brick.changeSize(54, 16);
    brick.moveHorizontal(10);
    brick.moveVertical(550);
    bricks.add(brick);
    }


    /**
    * Create a new row of 8 bricks.
    */
    private void drawRow() {
    Integer r=0;

    while(r < 9){
    makeBrick();
    r++;

    }
    }


    import java.util.ArrayList;
    /**
    * Write a description of class BrickWall here.
    *
    * @author (your name)
    * @version (a version number or a date)
    */
    public class BrickWall
    {
    // instance variables - replace the example below with your own
    private Integer bWidth;
    private Integer bHeight;
    private Integer numRows;
    private Integer rowLength;
    private ArrayList<String> colors;
    private ArrayList<Rectangle> bricks;
    private Boolean decrease;
    private Boolean isSymmetric;
    private Boolean isMultiColor;
    private Integer currentColor;
    private Integer startX;
    private Integer startY;
    private Rectangle brick;

    /**
    * Constructor for objects of class BrickWall.
    * @param rows The number of rows in the wall
    * @param rowlen The maximum number of bricks in a row
    * @param isMultiColor when false all bricks are the same colour. When true the colours cycle through the 6 colours.
    * @param decrease If true subtract 2 from rowLen for each row drawn
    * @param isSymmetric if decrease is greater than 0 and isSymmetric is true the wall is a pyramid shape, otherwise it is a rectangle or a right angle triangle.
    */
    public BrickWall(Integer rows, Integer rowLen)
    {
    // initialise instance variables
    setUpColors();
    bWidth = 54;
    bHeight = 16;
    startX = 10;
    startY = 550;
    bricks = new ArrayList<Rectangle>();
    currentColor = 0;
    setNumRows(rows);
    setRowLength(rowLen);
    this.isMultiColor = false;
    this.decrease = false;
    this.isSymmetric = false;
    }

    private void setUpColors() {
    colors = new ArrayList<String>();
    colors.add("red");
    colors.add("yellow");
    colors.add("blue");
    colors.add("green");
    colors.add("magenta");
    colors.add("black");
    }

    /**
    * Toggle whether the wall is multicoloured.
    */
    public void toggleMultiColour() {
    isMultiColor = !isMultiColor;
    currentColor = 0;
    }

    /**
    * Toggle whether the decrease in a row length is symmetric.
    */
    public void toggleSymmetric() {
    isSymmetric = !isSymmetric;
    }

    /**
    * Toggle whether the length of a new row is one less than the length of the previous row.
    */
    public void toggleDecrease() {
    decrease = ! decrease;
    }

    /**
    * @return the number of bricks in the current wall.
    */
    public Integer getNumberOfBricks() {
    return bricks.size();
    }

    /**
    * Set the length of a row. There can be no more than 22 bricks in a row.
    * @param len The number of bricks in a row. If len is less than or equal
    * to zero OR len is greater than 22, the row length will be assigned the value 22.
    * Otherwise the length of a row will be assigned the value of len.
    */
    public void setRowLength(Integer len) {
    if (len <= 0 || len > 22) {
    rowLength = 22;
    } else {
    rowLength = len;
    }
    }

    /**
    * Set the maximum number of rows in the wall. If the length of a row decreases,
    * there may not be this many rows in the wall.
    * @param rows The maximum number of rows in the wall. If rows is less than
    * or equal to zero OR rows is greater than 30, the number of rows will be assigned the value 30.
    * Otherwise the number of rows will be assigned the value of rows.
    */
    public void setNumRows(Integer rows) {
    if (rows <= 0 || rows > 30) {
    numRows = 30;
    } else {
    numRows = rows;
    }
    }

    /**
    * Create a new rectangle object representing a brick and add it to the arraylist.
    */
    private void makeBrick() {
    brick = new Rectangle();
    brick.makeVisible();
    brick.changeSize(54, 16);
    brick.moveHorizontal(10);
    brick.moveVertical(550);
    bricks.add(brick);
    }


    /**
    * Create a new row of 8 bricks.
    */
    private void drawRow() {
    Integer r=0;

    while(r < 9){
    makeBrick();
    r++;

    }
    }


    /**
    * Draw the wall. The first brick will be positioned at the coordinates (10, 550).
    * The number of bricks in a row is specified by setRowLength(). The maximum number of rows
    * is specified by setNumRows(). If decrease is true, each subsequent row of bricks
    * contains one brick less than the previous row. If symmetric is true AND decrease is true then
    * the wall is pyramid shaped. If symmetric is false AND decrease is true then the wall is shaped
    * like a right angle triangle.
    */
    public void draw()
    {
    eraseWall();
    //------------------------------


    drawRow();


    boolean decrease = false;
    boolean symmetric = false;



    //------------------------------
    }


    public void eraseWall() {
    Canvas canvas = Canvas.getCanvas();
    for (int i=0; !bricks.isEmpty(); i++) {
    canvas.erase(bricks.remove(0));
    }
    }

    }


  2. #2
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: Make brick wall using BlueJ

    Try using a for loop.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Make brick wall using BlueJ

    Do you have an x location you use to define where the brick is located?
    Do you change that value for each brick so each brick is where you want it to be?

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Make brick wall using BlueJ

    Brick wall using BLEUJ

    Duplicate post.
    Improving the world one idiot at a time!

Similar Threads

  1. Help in programming question. Bluej complier
    By Rads23 in forum What's Wrong With My Code?
    Replies: 12
    Last Post: August 3rd, 2011, 09:12 PM
  2. Replies: 7
    Last Post: July 21st, 2011, 02:29 PM
  3. Increase heap size; 4 GB wall?
    By BKB in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 13th, 2010, 08:55 PM
  4. Destroy Brick
    By Ceasar in forum Java Theory & Questions
    Replies: 2
    Last Post: October 10th, 2009, 04:36 AM
  5. Simple java program to calculate wall covering of a room
    By parvez07 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 22nd, 2009, 03:31 PM