0

Note: I'm using TornadoFX and kotlin, but it is based of JavaFX with some kotlin additions, which is why I'm mentioning JavaFX instead, since it seems more related to JavaFX than TornadoFX.

I'm trying to get the color of a specific location on the JavaFX window (the scene). The reason is because for my 2D game, I'm trying to build a map. For example, if I touch black, then stop moving in that direction (so the border). Or if it's red, then lose a life (obstacle). Rather than hardcoding that (I've got no idea how I'd be able to do that, since I don't want the map to just be a square), I'm trying to get the pixels and get the color. Note that since this is part of the hit detection system, it'll be ran 100+ a second, so I'll need a solution that doesn't take too much time.

Also, note that I'm not trying to get a pixel from an image, but the window that the user sees. (Just clarifying so that someone doesn't misunderstand) EDIT: I just realized that I could maybe use the image and get the color from that... although if I zoom in the image to make the map larger.. that part confuses me of how I could do it then.

dkim19375
  • 31
  • 5
  • An alternative approach to collision detection during animation is examined [here](https://stackoverflow.com/q/39185306/230513) – trashgod Jul 03 '21 at 23:19
  • @trashgod The issue with this is that if I have an image drawn for the map, and set the image as the background, I can't detect it using this method. – dkim19375 Jul 03 '21 at 23:26
  • I can see the appeal of using an image to define boundaries, but it has the effect of forcing the model to refer continually to a view element; instead, consider preprocessing the image to build a corresponding model. – trashgod Jul 04 '21 at 12:00
  • @trashgod What do you mean by building a corresponding model? And do you have an article/link to where I can find more information? – dkim19375 Jul 05 '21 at 16:45
  • This [answer](https://stackoverflow.com/a/12115647/230513) outlines the idea; the details would depend on your particular game. – trashgod Jul 05 '21 at 16:57

1 Answers1

1

You can use Robot API to do this, to get the colour of the current mouse position you can do this

int xValue = MouseInfo.getPointerInfo().getLocation().x;
int yValue = MouseInfo.getPointerInfo().getLocation().y;
Robot robot = new Robot();
Color color = robot.getPixelColor(xValue, yValue);

To get the current position when the cursor is moved you need to use the setOnMouseMoved listener

yourViewNode.setOnMouseMoved(event -> {
    int xValue = MouseInfo.getPointerInfo().getLocation().x;
    int yValue = MouseInfo.getPointerInfo().getLocation().y;
    Robot robot = new Robot();
    Color color = robot.getPixelColor(xValue, yValue);
});

Then you can compare the color and check with it, if you want the color when the user clicks only you need to listen for the right or left click then use the same code in the listener to get the color at this moment

I have used this solution in my bot creator project to provide tools that can take action depend on the current position color

AmrDeveloper
  • 3,826
  • 1
  • 21
  • 30
  • Hmm, it seems to work.. until I changed the location of the window. For example, if I hover over a green circle, it shows green, but then if I move the window to my other monitor, it doesn't print out green anymore :/ Do you know why? I tried printing the x and y, and it prints out the location of where the window used to be – dkim19375 Jul 03 '21 at 23:24
  • maybe this could help: https://www.daniweb.com/programming/software-development/threads/358011/get-mouse-position-pixel-coordinates-relative-to-jframe – papalulu Jul 04 '21 at 01:20
  • this code get the color of current position when it run, you can use it inside mouse listener or event loop – AmrDeveloper Jul 04 '21 at 02:11
  • I have updated the answer to show an example about get position with move listener – AmrDeveloper Jul 04 '21 at 13:36
  • It still won't work, it only works on my main monitor, I think it might be because of how Robot works? – dkim19375 Jul 05 '21 at 16:43