Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
287 views
in Technique[技术] by (71.8m points)

How does Java / OS detect click co-ordinates on a viewport?

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).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...