Function in java does not finish
Greetings,
this is my very first question and I just do programming as a hobby so keep that in mind when trying to help me, please.
I have a function that 开发者_JS百科tries to read the centre point of a crosshair. Something like a position finder in a game. The code looks like this:
public static Point location(BufferedImage image) {
int pixelLeft;
int pixelTop;
Point point = new Point();
for (int i = Main.RADAR_TOP+1; i < Main.RADAR_BOTTOM; i++) {
pixelLeft = image.getRGB(Main.RADAR_LEFT+7, i);
if ((((pixelLeft>>16) & 0xff) > 35 &&
((pixelLeft>>8) & 0xff) > 35 &&
((pixelLeft) & 0xff) > 35)){
point.y = i;
break;
}
}
for (int j = Main.RADAR_LEFT+1; j < Main.RADAR_RIGHT; j++) {
pixelTop = image.getRGB(j, Main.RADAR_TOP+5);
if ((((pixelTop>>16) & 0xff) > 35 &&
((pixelTop>>8) & 0xff) > 35 &&
((pixelTop) & 0xff) > 35)){
point.x = j;
break;
}
}
System.out.println("location: "+point.x+","+point.y);
/** lastX = point.x;
lastY = point.y;*/
return point;
}
What happens is that at times, this function simply does not return control to the main application. Basically it gets stuck running. Therefore the last comment in the function to do a check. I first thought that maybe it had something to do with reading static members of the class where the function resides so I commented those out as you can see at the end. The problem still occurs though.
I can't really see or think of any reason for this behaviour. I'll appreciate very much any ideas or comments that could point me to a mistake or something.
The problem was that the location function was called by a second function and this second function was inside a case-statement where I missed the break. That's why the application "got stuck".
Are you sure that Main.RADAR_TOP is less then Main.RADAR_BOTTOM ? Else the forloop should spin for a while (a long while..)
精彩评论