Results 1 to 7 of 7
  1. #1
    Kegorian is offline Member
    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0

    Default Better solution for picking a color?

    Hey all! I'm very new to Java, I've been doing it for around 4 days or so now. I've been using the Standford free tutorials and I'm currently doing their 3rd assignment (To create a breakout game). I've currently just finished making my bricks, however I think that it's possible to refine my code and would like it if you could help me out or tell me it's ok as it is.

    The "problem" I have is when creating each brick, when it moves two lines down, the color of the brick changes. Now I'm currently using an if statements, to call a different method depending on the row number.

    What I want to know is there some sort of method or way to get it so I can do:
    Java Code:
    Brick.setFillColor(getRowColor);
    Java Code:
    int rowNumber = 1;
    
    for(int i = NUMBER_OF_ROWS ; i >= 1; i--){
    * int brickNumber = 0;
    * for(int j = 0; j <= BRICKS_IN_ROW; j++) {
    * * if (rowNumber == 1 || rowNumber == 2) {
    * * * createRedBricks(rowNumber, brickNumber);
    * * } else if (rowNumber == 3 || rowNumber == 4) {
    * * *  createOrangeBricks(rowNumber, brickNumber);
    * * } else if (rowNumber == 5 || rowNumber == 6) {
    * * * createYellowBricks(rowNumber, brickNumber);
    * * } else if (rowNumber == 7 || rowNumber == 8) {
    * * * createGreenBricks(rowNumber, brickNumber);
    * * } else {
    * * * createBlueBricks(rowNumber, brickNumber);
    * * }
    * pause(50);
    * brickNumber++;
    * }
    * rowNumber++;
     }
    Above is the code I use to draw the bricks onto the screen and below is an example of a red brick I'm using.

    Java Code:
    private void createRedBricks(int rowNumber, int brickNumber) { 
    * double PosY = calcYPos(rowNumber);
    * double PosX = calcXPos(BRICKS_IN_ROW, brickNumber);
            
    * GRect Brick = new GRect(PosX, PosY, BRICK_WIDTH, BRICK_HEIGHT);
    * Brick.setFilled(true);
    * Brick.setFillColor(Color.red);
    * add(Brick);
    }
    Below is also an example of the program I've created so far:


    p.s: sorry for the long post, but I felt it best to include all information you may need to help me out.

  2. #2
    MR bruto's Avatar
    MR bruto is offline Senior Member
    Join Date
    May 2013
    Location
    The Netherlands
    Posts
    127
    Rep Power
    0

    Default Re: Better solution for picking a color?

    you can make a Color with an RGB code (Red Green Blue).

    look on the internet for RGB codes of colors, or keep changing the numbers to get your desired color.
    never go any higer than 255

    like:
    Java Code:
    Brick.setFillColor(new Color(120, 150, 250));

  3. #3
    Kegorian is offline Member
    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0

    Default Re: Better solution for picking a color?

    I know I can do this and that's not my problem. The color is all set and correct, but I have five different methods one for each colored bricks. However, I want one method for bricks of all different colors, but changing the line:
    Java Code:
    Brick.setFillColor();
    For example, I want a second method that chooses the color for setFillColor(getColor()); for example. Where getColor would be a method that chooses a color then relays it back to setFillColor.

    Sorry I'm not very good at explaining stuff.

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

    Default Re: Better solution for picking a color?

    Java Code:
    // for three rows      
    List<Color> colors = Arrays.<Color>asList(Color.red, Color.blue, Color.green);
    
    Brick.setFillColor(colors.get(rowNo));
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Kegorian is offline Member
    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0

    Default Re: Better solution for picking a color?

    Thanks for the reply Jim. Would I just add the first line, where my variables are, and then the brick.setfill... to where it should be?

    Edit: I also get an error with 'Lists' and 'Arrays.' Is there an import I need to do?
    Last edited by Kegorian; 06-02-2013 at 01:00 AM.

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

    Default Re: Better solution for picking a color?

    I assume this would be shared field so you could just make it static and place it at the top of the class.

    You could also do it with arrays if you are new at this.

    Java Code:
    Color[] colors = {Color.red, Color.green};
    
    Brick.setFillColor(colors[rowNo]);
    But eventually you should get used to using using Lists. They are all in java.util.*.

    Also, this is a fairly ambitious assignment for number 3 and just getting into Java.

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

  7. #7
    Kegorian is offline Member
    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0

    Default Re: Better solution for picking a color?

    Thanks, I just used the array for simplicity for now.

    I know, however it's around the 10th tutorial they've shown. I've tried to learn how to program before and this is the furthest I've got using these tutorials.

Similar Threads

  1. help picking
    By gg4545 in forum New To Java
    Replies: 8
    Last Post: 04-05-2012, 08:57 AM
  2. Picking random int from array
    By AlawiGMX in forum New To Java
    Replies: 5
    Last Post: 12-01-2011, 06:47 PM
  3. Randomly picking variables
    By AlawiGMX in forum New To Java
    Replies: 15
    Last Post: 11-30-2011, 12:36 AM
  4. Picking randomly
    By AbdulAziz Bader in forum New To Java
    Replies: 17
    Last Post: 06-02-2010, 01:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •