Skip to content

Instantly share code, notes, and snippets.

@Da9el00
Created June 18, 2023 08:43
Show Gist options
  • Save Da9el00/da610e1cf780c17132ac394eeee4c727 to your computer and use it in GitHub Desktop.
Save Da9el00/da610e1cf780c17132ac394eeee4c727 to your computer and use it in GitHub Desktop.
JavaFX - Movable Line
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<AnchorPane fx:id="anchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/18.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.movableline.LineController">
<children>
<HBox layoutX="152.0" layoutY="326.0" spacing="200.0">
<children>
<Button mnemonicParsing="false" onAction="#addLine" text="Add" />
<Button mnemonicParsing="false" onAction="#removeLine" text="Remove" />
</children>
</HBox>
</children>
</AnchorPane>
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class LineApplication extends Application {
@Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(LineApplication.class.getResource("line-view.fxml"));
Scene scene = new Scene(fxmlLoader.load());
stage.setTitle("Hello!");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.layout.AnchorPane;
import java.util.ArrayList;
import java.util.List;
public class LineController {
List<MovableLine> lines = new ArrayList<>();
@FXML
private AnchorPane anchorPane;
@FXML
void addLine(ActionEvent event) {
MovableLine newLine = new MovableLine(anchorPane);
lines.add(newLine);
}
@FXML
void removeLine(ActionEvent event) {
MovableLine currentLine = lines.get(0);
lines.remove(0);
currentLine.removeLineFrom(anchorPane);
}
}
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;
public class MovableLine {
Line line;
Circle startPoint, endPoint;
public MovableLine(Pane root) {
createDraggableLine(root);
}
private void createDraggableLine(Pane root){
// Create the line
line = new Line(50, 50, 100, 100);
line.setStroke(Color.BLACK);
line.setStrokeWidth(2);
// Create the start and end points
startPoint = createDraggablePoint(line.getStartX(), line.getStartY());
endPoint = createDraggablePoint(line.getEndX(), line.getEndY());
// Add mouse event handlers for dragging
startPoint.setOnMouseDragged(e -> handlePointMouseDragged(e, line, true));
endPoint.setOnMouseDragged(e -> handlePointMouseDragged(e, line, false));
root.getChildren().addAll(line, startPoint, endPoint);
}
private void handlePointMouseDragged(MouseEvent event, Line line, Boolean startPoint) {
Circle point = (Circle) event.getSource();
double offsetX = event.getX();
double offsetY = event.getY();
point.setCenterX(offsetX);
point.setCenterY(offsetY);
point.setLayoutX(event.getSceneX() - offsetX);
point.setLayoutY(event.getSceneY() - offsetY);
if (startPoint) {
line.setStartX(offsetX);
line.setStartY(offsetY);
} else {
line.setEndX(offsetX);
line.setEndY(offsetY);
}
}
private Circle createDraggablePoint(double x, double y) {
Circle point = new Circle(x, y, 5, Color.RED);
point.setStroke(Color.BLACK);
point.setStrokeWidth(1);
point.setCenterX(x);
point.setCenterY(y);
return point;
}
public void removeLineFrom(Pane root){
root.getChildren().remove(line);
root.getChildren().remove(startPoint);
root.getChildren().remove(endPoint);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment