Annux3
Annux3

Reputation: 1

Javafx drag and drop shapes on canvas

I am quite beginner in Java and need some help with my application. I am would like to use drag and drop on custom made shapes with javafx canvas i.e multiple polygons that make up a bow tie.

I have created a method that draws a bow tie that looks like this:

public void joonistaBowTie(GraphicsContext gc) {
// Bowtie left side
gc.setFill(Color.RED);
double xpoints[] = { 242, 242, 200 };
double ypoints[] = { 245, 290, 270 };
int num = 3;
gc.fillPolygon(xpoints, ypoints, num);
// Bowtie right side
gc.setFill(Color.RED);
double xpoints1[] = { 160, 160, 200 };
double ypoints1[] = { 245, 290, 270 };
int num1 = 3;
gc.fillPolygon(xpoints1, ypoints1, num1);
// Bowtie middle part
gc.setFill(Color.RED);
gc.fillOval(190, 255, 20, 30);
}

I have moved that method into a separate class called BowTie. I also have a main class that looks like this:

public class GraafikaNaide extends Application {
Bowtie bowtie;

@Override
public void start(Stage primaryStage) {
    primaryStage.setTitle("JavaFX-iga joonistatud kloun");
    Group root = new Group();
    Canvas canvas = new Canvas(1000, 1000);
    GraphicsContext gc = canvas.getGraphicsContext2D();
    joonista(gc);
    root.getChildren().add(canvas);
    primaryStage.setScene(new Scene(root));
    primaryStage.show();
}

private void joonista(GraphicsContext gc) {
    Bowtie bowtie = new Bowtie();
    bowtie.joonistaBowTie(gc);

}

I also found somewhat example on how to do drag and drop, but i just lack knowledge on how to implement this code to mine.

Could someone please help me with this?

Thanks

Upvotes: 0

Views: 5516

Answers (1)

Jay Lin
Jay Lin

Reputation: 902

Using the link you provided, here is how you would incorporate it with your work:

public class GraafikaNaide extends Application
{
    joonistaBowTie bowtie;
    double orgSceneX, orgSceneY;
    double orgTranslateX, orgTranslateY;

    @Override
    public void start(Stage primaryStage)
    {
        Canvas canvas = new Canvas(1000, 1000);
        GraphicsContext gc = canvas.getGraphicsContext2D();
        joonista(gc);
        canvas.setOnMousePressed(canvasOnMousePressedEventHandler);
        canvas.setOnMouseDragged(canvasOnMouseDraggedEventHandler);

        Group root = new Group();
        root.getChildren().add(canvas);

        primaryStage.setTitle("JavaFX-iga joonistatud kloun");
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }

    private void joonista(GraphicsContext gc)
    {
        joonistaBowTie bowtie = new joonistaBowTie();
        bowtie.joinBowTie(gc);
    }

    EventHandler<MouseEvent> canvasOnMousePressedEventHandler = new EventHandler<MouseEvent>()
    {
        @Override
        public void handle(MouseEvent mouseEvent)
        {
            orgSceneX = mouseEvent.getSceneX();
            orgSceneY = mouseEvent.getSceneY();
            orgTranslateX = ((Canvas)(mouseEvent.getSource())).getTranslateX();
            orgTranslateY = ((Canvas) (mouseEvent.getSource())).getTranslateY();
        }
    };

    EventHandler<MouseEvent> canvasOnMouseDraggedEventHandler = new EventHandler<MouseEvent>()
    {
        @Override
        public void handle(MouseEvent mouseEvent)
        {
            double offsetX = mouseEvent.getSceneX() - orgSceneX;
            double offsetY = mouseEvent.getSceneY() - orgSceneY;
            double newTranslateX = orgTranslateX + offsetX;
            double newTranslateY = orgTranslateY + offsetY;

            ((Canvas) (mouseEvent.getSource())).setTranslateX(newTranslateX);  //transform the object
            ((Canvas) (mouseEvent.getSource())).setTranslateY(newTranslateY);
        }
    };
}

Hope this helps.

Upvotes: 4

Related Questions