How to draw a continuous line with mouse on JavaFX canvas?

前端 未结 2 1867
温柔的废话
温柔的废话 2020-12-10 06:46

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 ga

相关标签:
2条回答
  • 2020-12-10 07:19

    Sorry about previous message, should have asked a question in a new thread, right?

    Here's a solution that works better, not creating the strange lines like in the scribbletest above.

    canvas.addEventHandler(MouseEvent.MOUSE_PRESSED, 
            new EventHandler<MouseEvent>(){
        @Override
        public void handle(MouseEvent event) {
            graphicsContext.beginPath();
            graphicsContext.moveTo(event.getX(), event.getY());
            graphicsContext.stroke();
    
        }
    });
    
    canvas.addEventHandler(MouseEvent.MOUSE_DRAGGED, 
            new EventHandler<MouseEvent>(){
        @Override
        public void handle(MouseEvent event) {
            graphicsContext.lineTo(event.getX(), event.getY());
            graphicsContext.stroke();
            graphicsContext.closePath();
            graphicsContext.beginPath();
            graphicsContext.moveTo(event.getX(), event.getY());
        }
    });
    
    canvas.addEventHandler(MouseEvent.MOUSE_RELEASED, 
            new EventHandler<MouseEvent>(){
        @Override
        public void handle(MouseEvent event) {
            graphicsContext.lineTo(event.getX(), event.getY());
            graphicsContext.stroke();
            graphicsContext.closePath();
        }
    });
    
    0 讨论(0)
  • 2020-12-10 07:29

    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<MouseEvent>(){
    
                @Override
                public void handle(MouseEvent event) {
                    graphicsContext.beginPath();
                    graphicsContext.moveTo(event.getX(), event.getY());
                    graphicsContext.stroke();
                }
            });
    
            canvas.addEventHandler(MouseEvent.MOUSE_DRAGGED, 
                    new EventHandler<MouseEvent>(){
    
                @Override
                public void handle(MouseEvent event) {
                    graphicsContext.lineTo(event.getX(), event.getY());
                    graphicsContext.stroke();
                }
            });
    
            canvas.addEventHandler(MouseEvent.MOUSE_RELEASED, 
                    new EventHandler<MouseEvent>(){
    
                @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)
提交回复
热议问题