java用鼠标画连续直线,如何在JavaFX画布上用鼠标绘制连续线?

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.

dQpcb.png

解决方案

This code from here.

import javafx.application.Application;

import javafx.event.EventHandler;

import javafx.scene.Scene;

import javafx.scene.canvas.Canvas;

import javafx.scene.canvas.GraphicsContext;

import javafx.scene.input.MouseEvent;

import javafx.scene.layout.StackPane;

import javafx.scene.paint.Color;

import javafx.stage.Stage;

/**

* @web http://java-buddy.blogspot.com/

*/

public class JavaFX_DrawOnCanvas extends Application {

@Override

public void start(Stage primaryStage) {

Canvas canvas = new Canvas(400, 400);

final GraphicsContext graphicsContext = canvas.getGraphicsContext2D();

initDraw(graphicsContext);

canvas.addEventHandler(MouseEvent.MOUSE_PRESSED,

new EventHandler(){

@Override

public void handle(MouseEvent event) {

graphicsContext.beginPath();

graphicsContext.moveTo(event.getX(), event.getY());

graphicsContext.stroke();

}

});

canvas.addEventHandler(MouseEvent.MOUSE_DRAGGED,

new EventHandler(){

@Override

public void handle(MouseEvent event) {

graphicsContext.lineTo(event.getX(), event.getY());

graphicsContext.stroke();

}

});

canvas.addEventHandler(MouseEvent.MOUSE_RELEASED,

new EventHandler(){

@Override

public void handle(MouseEvent event) {

}

});

StackPane root = new StackPane();

root.getChildren().add(canvas);

Scene scene = new Scene(root, 400, 400);

primaryStage.setTitle("java-buddy.blogspot.com");

primaryStage.setScene(scene);

primaryStage.show();

}

public static void main(String[] args) {

launch(args);

}

private void initDraw(GraphicsContext gc){

double canvasWidth = gc.getCanvas().getWidth();

double canvasHeight = gc.getCanvas().getHeight();

gc.setFill(Color.LIGHTGRAY);

gc.setStroke(Color.BLACK);

gc.setLineWidth(5);

gc.fill();

gc.strokeRect(

0, //x of the upper left corner

0, //y of the upper left corner

canvasWidth, //width of the rectangle

canvasHeight); //height of the rectangle

gc.setFill(Color.RED);

gc.setStroke(Color.BLUE);

gc.setLineWidth(1);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值