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: How to get getPixelColor to Loop

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    13
    My Mood
    Relaxed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default How to get getPixelColor to Loop

    public static void main(String[] args) throws InterruptedException {
     
            try {
     
                   Robot robot = new Robot();
                   Color End = robot.getPixelColor(1147, 706);
                   Color L = new Color(53,51,41);
     
                   while(End.getRGB() != L.getRGB()){
                   //thing here will be repeating until on End pixel will appear L color
                 }} catch (AWTException e) {
    }

    Problem is that robot will get color of pixel just once and if pixel later change, robot wont get its color. Therefore while loop wont be activated. I know that the answer will be trivial, but I am just noob
    Last edited by Vergil333@gmail.com; January 20th, 2012 at 03:36 AM.


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    660
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: How to get getPixelColor to Loop

    Forgive me if I'm being unreasonable, but could you possibly rephrase the question? Having a hard time understanding the exact issue you're having.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,664
    Thanks
    45
    Thanked 1,960 Times in 1,934 Posts

    Default Re: How to get getPixelColor to Loop

    Where in the loop do you change either of the values being tested in the while condition?

  4. #4
    Junior Member
    Join Date
    Jan 2012
    Posts
    13
    My Mood
    Relaxed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How to get getPixelColor to Loop

    Quote Originally Posted by newbie View Post
    Forgive me if I'm being unreasonable, but could you possibly rephrase the question? Having a hard time understanding the exact issue you're having.
    Sorry, my English is bad. I will try to explain myself.

    robot.getPixelColor takes colour of pixel on coordinates x=1147 and y=706 and write it to variable End.
    If colour is not the same as in variable L, then the loop will starts.
    When the pixel on coordinates x and y change its colour, value of variable End will be still the same and 'while loop' wont stops.
    I need to make variables L and End have the same values.

    Norm gave me a good question. I forgot on that.
    But still I don't know how to implement it into the while condition.

    public static void main(String[] args) throws InterruptedException {
     
            try {
     
                   Robot robot = new Robot();
                   Color End = robot.getPixelColor(1147, 706);
                   Color L = new Color(53,51,41);
     
                   while(End.getRGB() != L.getRGB()){
                   //some things
                   Color End = robot.getPixelColor(1147, 706); // <<---------- it can't be written like this, right?
                 }} catch (AWTException e) {
    }

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,664
    Thanks
    45
    Thanked 1,960 Times in 1,934 Posts

    Default Re: How to get getPixelColor to Loop

    Your code is defining two different variables with the name end (note variable names should start with lowercase letter). Use the existing variable: end inside of the loop. Do not define a new one:
      end = robot.getPixelColor(1147, 706);

  6. #6
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: How to get getPixelColor to Loop

    while(End.getRGB() != L.getRGB()){
                   //some things
                   Color End = robot.getPixelColor(1147, 706); // <<---------- it can't be written like this, right?
                 }} catch (AWTException e) {
    }
    What's actually happening here is, the End variable inside the loop has scope only inside the loop and it's not actually changing the above variable defined as End. And End and L are not changing at all. Just don't create a new variable but use the above variable to get the pixel color and to compare. (Also, read the Sun's naming conventions)

  7. The Following User Says Thank You to Mr.777 For This Useful Post:

    Vergil333@gmail.com (January 22nd, 2012)

  8. #7
    Junior Member
    Join Date
    Jan 2012
    Posts
    13
    My Mood
    Relaxed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How to get getPixelColor to Loop

    Thank you, that should resolve my problem. Really thanks

  9. #8
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: How to get getPixelColor to Loop

    Quote Originally Posted by Vergil333@gmail.com View Post
    Thank you, that should resolve my problem. Really thanks
    Yeah it should. But the question is, did it?

  10. #9
    Junior Member
    Join Date
    Jan 2012
    Posts
    13
    My Mood
    Relaxed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How to get getPixelColor to Loop

    Quote Originally Posted by Mr.777 View Post
    Yeah it should. But the question is, did it?
    It didn't But I think problem is in statements inside the While.

    Here is something from the code:
    public static void main(String[] args) throws InterruptedException {
        int l=0;
            try {
     
                   Robot robot = new Robot();
                   Robot r = new Robot();
                   Color end = robot.getPixelColor(1147, 706);
                   Color k = new Color(53,51,41);
     
                   while(end.getRGB() != k.getRGB()){
     
                             Color spd = robot.getPixelColor(72, 572);
                             Color mis = new Color(55,53,31);
                             Color mas = new Color(82,79,57);
     
                             if(spd.getRGB() >= mis.getRGB() && spd.getRGB() <= mas.getRGB()){
                             r.delay(50);
                             r.keyPress(KeyEvent.VK_ENTER);
                             r.delay(50);
                             r.keyRelease(KeyEvent.VK_ENTER);
                             r.delay(1000);}
     
                             Color btb = robot.getPixelColor(531, 148);
                             Color alert = new Color(174,42,37);
     
                             while(l<0){
                                  if(btb.getRed() > alert.getRed() && btb.getGreen() < alert.getGreen() && btb.getBlue() < alert.getBlue()){
                                  r.delay(50);
                                  r.keyPress(KeyEvent.VK_F3);
                                  r.delay(50);
                                  r.keyRelease(KeyEvent.VK_F3);
                                  r.delay(50);
                                  l++;}
                            btb = robot.getPixelColor(531, 148);
                            }
     
                   end = robot.getPixelColor(1147, 706);
                 }} catch (AWTException e) {
    }}}

    It's just part of the code. I have there 3 Whiles and 1 If in the "main while".

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,664
    Thanks
    45
    Thanked 1,960 Times in 1,934 Posts

    Default Re: How to get getPixelColor to Loop

    Can you explain your problem?
    What is the code you posted supposed to do? What does it do?
    Have you tried debugging the code by adding printlns to show the values of variables and the execution flow as the code is executed?

  12. The Following User Says Thank You to Norm For This Useful Post:

    Vergil333@gmail.com (January 22nd, 2012)

  13. #11
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: How to get getPixelColor to Loop

    .......................... Sorry just seen l

  14. #12
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: How to get getPixelColor to Loop

    end = robot.getPixelColor(1147, 706);
    Each time you are getting color of same pixels.

  15. #13
    Junior Member
    Join Date
    Jan 2012
    Posts
    13
    My Mood
    Relaxed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How to get getPixelColor to Loop

    Hallo again. It is working now good In my code I have a looot of loops in other loops and them in other loops Adding println really helped me to find and fix issues that I had. There is still something not working properly, but I think I can deal with it now
    Thanks for helping me

  16. #14
    Member DusteroftheCentury's Avatar
    Join Date
    Jan 2012
    Location
    Northern California
    Posts
    42
    My Mood
    Fine
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Default Re: How to get getPixelColor to Loop

    Wow what is a robot in Java?

  17. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,664
    Thanks
    45
    Thanked 1,960 Times in 1,934 Posts

    Default Re: How to get getPixelColor to Loop

    There is a class named Robot in Java SE. Read the API doc for more info.
    Java Platform SE 6

Similar Threads

  1. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 07:57 PM
  2. For loop; Problems with my for loop
    By mingleth in forum Loops & Control Statements
    Replies: 5
    Last Post: November 16th, 2011, 06:24 PM
  3. [SOLVED] My while loop has run into an infinite loop...?
    By kari4848 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 1st, 2011, 11:05 AM
  4. for loop and while loop problems
    By Pulse_Irl in forum Loops & Control Statements
    Replies: 4
    Last Post: May 3rd, 2010, 02:09 AM
  5. hi. i want to rewrite this do loop into a while loop.
    By etidd in forum Loops & Control Statements
    Replies: 3
    Last Post: January 26th, 2010, 04:27 PM