0

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.

vladys.bo
  • 730
  • 2
  • 11
  • 34
  • A couple of tangental review comments - you shouldn't mix awt/swing and Java code in the way you do with JOptionPane, instead use [Java 8u40](https://jdk8.java.net/download.html) which has JavaFX dialog boxes equivalent to JOptionPane built in. You shouldn't call System.exit to shutdown your application, instead call [Platform.exit()](https://docs.oracle.com/javase/8/javafx/api/javafx/application/Platform.html#exit--). – jewelsea Dec 03 '14 at 21:59
  • You will probably have a better chance of getting quality help if you split your questions up and provide your attempt at each piece separately. For example - How to select a line? How to Drag a Line? How to Delete a Line? etc. – jewelsea Dec 03 '14 at 22:02
  • To understand how to select and drag things around, you could take a look at this [Demonstration of modifying a polygon's shape in JavaFX by allowing the user to drag around control points attached to the corners of the Polygon](https://gist.github.com/jewelsea/5375786). It is not the exact requirements for your exercise, but I am not going to write a specific solution for the exact requirements. – jewelsea Dec 03 '14 at 22:03

0 Answers0