/* * The MIT License * * Copyright 2020 tjclancyhome. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package org.tjc.jfx.graph; import org.tjc.jfx.jfxsamplecomponents.ShapePane; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.control.ScrollPane; import javafx.scene.control.ToolBar; import javafx.scene.effect.BlurType; import javafx.scene.effect.DropShadow; import javafx.scene.layout.AnchorPane; import javafx.scene.layout.VBox; import javafx.scene.paint.Color; import javafx.scene.paint.Paint; import javafx.scene.shape.Line; import javafx.scene.shape.Rectangle; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.tjc.jfx.jfxsamplecomponents.Builders; import org.tjc.jfx.jfxsamplecomponents.Builders.DropShadowBuilder; public class GraphSceneController { private static final Logger log = LoggerFactory.getLogger(GraphSceneController.class); private static final double DEFAULT_STROKE_WIDTH = 1.0; private DropShadow dropShadow; private ShapePane roundedRectangle; private ShapePane rectangle; private ShapePane line; @FXML private VBox mainWindow; @FXML private ToolBar toolBar; @FXML private AnchorPane graphPane; @FXML private ScrollPane graphScrollPane; /** * Initializes the controller class. */ public void initialize() { this.dropShadow = buildDropShadow(); rectangle = makeRectangle(100, 100, 50, 50, Color.PEACHPUFF); roundedRectangle = makeRoundedRectangle(175, 100, 50, 50, 10, 10, Color.PEACHPUFF); graphPane.getChildren().addAll( rectangle, roundedRectangle); connectShapes(rectangle, roundedRectangle); } private ShapePane makeRectangle(double x, double y, double width, double height, Paint paint) { Rectangle shape = new Rectangle(width, height, paint); shape.setSmooth(true); shape.setStroke(Color.BLACK); shape.setStrokeWidth(DEFAULT_STROKE_WIDTH); shape.setMouseTransparent(true); ShapePane shapePane = new ShapePane(shape, true); shapePane.setLayoutX(x - shapePane.getLayoutBounds().getMinX()); shapePane.setLayoutY(y - shapePane.getLayoutBounds().getMinY()); shapePane.setEffect(dropShadow); return shapePane; } private ShapePane makeRoundedRectangle(double x, double y, double width, double height, double arcHeight, double arcWidth, Paint paint) { Rectangle shape = new Rectangle(width, height, paint); shape.setSmooth(true); shape.setArcHeight(arcHeight); shape.setArcWidth(arcWidth); shape.setStroke(Color.BLACK); shape.setStrokeWidth(DEFAULT_STROKE_WIDTH); shape.setMouseTransparent(true); ShapePane shapePane = new ShapePane(shape, true); shapePane.setLayoutX(x - shapePane.getLayoutBounds().getMinX()); shapePane.setLayoutY(y - shapePane.getLayoutBounds().getMinY()); shapePane.setEffect(dropShadow); return shapePane; } private ShapePane makeLine(double startX, double startY, double endX, double endY) { Line l = new Line(startX, startY, endX, endY); ShapePane shapePane = new ShapePane(l, true); shapePane.setLayoutX(startX - shapePane.getLayoutBounds().getMinX()); shapePane.setLayoutY(startY - shapePane.getLayoutBounds().getMinY()); return shapePane; } private DropShadow buildDropShadow() { DropShadowBuilder builder = Builders.getDropShadowBuilder(); DropShadow shadow = builder .blurType(BlurType.GAUSSIAN) .color(Color.DARKGREY.darker()) .height(4) .width(4) .radius(3) .offsetX(3) .offsetY(3) .spread(0.0) .build(); return shadow; } private void connectShapes(ShapePane from, ShapePane to) { double fromCenterX = centerX(from); double fromCenterY = centerY(from); double toCenterX = centerX(to); double toCenterY = centerY(to); ShapePane l = makeLine(fromCenterX, fromCenterY, toCenterX, toCenterY); this.line = l; graphPane.getChildren().add(this.line); this.line.toBack(); } private void rightOf(ShapePane p1, ShapePane p2, double widthBetween) { p2.setLayoutX(p1.getLayoutX() + p1.getWidth() + widthBetween); p2.setLayoutY(p1.getLayoutY()); } private double centerX(ShapePane pane) { return pane.getBoundsInParent().getCenterX(); } private double centerY(ShapePane pane) { return pane.getBoundsInParent().getCenterY(); } private void debugPrintControllerState() { log.debug("###"); log.debug("### =================================================="); log.debug("### Start Printing Controller State"); log.debug("### =================================================="); log.debug("### dropShadow : {}", dropShadow); log.debug("### rectangle : {}", rectangle); log.debug("### centerX : {}", centerX(rectangle)); log.debug("### centerY : {}", centerY(rectangle)); log.debug("### roundedRectangle: {}", roundedRectangle); log.debug("### centerX : {}", centerX(roundedRectangle)); log.debug("### centerY : {}", centerY(roundedRectangle)); log.debug("### mainWindow : {}", mainWindow); log.debug("### toolBar : {}", toolBar); log.debug("### graphPane : {}", graphPane); log.debug("### graphPane.boundsInLocal : {}", graphPane.getBoundsInLocal()); log.debug("### graphPane.boundsInParent: {}", graphPane.getBoundsInParent()); log.debug("### =================================================="); log.debug("### Done Printing Controller State"); log.debug("### =================================================="); log.debug("###"); } @FXML private void onRoundRightOfSquare(ActionEvent event) { rightOf(rectangle, roundedRectangle, 25); debugPrintControllerState(); } @FXML private void onSquareRightOfRound(ActionEvent event) { rightOf(roundedRectangle, rectangle, 25); debugPrintControllerState(); } @FXML private void onPrintControllerState(ActionEvent event) { debugPrintControllerState(); } }