Did you add the return statement at the end of the method?
The {} should be aligned like this. Indented every nesting level
{ { { } } } // end of method
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.
Did you add the return statement at the end of the method?
The {} should be aligned like this. Indented every nesting level
{ { { } } } // end of method
Lurie1 (February 1st, 2012)
ok fixed all those errors yay thanks Norm but now i go on to step 2 of my new flow chart and boom another problem!!..!!.. annoying!
< import java.awt.*; import java.awt.event.*; import java.awt.Robot; import java.awt.AWTException; import java.awt.Rectangle; import java.awt.Color; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class GB{ public boolean checkColor(Color inputColor) throws Exception { Robot robot = new Robot(); Rectangle rectangle = new Rectangle(0, 0, 1365, 770); BufferedImage image = robot.createScreenCapture(rectangle); for(int x = 0; x < rectangle.getWidth(); x++) { for (int y = 0; y < rectangle.getHeight(); y++) { if (image.getRGB(x, y) == inputColor.getRGB()) { return true; } return false; } { while (!checkColor(inputColor)) { inputColor = new Color(196, 195, 181); robot.mouseMove(x, y); robot.mousePress(InputEvent.BUTTON1_MASK); robot.delay(150); robot.mouseRelease(InputEvent.BUTTON1_MASK); } } return false; } return false; } } >
i get 1 error:
GB.java:36: cannot find symbol
symbol : variable y
location class GB
line 36:
robot.mouseMove(x, y);
what i don't get is why it found x but not y. because i know its goina be asked, the symbols x, y i want are the ones that the method came up with when it searched for the pixel. the line for this is:
if (image.getRGB(x, y) == inputColor.getRGB())
The variable y is only known within the for loop where it is defined. It is not known outside of the loop.
Look at the logic of your code. In the for(y) loop, it can sometimes return true or it will return false.
If it enters the loop, it well never exit the loop. It will always do a return.
What are the extra pair of {}s used before the while() statement used for ?
The formatting of the code is much better except for the placement of the return false statements. They should be indented within the {}s that they are in.
ok ill fix that code, so how would i be able to call that variable, for say the mouse move, or assign (x, y) something if i want to check a specific pixel, i would have to declare them as variables like the inputColor right?
Yes you could define the y outside of the for loop.
What value do you want y to have outside of the loop? It would always have the last value it had when it exited the loop.
well i want (x, y) to be the pixel coordinates of the pixel either just found through a call with a new input color or the coordinates being called to find the color.
In your other thread for this problem:
http://www.javaprogrammingforums.com...tone-wall.html
I said:
This code you have posted here has no logic to it. There are method calls and loops here and there but no logical connection between them. You need to stop coding and spend some time designing what the code is supposed to do.you need to work out the logic for all the different cases and program them using if statements etc.
There are many techniques for working out logic. I use flowcharts. Some use pseudo code.
Do you have a technique for working out program logic?
Bottom line, you need to work out the logic before you try to code it.
What you just posted is close to pseudo code, try writing the logic making a list of the steps that need to be decided and the marking the loop back points for re starting the search.
i suppose it should have 0 and so should x, the only thing is when it returns with a x,y and pixel as true or false those x,y values would be the ones used for mouseMove and in the next run through i could use those values again say if it returns false check at same x,y again.
Can you describe in detail what the checkColor method is supposed to do?
What arguments are passed to it,
what processing it does
What results it returns and what values it changes
Then take each of the steps you have listed and put them into the code as comments.
Each section of the code should be describe by an item/step that you have written down before you write the code.
ok will do ill post it when im finished
The idea is to do the design BEFORE you write the code.
ok let me explain it's concept to you:
the program when launched will recognize two colors, it will click on them one after the other to open the game window. when the cage window is open the program will make 4 opening moves, a click drag release, click drag release, click drag release, the fourth move will be depend of if a pixel, is one color or another and the program needs to keep checking the pixel until it becomes the desired color, then it will do the fourth and final opening move. that is all i need it to do.
the program when launched will recognize two colors,
it will click on them one after the other to open the game window.
when the cage window is open the program will make 4 opening moves,
1)a click drag release,
2)click drag release,
3)click drag release,
4)the fourth move will be depend of if a pixel is one color or another
and
the program needs to keep checking the pixel until it becomes the desired color,
then it will do the fourth and final opening move.
Now write detailed specifications for what the method that will check for the desired color: 4)
What are the steps that it needs to do?
Make a list of simple steps or pseudo code that describes how it will work. I don't see any design in what you have coded for that method.
okey dokey one sec
java GB.java
program searches for color (196, 195, 181)
program moves mouse to color
program clicks color
program releases mouse
program searches for color (95, 118, 127)
program moves mouse to color
program clicks color
porgram releases mouse
program searches for a color
if color is not found searches again
when color is found program moves mouse to new color (252, 200, 111)
from color program moves mouse -15 pixels on y axis
program clicks
program drags (right 5 pixels)
program releases mouse button
program moves mouse +50 pixels on the y axis
program clicks program drags (left 5 pixels)
program releases mouse button
program moves mouse +200 pixels on x axis
program clicks
program drags mouse (left 5 pixels)
program finds pixel using current mouse pozition and taking away 395 pixels on the x axis and -60 pixels on the y axis
it gets current pixel color
checks color ever 50 miliseconds
when pixel color = not the same as orriginal pixels color it moves mouse by -15 pixels on the y axis
clicks
drags right by 5 pixels
releases button
is that what you wanted?
Is all that the logic for the one method: checkColor? This method you are trying to code:
public boolean checkColor(Color inputColor) { BufferedImage image = robot.createScreenCapture(rectangle); for(int x = 0; x < rectangle.getWidth(); x++) { for (int y = 0; y < rectangle.getHeight(); y++) { if (image.getRGB(x, y) == inputColor.getRGB()) { return true; } } } return false; { while (!checkColor(newColor)) { new Color = new Color(196, 195, 181); } } }
no you remember my original code? well i had it there so each time i wanted a pixel returned i had a different get pixel method, i was told and agree that that was a bad way to go about it, what i have so far is supposed to be a method i can call on whenever i want a pixel checked or found:
< public boolean checkColor(Color inputColor) throws Exception { Robot robot = new Robot(); Rectangle rectangle = new Rectangle(0, 0, 1365, 770); BufferedImage image = robot.createScreenCapture(rectangle); for(int x = 0; x < rectangle.getWidth(); x++) { for (int y = 0; y < rectangle.getHeight(); y++) { if (image.getRGB(x, y) == inputColor.getRGB()) { return true; } return false; } { while (!checkColor(inputColor)) { inputColor = new Color(196, 195, 181); robot.mouseMove(x, y); robot.mousePress(InputEvent.BUTTON1_MASK); robot.delay(150); robot.mouseRelease(InputEvent.BUTTON1_MASK); } } } >
What is the while (!checkColor(inputColor)) for?
When is it executed?
oh i see a problem that is suppose to call the method to find the pixel indicated 196, 195, 181 and return the x, y values and true or false depending on if it found it or not
How does that code fit into the design for what checkColor is supposed to do?
I don't see any relationship.
ya i was just looking at that and it dose not do anything
Also see post#28
< import java.awt.*; import java.awt.event.*; import java.awt.Robot; import java.awt.AWTException; import java.awt.Rectangle; import java.awt.Color; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class GB{ int x; int y; public boolean checkColor(Color inputColor) throws Exception { Robot robot = new Robot(); Rectangle rectangle = new Rectangle(0, 0, 1365, 770); BufferedImage image = robot.createScreenCapture(rectangle); for(int x = 0; x < rectangle.getWidth(); x++) { for (int y = 0; y < rectangle.getHeight(); y++) { if (image.getRGB(x, y) == inputColor.getRGB()) { return true; } return false; } } return false; } } >
ok wait so how would i change that so it will return with three values, the x cordinent the y cordinent and the pixel RGB value, it wouldn't be Boolean anymore right? It would be an int right?
Last edited by Lurie1; February 2nd, 2012 at 10:00 PM.
Are you changing the design of what the method is supposed to do now?
A method can only return one value. To return more than one value it would have to put all of them into a class as member variables, create an instance of that class with the values and return that instance.
well according to my outline this method needs to be able to return all or one of the following:
x
y
RGB color
now how would i got about putting those three returns in to a class as a member?
< import java.awt.*; import java.awt.event.*; import java.awt.Robot; import java.awt.AWTException; import java.awt.Rectangle; import java.awt.Color; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class GBpixel{ public int x; public int y; public int inputColor; public int checkColor(Color inputColor) throws Exception { Robot robot = new Robot(); Rectangle rectangle = new Rectangle(0, 0, 1365, 770); BufferedImage image = robot.createScreenCapture(rectangle); for(int x = 0; x < rectangle.getWidth(); x++) { for (int y = 0; y < rectangle.getHeight(); y++) { if (image.getRGB(x, y) == inputColor.getRGB()) { } } } return 0; } } >