package test; import javafx.application.Application; import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.Node; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.input.ClipboardContent; import javafx.scene.input.DragEvent; import javafx.scene.input.Dragboard; import javafx.scene.input.KeyCode; import javafx.scene.input.KeyEvent; import javafx.scene.input.MouseEvent; import javafx.scene.input.ScrollEvent; import javafx.scene.input.TransferMode; import javafx.scene.layout.Pane; import javafx.scene.text.Font; import javafx.scene.text.Text; import javafx.stage.Stage; public class PaneEvents extends Application { @Override public void start(Stage stage) { stage.setTitle("Pane events - RT-31021"); Group root = new Group(); Scene scene = new Scene(root, 600, 600); final Display display = new Display(100.0, 100.0, 300.0, 300.0); Text source = new Text("Drag from here to the pane"); source.setFont(new Font(20)); source.relocate(20, 20); addDragHandlers(source, display); root.getChildren().addAll(display, source); stage.setScene(scene); stage.show(); } private void addDragHandlers(final Node source, final Pane target) { source.setOnDragDetected(new EventHandler() { @Override public void handle(MouseEvent event) { Dragboard db = source.startDragAndDrop(TransferMode.COPY); ClipboardContent cc = new ClipboardContent(); cc.putString("Dummy"); db.setContent(cc); } }); target.setOnDragOver(new EventHandler() { @Override public void handle(DragEvent event) { event.acceptTransferModes(TransferMode.ANY); } }); target.setOnDragDropped(new EventHandler() { @Override public void handle(DragEvent event) { Button b = new Button("Dropped button"); b.relocate(event.getX(), event.getY()); target.getChildren().add(b); } }); } public static class Display extends Pane { public Display(Double x, Double y, Double width, Double height) { super(); relocate(x, y); setPrefSize(width, height); setStyle("-fx-background-color: gray;"); addEventHandler(MouseEvent.ANY, new MouseHandler(this)); } } public static class MouseHandler implements EventHandler { private double sceneAnchorX; private double sceneAnchorY; private double anchorAngle; private Parent parent; private final Node nodeToMove; MouseHandler(Node nodeToMove) { this.nodeToMove = nodeToMove; } @Override public void handle(MouseEvent event) { if (event.getEventType() == MouseEvent.MOUSE_PRESSED) { sceneAnchorX = event.getSceneX(); sceneAnchorY = event.getSceneY(); anchorAngle = nodeToMove.getRotate(); event.consume(); } else if (event.getEventType() == MouseEvent.MOUSE_DRAGGED) { if (event.isControlDown()) { // nodeToMove.setRotationAxis(new Point3D(sceneAnchorY,event.getSceneX(),0)); nodeToMove.setRotate(anchorAngle + sceneAnchorX - event.getSceneX()); sceneAnchorX = event.getSceneX(); sceneAnchorY = event.getSceneY(); anchorAngle = nodeToMove.getRotate(); event.consume(); } else { double x = event.getSceneX(); double y = event.getSceneY(); nodeToMove.setTranslateX(nodeToMove.getTranslateX() + x - sceneAnchorX); nodeToMove.setTranslateY(nodeToMove.getTranslateY() + y - sceneAnchorY); sceneAnchorX = x; sceneAnchorY = y; event.consume(); } } else if (event.getEventType() == MouseEvent.MOUSE_CLICKED) { // nodeToMove.setFocusTraversable(true); nodeToMove.requestFocus(); event.consume(); } nodeToMove.setOnKeyPressed(new EventHandler() { @Override public void handle(KeyEvent event) { if (KeyCode.UP.equals(event.getCode())) { nodeToMove.setScaleX(nodeToMove.getScaleX() + .1); nodeToMove.setScaleY(nodeToMove.getScaleY() + .1); nodeToMove.setScaleZ(nodeToMove.getScaleZ() + .1); System.out.println("kaw"); event.consume(); } if (KeyCode.DOWN.equals(event.getCode())) { if (nodeToMove.getScaleX() > 0.15) { nodeToMove.setScaleX(nodeToMove.getScaleX() - .1); nodeToMove.setScaleY(nodeToMove.getScaleY() - .1); nodeToMove.setScaleZ(nodeToMove.getScaleZ() - .1); } System.out.println(nodeToMove.getScaleX()); event.consume(); } nodeToMove.setOnScroll(new EventHandler() { @Override public void handle(ScrollEvent event) { if (nodeToMove.isFocused()) { if (event.getDeltaY() > 0) { nodeToMove.setTranslateZ(10 + nodeToMove.getTranslateZ()); } else { nodeToMove.setTranslateZ(-10 + nodeToMove.getTranslateZ()); } event.consume(); } } }); } }); } } public static void main(String[] args) { Application.launch(args); } }