Java FX dragging and deleting lines

1.6k views Asked by At

Today I need your help with JavaFX 2. I have an exercise to draw lines using mouse, then make them draggable with pressed CTRL and leftMouse press and drag, and delete them using ctrl+clicked rightMouse. I understand an algorithm, but don't know how to release it on JAVA FX. I wrote code for drawing, but another functions I don't know how. I understand that for DELETING I need to know index of line I want to delete for this I'm taking coordinates of line which is near my pointer and then delete this line. To drag, I need to know selectedLine(like in deleting) but during dragging change start and end coords of line. And of course, to know that i selected line which I want, I should change color of selected line.
My code:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.stage.Stage;

import javax.swing.JOptionPane;

import javafx.event.EventHandler;


public class Main extends Application {

    Path path;

    public static boolean ctrl = false;

    @Override
    public void start(Stage primaryStage) throws Exception{

        BorderPane root = new BorderPane();

        primaryStage.setTitle("Paint");
        Scene scene = new Scene(root, 500 , 500);


        path = new Path();
        path.setStroke(Color.BLACK);

        primaryStage.setScene(scene);
        primaryStage.show();

        Pane pane = new Pane();
        root.setCenter(pane);

        scene.setOnMousePressed(mh);
        scene.setOnMouseDragged(mh);
        scene.setOnMouseReleased(mh);
        scene.setOnMouseMoved(mh);
        scene.setOnKeyPressed(eh);
        scene.setOnKeyReleased(eh);


        root.getChildren().add(path);
    }

    EventHandler<MouseEvent> mh = new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent e) {
            if (e.getEventType() == MouseEvent.MOUSE_PRESSED) {
                    path.getElements().add(
                            new MoveTo(e.getX(), e.getY()));
            } else if (e.getEventType() == MouseEvent.MOUSE_DRAGGED) {

                if (e.getButton() == MouseButton.PRIMARY) {
                    path.getElements().add(
                            new LineTo(e.getX(), e.getY()));
                } else if(ctrl && e.getButton() == MouseButton.PRIMARY){

                }

            } else if (e.getEventType() == MouseEvent.MOUSE_CLICKED) {
                path.getElements().add(
                        new LineTo(e.getX(), e.getY()));


            } else if(e.getEventType() == MouseEvent.MOUSE_RELEASED){
                if (ctrl && e.getButton() == MouseButton.SECONDARY) {
//                    System.out.println("here");
//                    System.out.println(path.getElements());
                }


            } else if(e.getEventType() == MouseEvent.MOUSE_MOVED){
            }
        }
    };

    EventHandler<KeyEvent> eh = new EventHandler<KeyEvent>(){

        @Override
        public void handle(KeyEvent t) {
            if (t.getEventType() == KeyEvent.KEY_PRESSED){
                if (t.getCode() == KeyCode.ESCAPE) {
                    int result = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit?",
                            "confirmation", JOptionPane.YES_NO_OPTION);

                    if (result == JOptionPane.YES_OPTION) {
                        System.exit(0);
                    }
                }

                if (t.getCode() == KeyCode.CONTROL){
                    ctrl = true;
//                    System.out.println("CTRL works");
                }
            }

            if (t.getEventType() == KeyEvent.KEY_RELEASED){
                if (t.getCode() == KeyCode.CONTROL) {
                    ctrl = false;
                }
            }

        }
    };


    public static void main(String[] args) {
        launch(args);
    }
}

Please, help me make it with javafx, I did it using awt/swing, but now I can't use it.

0

There are 0 answers