-2

Is there anyone that can explain me how does Java (or the JVM I don't know) know where I click?

I mean, I already know how to use JButton, mouseClicked Events etc. I was just wondering what happens between a click on a JButton with my mouse, and my JButton being triggered by Java to fire my Event. How does Java know I clicked on this particular button and not another or not somewhere else?

I tried to search on internet but didn't find anything about that.

EDIT : I'm wondering what is the flow of events between me clicking on my mouse on any component of my Java program, and my program reacting to this click by triggering an event like mouseClicked or actionListener (if implemented). Which part is handled by the OS and which part by Java or the JVM and finally which part by my program (if any).

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • 2
    Possible duplicate of [How to get location of a mouse click relative to a swing window](https://stackoverflow.com/questions/12396066/how-to-get-location-of-a-mouse-click-relative-to-a-swing-window) – Jaroslaw Pawlak Jun 28 '18 at 14:28
  • Non exhaustive : Mouse send an electric signal -> driver convert it to a click event and send it to the OS -> OS receive this click event -> OS get the current position of the sursor -> OS check the active windows at this position -> OS send a click event at this active windows with the position of the cursor -> program running this window receive this click event -> program trigger listener to click event with the cursor position – vincrichaud Jun 28 '18 at 14:40
  • 1
    I think what you really want to know is _how can I efficiently detect a collision between a point and one of several areas in 2d space_ - so that's what you should ask about. How Java does it is somewhat irrelevant. – davmac Jun 28 '18 at 14:45
  • This very much feels like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem/66378#66378). If you want to know where a click is occurring, you should ask how to know where a click is occurring, rather than asking how the mouse pointer’s location is tracked at a system level. – VGR Jun 28 '18 at 15:00
  • @vincrichaud Thank you ! So I run my program on my JVM, and there is something like a "general event listener" that implicitly detects on wich component of my active window I clicked, and if this component can handle a mouse event like mouseClicked or actionListener, this event is triggered ? Do you know how all of that is handled ? –  Jun 28 '18 at 16:09
  • @VGR Yes, reading my question again I see your point. It is confusing because I talk about my area problem, it is maybe irrelevant but I actually wonder how the mouse pointer's location is tracked, at least between the OS and my mouseClicked event for example. Sorry, I'm new on SE sites, I'll do better next time ! –  Jun 28 '18 at 16:15

1 Answers1

2

I can't answer how exactly Java detects click, but your Questions sounds like you want a nice way of handling clicks in a custom painted JPanel.

I have the following suggestion:

Whenever you draw clickable objects, you draw the same shape in a specific color in a "feedback buffer" texture. The color represents the button/clickable id. This is used in 3D applications as well.

The drawing order then equals the z-order of your UI elements.

After drawing, you may click the JPanel, and use the feedback buffer to evaluate the clicked element:

Get pixel color at clicked position, look into your color-id array, and tell the element with the id that a click occurred.

This is pseudo example image of a button layout and a feedback buffer: enter image description here

Note how even stacked buttons and windows overwrite previously drawn pixels. Using this technique ensures your click-evaluation respects the button-z order and even window frames.

Also note that the performance while evaluating a click will be independent from the amount of overlapping buttons.

KYL3R
  • 3,877
  • 1
  • 12
  • 26
  • 1
    That's really smart ! It is not what I was asking about, but anyway this is better than what I've done on my program, I'll try it out ! –  Jun 28 '18 at 16:19