I'm making an basic paint application in Javafx. And I'm wondering if there is some way to increase the draw speed of lines on the canvas.
public class MouseListener implements EventHandler<MouseEvent>{
@Override
public void handle(MouseEvent event) {
if(event.getSource() == canvas){
canvas.getParent().setCursor(Cursor.CROSSHAIR);
}
if(event.getEventType() == MouseEvent.MOUSE_PRESSED){
x1 = event.getX();
y1 = event.getY();
}else if(event.getEventType() == MouseEvent.MOUSE_MOVED){
pGraphics.reset();
x2 = event.getX();
y2 = event.getY();
mouseClick = 0;
pGraphics.drawLine(x1, y1, x2, y2);
}
}
public void drawLine(double x1, double y1, double x2, double y2){
gc.strokeLine(x1, y1, x2, y2);
}
public void reset()
{
gc.clearRect(0,0, gc.getCanvas().getWidth(), gc.getCanvas().getHeight());
}
As you can see the drawing is pretty straight forward using a MouseListener for detecting the (x1, y1) position where the line begins, and the same goes for setting the (x2, y2) postion where the line is supposed to end. As you see I clear the line after drawing, this is my intention since this is the function which are supposed to "show" where the line is supposed to go, e.g as in Microsoft Paint using line function. The issue of all this though, is that the line "laggs" behind the cursor, and quite much to when the cursor is moving at the right speed. So the question is: does this have something to do with my draw function, the canvas drawing function, the refresh rate of the mouseListener or something else entirely, or all together? Would really appreciate some suggestions to this.