Java: getting the mouse's polling rate? - java

I'm writing code which moves the mouse in an applet by sending MouseEvent objects for it to process. In order for my code to move the mouse from one location to another, I need to generate points to fill the path so that my mouse can move through them. However, in order to create the right amount of points (i.e., to mimic movement as if done by physically moving the mouse), I need to determine the physical mouse's polling rate so I know how often it tells my machine about its position.
I looked around for ways to retrieve this value, but the best that I found was the MouseInfo class, but all that it tells me is the number of buttons on the mouse and some information about its pointer - not what I'm looking for. Does anyone know a way (preferably without some sort of external dependency) to read the physical mouse's polling rate?

I'm not sure if there's a solution for this in the API, but I suggest setting up a mouseListener and capture timestamps with System.currentTimeMillis() or System.nanoTime(), then just wave the mouse around for a bit and measure the time between events fireing. While you're moving the mouse, the MouseEvents should fire as fast as the mouse is polled. I think.

Related

How to take two time-based inputs from a user without calling wait() or yield()

I'm currently in the process of making a game in Java. I've recently been working on mouse input, and where I'm stuck has to do with the game recognizing when the mouse has moved. I've obviously gotten it to accept mouse input, but I can't make the in-game player rotate correctly based upon mouse position.
What I can come up with to fix it is to take a measurement of the mouse's x position at one point in time, and then compare it to another measurement one millisecond later to see whether or not the mouse has moved, and if so, to get the player to rotate linearly faster with the distance the mouse has translated.
However, I have no idea how to do that without forcing the entire program to wait or yield (which would be bad).
I've tried writing the position to a file and then re-reading it immediately after, but it tends to read from the file at different rates, causing the rotation to be spastic and uneven. But besides that, I'm open to ideas. I can't really post my code because there are many class files that go into the program, but if necessary, I can post the whole project to github for anyone to look at.
GitHub Repository link: https://github.com/isl1/Duplicity
P.S. If it helps, I'm using LWJGL 3, paulscode, and Slick-Util on Eclipse Mars.

Libgdx - How to store evrey touch point on drag path?

Whenever i try to store points on drag event ,if i drag a little faster it doesn't store each and every point i drag on screen.
What should i do that it stores each and every point on my touch path?
It is not a problem of Java, or LibGDX. There are two possible causes:
Your touchscreen digitizer has low refresh rate so that it just can't
keep up with your finger's speed.
Your main loop has a performance issue and something inside loop
takes such a long time that you can't get enough number of touch
events.
Either way, the result will be the same.

Changing Mouse Sensitivity in Java

How do you get the sensitivity of a mouse, change it, and then apply it to the mouse?
-Progress removed, showed the speed of clicking instead of the speed of moving-
I have researched this "everywhere", but there is nothing on this subject.
First of all I think arg0.getXOnScreen will give You the absolute x coordinate of the mouse, not the old position as You're assuming by defining variable named oldX. getX should give you the position within the panel or (sth like widget i do not know the api you are using). The second thing is... what do You mean 'sensitivity of mouse' Do you want to change global system settings for mouse from java ? I do not think it is even possible. Look here this will require You to add jni lib to project and invoke some native libs, so You make your code platform dependent.
You would probably have to tinker with the Robot class and listnening for mouse events. So you'd have to listen for mouse pressed events and after that use robot to move the mouse 3x more pixels than the mouse is actually moving, then you'd have to do a mouse up event via the Robot class, reposition the mouse to the original position followed by a mouse down event via the robot.
See how this can be problematic? This would be extremely use case driven and not something to do generically. I've been doing Java a long time so I could probably pull it off but it is not something a novice could probably do because other issues would come up that need resolution during the debugging process.
Noticed this thread when dealing with a 3d API that rotates the view WAY TO SLOW.

Differentiate Between Robot mouseclick and human mouse click

I have an applet that I'm running online and I want to make sure people can't use the java Robot class to operate the applet. I know that yahoo does this on several of their game platforms and I was wondering if anyone knew how they accomplished it.
Watch mouse movement, and make sure you're not seeing "jumps" from one place to another, but movement over time instead. Sun/Oracle's J2SE tutorials show how to follow mouse movement events: http://download.oracle.com/javase/tutorial/uiswing/events/mousemotionlistener.html
Keep in mind that this would potentially fail to detect the difference between a robot and a person on something like a touch screen, or tablet input device.
One more thing to watch for is whether the user is clicking the same pixel, or just in the same vicinity. Humans are fairly imprecise, robots generally aren't unless programmed to be.
I would also put in a gesture logger for good measure that compiles this information, and keeps track of the actual movements of your users. If you suspect someone of cheating, you can then look at what their actual mouse movements looked like, and compare that with a known person. That will give you a better idea of what you need to look for than any of us can come up with off the tops of our heads.
keep track of the distribution of mouse positions over time. Humans move the mouse differently than a robot that knows exactly where to position it every single time it is clicked. Of course, a smarter robot can counter this defense.

Drawing/plot is not continuous (mouse events involved)

I'm having issues with a java class project. The first step consist in drawing a pattern, so I thought well, this can't be hard. And it isn't, but there one thing that really bothers me. First, check the screenshot below :
Screenshot
My problem is that this was done without releasing the mouse, so the drawing should be continuous. Instead of this, there's holes in it. I'm thinking this is related to the way mouse events are transmitted, But I have no idea how to tweak this.
The drawing zone is a JPanel. There is a set of Points that is used to paint the container on mouse events. Pressing add the first point, dragging the others, released clear the drawing zone.
Hope I was specific enough. Thanks for your time!
edit : Forgot the code. http://pastebin.com/RyXiGsvm
StanislavL's right that mouseMove/mouseDrag events are not generated for every pixel you move the mouse cursor over. Why don't you want to use lines? If the issue is that the result is ugly and jagged, you might consider using cubic splines instead. GeneralPath.curveTo provides an easy way to do this. If getting the control points right is a pain, you can also use GeneralPath.quadTo; this is a quadratic approximation that won't look quite as good, but you can easily just pass in the last three points from mouseDrags.
I guess you store mouse points obtained in mouseDrag processing. Mouse drag happens from after some time interval so if you move mouse relatively fast you just got points. TO drai it you can use drawLine() passing pairs of point. So you'll have lines.

Categories