Skip to content

Instantly share code, notes, and snippets.

@Da9el00
Last active May 29, 2022 17:44
Show Gist options
  • Save Da9el00/f88b34880bf181d57c18421e19c0ee2d to your computer and use it in GitHub Desktop.
Save Da9el00/f88b34880bf181d57c18421e19c0ee2d to your computer and use it in GitHub Desktop.
JavaFX Grid-based draggable nodes
package com.example.gridbaseddraggable;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class Application extends javafx.application.Application {
@Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(Application.class.getResource("hello-view.fxml"));
Scene scene = new Scene(fxmlLoader.load());
stage.setTitle("Hello!");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
package com.example.gridbaseddraggable;
import javafx.scene.shape.Rectangle;
public class Component {
private Rectangle rectangle;
private int startPositionX;
private int startPositionY;
public Component(int size, int startPositionX, int startPositionY) {
this.startPositionX = startPositionX;
this.startPositionY = startPositionY;
rectangle = new Rectangle(startPositionX, startPositionY, size, size);
}
public Rectangle getRectangle() {
return rectangle;
}
public int getStartPositionX() {
return startPositionX;
}
public int getStartPositionY() {
return startPositionY;
}
}
package com.example.gridbaseddraggable;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.layout.AnchorPane;
import javafx.scene.shape.Rectangle;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable {
private int gridSize = 50;
@FXML
private AnchorPane pane;
private GridHandler gridHandler;
private DraggableMakerGrid draggableMakerGrid;
private DraggableMaker draggableMaker = new DraggableMaker();
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
draggableMakerGrid = new DraggableMakerGrid(pane.getPrefWidth(), pane.getPrefHeight(), gridSize, pane);
gridHandler = new GridHandler(pane.getPrefWidth(), pane.getPrefHeight(), gridSize, pane);
gridHandler.updateGrid();
Component component = new Component(gridSize, 100, 100);
pane.getChildren().add(component.getRectangle());
draggableMakerGrid.makeDraggable(component);
//draggableMaker.makeDraggable(component.getRectangle());
}
}
package com.example.gridbaseddraggable;
import javafx.scene.Node;
public class DraggableMaker {
private double mouseAnchorX;
private double mouseAnchorY;
public void makeDraggable(Node node){
node.setOnMousePressed(mouseEvent -> {
mouseAnchorX = mouseEvent.getX();
mouseAnchorY = mouseEvent.getY();
});
node.setOnMouseDragged(mouseEvent -> {
node.setLayoutX(mouseEvent.getSceneX() - mouseAnchorX);
node.setLayoutY(mouseEvent.getSceneY() - mouseAnchorY);
});
}
}
package com.example.gridbaseddraggable;
import javafx.scene.Node;
import javafx.scene.layout.AnchorPane;
public class DraggableMakerGrid extends GridBase{
private double mouseAnchorX;
private double mouseAnchorY;
public DraggableMakerGrid(double planeWidth, double planeHeight, int gridSize, AnchorPane anchorPane) {
super(planeWidth, planeHeight, gridSize, anchorPane);
}
public void makeDraggable(Node node){
node.setOnMouseDragged(mouseEvent -> {
mouseAnchorX = mouseEvent.getSceneX();
mouseAnchorY = mouseEvent.getSceneY();
int x = (int) ((mouseAnchorX/getGridSize()) % getTilesAcross()) * getGridSize();
int y = (int) ((mouseAnchorY/getGridSize()) % getTilesDown()) * getGridSize();
node.setLayoutX(x);
node.setLayoutY(y);
});
}
public void makeDraggable(Component component){
Node node = component.getRectangle();
node.setOnMouseDragged(mouseEvent -> {
mouseAnchorX = mouseEvent.getSceneX();
mouseAnchorY = mouseEvent.getSceneY();
int x = (int) ((mouseAnchorX/getGridSize()) % getTilesAcross()) * getGridSize();
int y = (int) ((mouseAnchorY/getGridSize()) % getTilesDown()) * getGridSize();
node.setLayoutX(x - component.getStartPositionX());
node.setLayoutY(y - component.getStartPositionY());
});
}
}
package com.example.gridbaseddraggable;
import javafx.scene.layout.AnchorPane;
public abstract class GridBase {
private double planeWidth;
private double planeHeight;
private int tilesAcross;
private int tileAmount;
private int gridSize;
private int tilesDown;
private AnchorPane anchorPane;
public GridBase(double planeWidth, double planeHeight, int gridSize, AnchorPane anchorPane) {
this.planeWidth = planeWidth;
this.planeHeight = planeHeight;
this.gridSize = gridSize;
this.anchorPane = anchorPane;
tilesAcross = (int) (planeWidth / gridSize);
tileAmount = (int) ((planeWidth /gridSize) * (planeHeight /gridSize));
tilesDown = tileAmount/tilesAcross;
}
public double getPlaneWidth() {
return planeWidth;
}
public double getPlaneHeight() {
return planeHeight;
}
public int getTilesAcross() {
return tilesAcross;
}
public int getTileAmount() {
return tileAmount;
}
public int getGridSize() {
return gridSize;
}
public int getTilesDown() {
return tilesDown;
}
public AnchorPane getAnchorPane() {
return anchorPane;
}
}
package com.example.gridbaseddraggable;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
public class GridHandler extends GridBase {
Color backgroundColor1 = Color.WHITE;
Color backgroundColor2 = Color.color(0.82,0.82,0.82);
public GridHandler(double planeWidth, double planeHeight, int gridSize, AnchorPane anchorPane) {
super(planeWidth, planeHeight, gridSize, anchorPane);
}
public void updateGrid() {
for(int i = 0; i < getTileAmount(); i++){
int x = (i % getTilesAcross());
int y = (i / getTilesAcross());
Rectangle rectangle = new Rectangle(x * getGridSize(),y * getGridSize(),getGridSize(),getGridSize());
if((x + y) % 2 == 0){
rectangle.setFill(backgroundColor1);
} else {
rectangle.setFill(backgroundColor2);
}
getAnchorPane().getChildren().add(rectangle);
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane fx:id="pane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.gridbaseddraggable.Controller" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment