I'm creating a mouse tracker for my program by showing the mouse's location using a cross. However, when I moved my mouse very fast to test the integrity of the code's performance, I experienced lagging -- the cross was falling behind the mouse and would not catch up until the mouse stopped moving.
I'm guessing that I need some sort of multithreading solution to solve this issue, but I have no idea how to do it, as I'm fairly new to this concept. Can any javafx experts offer some insights? Much appreciated.
Here is the partial code for the mouse listener:
private void setMouseMoved(String [] time) {
setOnMouseMoved((MouseEvent event) -> {
double x = event.getX();
double y = event.getY();
Platform.runLater(new Runnable() {
@Override
public void run() {
setMoveObjects(time,x,y);
}
});
});
}
// Set the bounds for the mouse trackpad.
private void setMoveObjects(String[] time, double x, double y) {
if (x > xORIGIN && x < xEND && y > yORIGIN && y < yEND) {
setMoveHVlines(x, y);
}
}
// creating the mouse cross;
private void setMoveHVlines(double x, double y) {
HLINE.setStartY(y);
HLINE.setEndY(y);
VLINE.setStartX(x);
VLINE.setEndX(x);
}