The code below results in a JavaFX Canvas that can be drawn on with the mouse pointer but skips some points, i.e., leaves gaps if one tries to draw a continuous line. The gapping intensifies as pointer speed increases.
What causes this behaviour and what can be done to achieve a well-connected line? (N.B., I'm looking for an answer that explicitly toggles each single pixel the pointer passes over to black, not operations such as smoothing or connecting dots, etc.)
public class DrawingSample extends Application {
public void start(Stage stage) {
FlowPane flowPane = new FlowPane();
Canvas canvas = new Canvas(300, 300);
flowPane.getChildren().add(canvas);
GraphicsContext graphicsContext = canvas.getGraphicsContext2D();
graphicsContext.setFill(Color.WHITE);
graphicsContext.fillRect(0, 0, 300, 300);
canvas.setOnMouseDragged((event) -> {
graphicsContext.setFill(Color.BLACK);
graphicsContext.fillRect(event.getX(), event.getY(), 1, 1);
});
stage.setScene(new Scene(flowPane));
stage.show();
}
public static void main(String[] args) {
launch(DrawingSample.class);
}
}
The following figure demonstrates three lines drawn from left to right at increasing speeds as we go down.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…