Results 1 to 5 of 5
  1. #1
    06blawton is offline Member
    Join Date
    Nov 2014
    Posts
    1
    Rep Power
    0

    Default BlueJ: Generating a multicoloured wall...

    Hello,

    I'm trying to generate a multicoloured brick wall as part of an assignment using BlueJ.

    According to the instructions we've been given, once the wall is set up and we invoke the draw() method, if we invoke the toggleMultiColoured method the bricks will go from being all read to a mix of 6 different colours...

    ...When I invoke the toggleMultiColoured method, however, the wall I generate is still red.

    Here's the source code for the BrickWall class I wrote:

    Java Code:
    public class BrickWall
    {
        private int bWidth;
        private int bHeight;
        private int numRows;
        private int rowLength;
        private ArrayList<String> colors;
        private ArrayList<Rectangle> bricks;
        private boolean decrease;
        private boolean isSymmetric;
        private boolean isMultiColor;
        private int currentColor;
        private int startX;
        private int startY;
    
        public BrickWall(int rows, int 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");
        }
    
        public void toggleMultiColour() {
            isMultiColor = !isMultiColor;
            currentColor = 0;
        }
    
        public void toggleSymmetric() {
            isSymmetric = !isSymmetric;
        }
    
        public void toggleDecrease() {
            decrease = ! decrease;
        }
        
        public int getNumberOfBricks() {
            return bricks.size();
        }
     
        public void setRowLength(int len) {
            if (len <= 0 || len > 22) {
                rowLength = 22;
            } else {
                rowLength = len;
            }
        }
    
        
        public void setNumRows(int rows) {
            if (rows <= 0 || rows > 30) {
                numRows = 30;
            } else {
                numRows = rows;
            }
        }
    
    
        public void draw()
        {
            eraseWall();
            int i = 0;
            int yCord = 550; 
           while (i < this.numRows) {
            drawRow(10, yCord, this.rowLength);
            yCord = yCord + 5;
            i = i + 1; 
           }
        }
    
        public void eraseWall() {
            Canvas canvas = Canvas.getCanvas();
            for (int i=0; !bricks.isEmpty(); i++) {
                canvas.erase(bricks.remove(0));
            }
        }
        
        private void makeBrick(int x, int y) {
            Rectangle newBrick = new Rectangle();
            newBrick.changeSize(54, 16);
            newBrick.setPosition(x, y); 
            bricks.add(newBrick);
            newBrick.makeVisible();
        }
        
        private void drawRow(int x, int y, int numberOfBricks) {
            int i = 0; 
            while (i < numberOfBricks) {
                makeBrick(x, y); 
                x = x + 54;
                i = i + 1; 
            }
        }

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: BlueJ: Generating a multicoloured wall...

    Well the problem must be in the code which you did not post - the code which determines the color of a brick.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: BlueJ: Generating a multicoloured wall...

    And why are you using Canvas? If you are going to be painting stuff I would recommend using Swing components (e.g. JPanel, JFrame, etc).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: BlueJ: Generating a multicoloured wall...

    That Canvas is definitely not java.awt.Canvas.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: BlueJ: Generating a multicoloured wall...

    Quote Originally Posted by DarrylBurke View Post
    That Canvas is definitely not java.awt.Canvas.
    Oops! I didn't look that closely at it.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. 3d wall not rendering correctly
    By Andrew_3ds in forum New To Java
    Replies: 0
    Last Post: 05-13-2014, 04:37 AM
  2. Brick wall using BLEUJ
    By boumasmoud in forum New To Java
    Replies: 1
    Last Post: 11-03-2011, 10:33 PM
  3. Only one wall block?
    By werner291 in forum New To Java
    Replies: 4
    Last Post: 09-28-2011, 01:47 PM
  4. Wall bug on physics simulation
    By mustachMan in forum New To Java
    Replies: 2
    Last Post: 07-14-2010, 05:56 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
  •