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: toggle colors from an array when the method is called.

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

    Default toggle colors from an array when the method is called.

    I have to make the bricks i print out from the drawRow method multi coloured by calling the method and using this to go through the colors per brick.

    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;
        public ArrayList<String> colors;
        public 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 row1 = 0;
            while (row1 < numRows) {
                for (Integer row = 0; row <= rowLength; row++) {
                    makeBrick();
                    startX = startX + 54;
                }
                startY = startY + 16;
                startX = 10;
                row1++;
            }
        }
     
        private void makeBrick() {
            brick = new Rectangle();
            brick.makeVisible();
            brick.changeSize(bWidth, bHeight);
            brick.setPosition(startX, startY);
            brick.changeColor(colors.get(0));
            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();
            if (isMultiColor = true) {
     
            }
            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));
            }
        }
     
    }

    As you can see above, my code the drawRow method creates a brick wall of however many bricks in length and however many rows I declare. however when i call the toggleMultiColored method it should use the ArrayList colours and for all the bricks it creates go through each ArrayList color creating a multi coloured effect with the final wall.

    Many thanks in advanced.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: toggle colors from an array when the method is called.

    The posted code does not compile without compiler errors. Do you get any errors compiling it?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: toggle colors from an array when the method is called.

    Hi again,

    So when you say it should go through each ArrayList color for all the bricks, are you saying each brick has all of the colors, or each brick has single a different color?

    Regardless of the answer, your toggleMultiColour() method is fine, the change in question needs to occur in your makeBrick() method. In the makeBrick() method, you currently have the color set to the 0 index of your ArrayList. Instead, say: brick.changeColor(colors.get(currentColor));
    Now, after you create each brick, you have to increment currentColor by 1, but you also have to check to see if the new value of currentColor exceeds the bounds of the colors ArrayList. Lastly, you need to remember that you ONLY want to do this if isMultiColor is true.
    I won't give you the code, but I'll help you out with the sudo-code:
    if isMultiColor is true:
         increment currentColor;
         if currentColor is greater than the size of colors:
              set currentColor to 0;

    Tell me if any of that is confusing.
    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/

  4. The Following User Says Thank You to aussiemcgr For This Useful Post:

    samjoyboy (November 16th, 2012)

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

    Default Re: toggle colors from an array when the method is called.

    That was so helpful honestly made a lot of sense however i can only get it to work until it hits the end of the arraylist length this is my code:

        private void drawRow() {
            Integer row1 = 0;
            while (row1 < numRows) {
                if (isMultiColor == true) {
                    for (Integer row = 0; row <= rowLength; row++) {
                        makeBrick();
                        currentColor++;
                        startX = startX + 54;
                        if (currentColor >= colors.size()) {
                            currentColor = 0;
                        }
                    }
                    startY = startY + 16;
                    startX = 10;
                    row1++;
                }
            }
        }

    It just stop after the array has no more colors even though i have placed the if statement to stop this, any ideas??

    thanks again.

    --- Update ---

    done it lol silly mistake -.-

        private void drawRow() {
            Integer row1 = 0;
            while (row1 < numRows) {
                    for (Integer row = 0; row <= rowLength; row++) {
                        makeBrick();
                        if (isMultiColor == true) {
                            currentColor++;
                        }
                        startX = startX + 54;
                        if (currentColor >= colors.size()) {
                            currentColor = 0;
                        }
                    }
                    startY = startY + 16;
                    startX = 10;
                    row1++;
            }
        }

    Thanks so so so so much!!

Similar Threads

  1. Replies: 2
    Last Post: November 11th, 2012, 10:44 PM
  2. Replies: 4
    Last Post: June 15th, 2012, 01:50 PM
  3. Assigning Colors in a 2D array in a random order
    By captain in forum Object Oriented Programming
    Replies: 5
    Last Post: February 21st, 2012, 07:13 AM
  4. How are the colors in Swing's UIManager used?
    By JerryH in forum AWT / Java Swing
    Replies: 0
    Last Post: January 27th, 2011, 10:50 PM
  5. Modify Colors in a Picture
    By theuniverse in forum Java Theory & Questions
    Replies: 0
    Last Post: October 17th, 2009, 04:49 PM