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.

Re: Get Pixel

Where were you told that? Graphics doesn't have a getRGB() method, so that doesn't make much sense.

You might be looking for the getPixelColor() in the Robot class. The API is your friend.

But I'd be willing to bet there's an easier/better way to do this. For example, why are you testing the color of a pixel? Game logic should not be in your painting code (painting logic is fine). Are you trying to do collision detection?

Re: Get Pixel

Well if you're trying to do collision testing, you almost definitely don't want to be testing for collisions in your paint method.

But if you know where the objects are to be painted, then you should be able to figure out where they are for testing collisions. This isn't exactly trivial, but it's been done countless times before. Google is your friend.

Re: Get Pixel

I was thinking that the performance of fetching data from one pixel and react on the result would be much better than to store an entire area in an array and try to keep track of which pixels are colored that way for example. If thinking of a snake clone and the snake being of length 150 pixels, that would mean at least 150 iterations to just make sure you haven't run into the snake each time you are to update one pixel, as compared to just checking one single pixel to see if it's black or not. And it also gets around the problem of trying to figure out what pixels a circel of size x would color.

But still, I would be happy to hear any good alternative solutions to this problem!

Re: Get Pixel

If what you're testing for collisions is indeed only one pixel, then I suppose that's fine. But for most games, you're going to be checking the collision of quite a few pixels- or of paths, or walls, etc.

Re: Get Pixel

I tried to use the robot class; and it worked to some extent, the problem is that the robotclass only works if it's on top of the screen. I want to read the pixel of the active window. And yes; I can fetch the proper location, but if another window is on top of the app window, the robotclass doesn't do it.

Re: Get Pixel

Originally Posted by Ophe

I tried to use the robot class; and it worked to some extent, the problem is that the robotclass only works if it's on top of the screen. I want to read the pixel of the active window. And yes; I can fetch the proper location, but if another window is on top of the app window, the robotclass doesn't do it.

Yep, that's another reason not to use the pixel value.

You could try painting the game window to an image first, then using the image to get the pixel value, but I would predict that simply doing more standard collision checking would be simpler. Up to you.