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: I can't figure out why this isn't working!

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

    Post I can't figure out why this isn't working!

    On my current assignment I have to create a brick wall of multi-coloured tiles in java. All using blueJ and as you can see below my code on the "drawRow" method won't work, it just draws a single row and wont actually draw 4 rows with 8 bricks by length when I ask it to. Please help me and ask if you need more info...


    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;
            }
        }
     
        private void drawRow() {
           Integer row = 0;
           while (row <= rowLength) {
               makeBrick();
               startX = startX + 54;
               row++;
            }
        }
     
        private void makeBrick() {
            brick = new Rectangle();
            brick.makeVisible();
            brick.changeSize(bWidth, bHeight);
            brick.setPosition(startX, startY);
            bricks.add(brick);
        }
     
     
        /**
         * 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));
            }
        }
     
    }

    Many Thanks for the help in advance,

    samjoyboy.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: I can't figure out why this isn't working!

    A few questions, because I suspect you've confused yourself:
    1. Is the variable rowLength supposed to be the number of bricks in each row, or the number of rows that need to be created? I ask because you are incrementing a variable named row, while incrementing the position of the brick along the X axis only.
    2. Each row needs to have a different startY value, or each row will just be drawn over the previous. Where are you adjusting the Y coordinate to move down/up? Remember, X is East and West, and Y is North and South.
    3. Where are you telling each row to have 8 bricks, and for it to create 4 rows?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: I can't figure out why this isn't working!

    Quote Originally Posted by aussiemcgr View Post
    A few questions, because I suspect you've confused yourself:
    1. Is the variable rowLength supposed to be the number of bricks in each row, or the number of rows that need to be created? I ask because you are incrementing a variable named row, while incrementing the position of the brick along the X axis only.
    2. Each row needs to have a different startY value, or each row will just be drawn over the previous. Where are you adjusting the Y coordinate to move down/up? Remember, X is East and West, and Y is North and South.
    3. Where are you telling each row to have 8 bricks, and for it to create 4 rows?
    1. rowLength is how many bricks are in each row - this is declared when I create a new brickwall in BlueJ. The make brick creates a brick then the startX moves position of the next brick after the previous however I need to create 8 bricks in each row consisting of 4 rows to get a brick wall.
    2. each row needs to start below the previous which is why startY needs to add 16 after each row to go south of the previous row.
    3. I tell it to do this from the setRowLength and setNumRows methods.

    many thanks.

  4. #4
    Junior Member
    Join Date
    Nov 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: I can't figure out why this isn't working!

    solved...

         private void drawRow() {
            Integer row1 = 0;
            while (row1 < numRows) {
                for (Integer row = 0; row <= rowLength; row++) {
                    makeBrick();
                    startX = startX + 54;
                }
                startY = startY + 16;
                startX = 10;
                row1++;
            }
        }

    thanks anyhow guys.

Similar Threads

  1. Why isn't this code working?
    By tai8 in forum What's Wrong With My Code?
    Replies: 24
    Last Post: March 19th, 2012, 07:00 PM
  2. Why isn't this code working?
    By tai8 in forum What's Wrong With My Code?
    Replies: 33
    Last Post: March 12th, 2012, 03:23 PM
  3. my code isn't working an i can't figure out why
    By jack13580 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 18th, 2011, 12:21 PM
  4. Why isn't this working?
    By javapenguin in forum What's Wrong With My Code?
    Replies: 14
    Last Post: January 21st, 2011, 04:08 PM
  5. Can't figure out why my image isn't showing up!!
    By thrashingboy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 8th, 2010, 06:01 PM

Tags for this Thread