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 15 of 15

Thread: Writing a getColor() method

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Writing a getColor() method

    I am having trouble with a simple game. I have a game made with Blocks. The blocks are made as
    Block(double x1Lc, double y1Lc, double w, double h, Color c)

    They are then added to an ArrayList. The 0 block in the ArrayList is the player block. The other blocks are enemies or food. The enemies are blue and the food is red. If the character block touches an enemy then you lose the game. If the character block touches food then the food block is removed from the game.

    With my code now. If you touch any block then you lose the game. What do I need to do to check if the blocks color is blue or red and then respond accordingly.

    Here is my code for the for and if that works now but, ends if you touch any block.

    // player loses if any two Blocks touch:
          for( int k=1; k<list.size(); k++ )
          {// see if Block 0 touches any other Block:
              {// if Block 0 hits Block k, player loses:
              if( list.get(0).hits( list.get(k) ) )
                state = "lose";
              }
          }


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

    Default re: Writing a getColor() method

    Do you have any specific questions?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default re: Writing a getColor() method

    Quote Originally Posted by KristopherP View Post
    What do I need to do....?
    How about:
    Quote Originally Posted by KristopherP View Post
    check if the blocks color is blue or red and then respond accordingly
    Seems (with the limited code provided.....) there should be a getColor and setColor method in your block class.
    Then in your (posted) for loop something like:

    for.........
    if (food) { eat }
    else if (enemy) {die}
    else { touched unknown entity }

  4. #4
    Junior Member
    Join Date
    Feb 2013
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default re: Writing a getColor() method

    My main question is do I need to create a getColor method or can I use the getColor that is already available as part of Color.
    If I can use the getColor already available that is getColor(String nm, int v)
    If I use this would I attach my code as

    if( list.get(0).hits( list.get(k).getColor(red) ) )

    or am I working in the wrong direction?

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default re: Writing a getColor() method

    Quote Originally Posted by KristopherP View Post
    My main question is do I need to create a getColor method
    This seems like a normal way to interact with an object.

    Quote Originally Posted by KristopherP View Post
    can I use the getColor that is already available as part of Color.
    I don't think the getColor method of java.awt.Color will return the color of your block. Suggested reading.


    Quote Originally Posted by KristopherP View Post
    or am I working in the wrong direction?
    What happened when you tried: if( list.get(0).hits( list.get(k).getColor(red) ) ) ?
    Last edited by jps; April 22nd, 2013 at 07:24 PM. Reason: fixed tag

  6. #6
    Junior Member
    Join Date
    Feb 2013
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Writing a getColor() method

    I believe my previous post was in the wrong section. I need a way to check the color of objects in an arrayList. I included my Block class and what I have so far. I am not sure what I need to return or write to check the color of my blocks.

    import java.awt.Color;
     
    public class Block
    {
            //Instance variables
            private double x1, x2, y1, y2;
            private Color color;
     
            //Constuctor to build Block
            public Block(double x1Lc, double y1Lc, double w, double h, Color c)
           {
               x1 = x1Lc;
               y1 = y1Lc; 
               x2 = w;
               y2 = h;
               color = c;
     
            }
            public void  move(double right, double up)
           {
               x1 += right;
               y1 += up;
           }
     
           public void draw(Camera cam)
          {
             cam.setColor(color);
             cam.fillRect(x1, y1, x2, y2);
     
           }
     
           public boolean hits (Block other)
          {
     
                boolean result = ! ( ( x1+x2 < other.x1 || other.x1+ other.x2 < x1 ||
                                         y1+y2 < other.y1 || other.y1 + other.y2 < y1) );
                return result;
           }   
     
           public     getColor()
          {  
          }
     
    } // end of Class
    Last edited by KristopherP; April 22nd, 2013 at 10:13 PM. Reason: readability

  7. #7
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Writing a getColor() method

    I need a way to check the color of objects in an arrayList.
    Provide your Block class with a getColor() method to return the lock's colour. Then inside a for loop call this method for each block in the list.

  8. #8
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Writing a getColor() method

    Quote Originally Posted by KristopherP View Post
    I believe my previous post was in the wrong section.
    We can move threads if they need moved. I merged your threads for the same question.

  9. #9
    Junior Member
    Join Date
    Feb 2013
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default re: Writing a getColor() method

    Thanks but, the problem is I do not know how to do a getColor() method. I do not know what the return type should be or what code I would use to get the color from one of my objects

  10. #10
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default re: Writing a getColor() method

    Quote Originally Posted by KristopherP View Post
    I do not know what the return type should be...
    What type do you need to be able to compare to a color?

    Quote Originally Posted by KristopherP View Post
    ...what code I would use to get the color from one of my objects
    Is the color stored in a variable in your block object? Write a method that returns that value. You can search for samples with keywords "getters and setters".

  11. #11
    Junior Member
    Join Date
    Feb 2013
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Writing a getColor() method

    Quote Originally Posted by jps View Post
    What type do you need to be able to compare to a color?
    I am not sure I do not know if it is an object or string

    Quote Originally Posted by jps View Post
    Is the color stored in a variable in your block object? Write a method that returns that value. You can search for samples with keywords "getters and setters".
    It is , I need to return the variable from Color c Im just not sure how since Im confused on the method header and code. The text book has simple sample based examples for adding things or printing something to the screen.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Writing a getColor() method

    Quote Originally Posted by KristopherP View Post
    I am not sure I do not know if it is an object or string
    In Java, a String is an object...
    You ask the block to give it's color so you can determine if it is food or enemy. You are not sure what type to return from the method. You need to think about what you will compare the block's color to. If you will compare to a String, then your getColor method needs to return a String. If you will compare to a color object then your method should return a color object.

    The thing is, this is not a right or wrong answer. This is a how you choose to do it... Some ways may be more beneficial in one program than a different program. It is up to you to determine what suits your needs.

  14. #14
    Junior Member
    Join Date
    Feb 2013
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Writing a getColor() method

    Thanks for the input and links but, I am not getting it. I got my program to perform by using the locations in the arrayList since I know where each object is. I would still like to create a method to check the color though.

    // player loses if any two Blocks touch:
          for( int k=1; k<list.size(); k++ )
          {// see if Block 0 touches any other Block:
              {// if Block 0 hits Block k, player loses:
              if( list.get(0).hits( list.get(k) ) && k <=10)  //losing objects are 1-10 so <=10
     
                 state = "lose";
              }
                if( list.get(0).hits( list.get(k) ) && k <= list.size()) // food 11-15 so <= list
                  list.remove(k);

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Writing a getColor() method

    a method to check the color
    How is the color defined?
    As a String: "RED"
    or as a Color object: Color.RED

    Decide which way that a color is defined, then you'll be able to write code to check the color.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Java static method writing to a file
    By maple1100 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 4th, 2013, 05:12 PM
  2. Having trouble writing insert method for Ordered LinkList.
    By orbin in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 28th, 2012, 05:25 PM
  3. Writing output from a void method to a file
    By TheWhopper858 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: December 9th, 2011, 05:21 AM
  4. graphics fillRect and getColor
    By prashantgolu in forum AWT / Java Swing
    Replies: 2
    Last Post: July 25th, 2011, 07:30 AM
  5. Writing clone() method for LinkedList
    By vluong in forum Collections and Generics
    Replies: 6
    Last Post: October 27th, 2009, 08:41 AM