0

Trying to connect circles together in the picture with a line from the center of the circle to the other circle. The line should be shown will the mouse is being dragged.And when I release the mouse than it should not show any line at all. This is the code.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.shape.StrokeType;
import javafx.stage.Stage;

public class Projekt extends Application {

@Override
public void start(Stage primaryStage) {
   Pane pane = new Pane();
   //Inserting Circles
   for(int i=0;i<10;i++)
   {
       for(int j=0;j<10;j++)
       { 
           Circle c = new Circle(i*60+10,j*60+10,10);
           c.setFill(Color.WHITE);
           c.setStroke(Color.BLACK);
           c.setStrokeWidth(2);
           c.setStrokeType(StrokeType.OUTSIDE);
           c.setOnMouseDragged((MouseEvent e)->{
               Line line = new Line(c.getCenterX(),c.getCenterY(),e.getX(),e.getY());


           });
           pane.getChildren().add(c);



       }
   }



    Scene scene = new Scene(pane,600,600);
    primaryStage.setScene(scene);
    primaryStage.setTitle("Hello World!");

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

}
Elis Seiti
  • 15
  • 1
  • 8

1 Answers1

0

There are several ways to do this.

1-st of all you need to add your line to the pane. Because there is line creation in your code, but nothing adds this line to the pane. And you probably don't want to create new line each time, so just create some line (maybe even static one) and change coords with line.setStartX(x); line.setStartY(y); and line.setEndX(e.getX()); line.setEndY(e.getY()); on each mouse press and mouse drag events.

I think you will not be able to do this with just 1 setOnMouseDragged listener, you have to use at least some other events to set your line visible and invisible.

Here is starter code for you to play with:

public class Main extends Application {

private Line line = new Line();

@Override
public void start(Stage primaryStage) {
    Pane pane = new Pane();

    line.setVisible(false);
    pane.getChildren().add(line);

    //Inserting Circles
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
            double x = i * 60 + 10;
            double y = j * 60 + 10;
            Circle c = new Circle(x, y, 10);
            c.setFill(Color.WHITE);
            c.setStroke(Color.BLACK);
            c.setStrokeWidth(2);
            c.setStrokeType(StrokeType.OUTSIDE);

            c.setOnMousePressed((MouseEvent e) -> {
                line.setStartX(x);
                line.setStartY(y);
            });

            c.setOnMouseReleased((MouseEvent e) -> {
                line.setVisible(false);
            });

            c.setOnMouseDragged((MouseEvent e) -> {
                line.setEndX(e.getX());
                line.setEndY(e.getY());
                line.setVisible(true);
            });

            pane.getChildren().add(c);

        }
    }
varren
  • 14,551
  • 2
  • 41
  • 72