Below example demonstrates "gaps" in drawing due to mouse events arriving too slowly (I presume). I'd like to capture all "mouseovered" pixels, without drawing lines from the last pixel written to the current one. Doing this assumes the mouse moved in a straight line, and that may not be the case for very fast movements.
Do we have any workarounds to get the missing events?
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import java.awt.*;
public class DrawTest extends Application {
private static Point point;
@Override
public void start(Stage stage) {
Canvas canvas = new Canvas(800, 600);
AnchorPane anchorPane = new AnchorPane(canvas);
GraphicsContext gc = canvas.getGraphicsContext2D();
point = MouseInfo.getPointerInfo().getLocation();
gc.setFill(Color.WHITE);
gc.fillRect(0, 0, 800, 600);
canvas.setOnMouseDragged((event) -> {
gc.getPixelWriter().setColor((int) event.getX(), (int) event.getY(), Color.BLACK);
System.out.println("X " + event.getX() + " Y " + event.getY());
});
stage.setScene(new Scene(anchorPane));
stage.show();
}
public static void main(String[] args) {
Application.launch(DrawTest.class, args);
}
}
This is an example of some of the event coordinates that get rendered with large gaps:
X 189.0 Y 248.0
X 193.0 Y 248.0
X 199.0 Y 248.0
X 204.0 Y 247.0
X 211.0 Y 244.0
X 225.0 Y 240.0
I've tried using this flag but it didn't help (on Linux): -Djavafx.animation.fullspeed=true
I'd be willing to try limited Swing options or maybe even more "native" workarounds, but obviously would prefer a JFX resolution or at least an explanation why JFX doesn't do this. Would need a Linux compatible solution, Windows too I guess. :P
This question is very similar, but the solutions proposed are still of a "connect the dots" nature and not exactly suitable if you need to accurately record or react to the pixel-by-pixel mouse path, so please don't close this as a duplicate - it's not.
How to draw a continuous line with mouse on JavaFX canvas?
Edit:
Well... it turns out I guess JavaFX is vindicated after all. After adding the following bit in the middle of the example above, if you'll forgive the expected thread racing, it does look to me like JFX is matching AWT event for event with this crude polling method. Probably the best that can be done is to fill in the gaps somehow.
Thread t = new Thread(() -> {
while (true) {
Point p = MouseInfo.getPointerInfo().getLocation();
if (point.x != p.x || point.y != p.y) {
point = p;
System.out.println(point);
}
try {
Thread.sleep(1);
} catch (InterruptedException e) {
}
}
});
t.setDaemon(true);
t.start();
Output:
java.awt.Point[x=2890,y=301]
X 2890.0 Y 301.0
java.awt.Point[x=2884,y=303]
X 2884.0 Y 303.0
java.awt.Point[x=2870,y=308]
X 2870.0 Y 308.0
X 2848.0 Y 314.0
java.awt.Point[x=2848,y=314]
java.awt.Point[x=2822,y=322]
X 2822.0 Y 322.0
X 2790.0 Y 330.0
java.awt.Point[x=2790,y=330]
java.awt.Point[x=2760,y=338]
X 2760.0 Y 338.0
X 2726.0 Y 344.0
java.awt.Point[x=2726,y=344]
X 2694.0 Y 350.0
java.awt.Point[x=2694,y=350]
X 2668.0 Y 356.0
java.awt.Point[x=2668,y=356]
java.awt.Point[x=2646,y=360]
X 2646.0 Y 360.0
java.awt.Point[x=2631,y=366]
X 2631.0 Y 366.0
X 2623.0 Y 367.0
java.awt.Point[x=2623,y=367]
X 2620.0 Y 369.0
java.awt.Point[x=2620,y=369]
java.awt.Point[x=2621,y=369]
X 2621.0 Y 369.0
X 2622.0 Y 368.0
java.awt.Point[x=2622,y=368]