Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Thomas Clancy
Added: Dec 21, 2020 10:03 PM
Views: 11
Tags: no tags
  1. /*
  2.  * The MIT License
  3.  *
  4.  * Copyright 2020 tjclancyhome.
  5.  *
  6.  * Permission is hereby granted, free of charge, to any person obtaining a copy
  7.  * of this software and associated documentation files (the "Software"), to deal
  8.  * in the Software without restriction, including without limitation the rights
  9.  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10.  * copies of the Software, and to permit persons to whom the Software is
  11.  * furnished to do so, subject to the following conditions:
  12.  *
  13.  * The above copyright notice and this permission notice shall be included in
  14.  * all copies or substantial portions of the Software.
  15.  *
  16.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19.  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21.  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22.  * THE SOFTWARE.
  23.  */
  24. package org.tjc.jfx.graph;
  25.  
  26. import org.tjc.jfx.jfxsamplecomponents.ShapePane;
  27. import javafx.event.ActionEvent;
  28. import javafx.fxml.FXML;
  29. import javafx.scene.control.ScrollPane;
  30. import javafx.scene.control.ToolBar;
  31. import javafx.scene.effect.BlurType;
  32. import javafx.scene.effect.DropShadow;
  33. import javafx.scene.layout.AnchorPane;
  34. import javafx.scene.layout.VBox;
  35. import javafx.scene.paint.Color;
  36. import javafx.scene.paint.Paint;
  37. import javafx.scene.shape.Line;
  38. import javafx.scene.shape.Rectangle;
  39. import org.slf4j.Logger;
  40. import org.slf4j.LoggerFactory;
  41. import org.tjc.jfx.jfxsamplecomponents.Builders;
  42. import org.tjc.jfx.jfxsamplecomponents.Builders.DropShadowBuilder;
  43.  
  44. public class GraphSceneController {
  45.     private static final Logger log = LoggerFactory.getLogger(GraphSceneController.class);
  46.     private static final double DEFAULT_STROKE_WIDTH = 1.0;
  47.     private DropShadow dropShadow;
  48.     private ShapePane roundedRectangle;
  49.     private ShapePane rectangle;
  50.     private ShapePane line;
  51.  
  52.     @FXML
  53.     private VBox mainWindow;
  54.     @FXML
  55.     private ToolBar toolBar;
  56.     @FXML
  57.     private AnchorPane graphPane;
  58.     @FXML
  59.     private ScrollPane graphScrollPane;
  60.  
  61.     /**
  62.      * Initializes the controller class.
  63.      */
  64.     public void initialize() {
  65.         this.dropShadow = buildDropShadow();
  66.         rectangle = makeRectangle(100, 100, 50, 50, Color.PEACHPUFF);
  67.         roundedRectangle = makeRoundedRectangle(175, 100, 50, 50, 10, 10, Color.PEACHPUFF);
  68.         graphPane.getChildren().addAll(
  69.             rectangle,
  70.             roundedRectangle);
  71.         connectShapes(rectangle, roundedRectangle);
  72.     }
  73.  
  74.     private ShapePane makeRectangle(double x, double y, double width, double height, Paint paint) {
  75.         Rectangle shape = new Rectangle(width, height, paint);
  76.         shape.setSmooth(true);
  77.         shape.setStroke(Color.BLACK);
  78.         shape.setStrokeWidth(DEFAULT_STROKE_WIDTH);
  79.         shape.setMouseTransparent(true);
  80.  
  81.         ShapePane shapePane = new ShapePane(shape, true);
  82.         shapePane.setLayoutX(x - shapePane.getLayoutBounds().getMinX());
  83.         shapePane.setLayoutY(y - shapePane.getLayoutBounds().getMinY());
  84.         shapePane.setEffect(dropShadow);
  85.         return shapePane;
  86.     }
  87.  
  88.     private ShapePane makeRoundedRectangle(double x, double y, double width, double height, double arcHeight, double arcWidth, Paint paint) {
  89.         Rectangle shape = new Rectangle(width, height, paint);
  90.         shape.setSmooth(true);
  91.         shape.setArcHeight(arcHeight);
  92.         shape.setArcWidth(arcWidth);
  93.         shape.setStroke(Color.BLACK);
  94.         shape.setStrokeWidth(DEFAULT_STROKE_WIDTH);
  95.         shape.setMouseTransparent(true);
  96.  
  97.         ShapePane shapePane = new ShapePane(shape, true);
  98.         shapePane.setLayoutX(x - shapePane.getLayoutBounds().getMinX());
  99.         shapePane.setLayoutY(y - shapePane.getLayoutBounds().getMinY());
  100.         shapePane.setEffect(dropShadow);
  101.         return shapePane;
  102.     }
  103.  
  104.     private ShapePane makeLine(double startX, double startY, double endX, double endY) {
  105.         Line l = new Line(startX, startY, endX, endY);
  106.         ShapePane shapePane = new ShapePane(l, true);
  107.         shapePane.setLayoutX(startX - shapePane.getLayoutBounds().getMinX());
  108.         shapePane.setLayoutY(startY - shapePane.getLayoutBounds().getMinY());
  109.         return shapePane;
  110.     }
  111.  
  112.     private DropShadow buildDropShadow() {
  113.         DropShadowBuilder builder = Builders.getDropShadowBuilder();
  114.         DropShadow shadow = builder
  115.             .blurType(BlurType.GAUSSIAN)
  116.             .color(Color.DARKGREY.darker())
  117.             .height(4)
  118.             .width(4)
  119.             .radius(3)
  120.             .offsetX(3)
  121.             .offsetY(3)
  122.             .spread(0.0)
  123.             .build();
  124.         return shadow;
  125.     }
  126.  
  127.     private void connectShapes(ShapePane from, ShapePane to) {
  128.         double fromCenterX = centerX(from);
  129.         double fromCenterY = centerY(from);
  130.         double toCenterX = centerX(to);
  131.         double toCenterY = centerY(to);
  132.  
  133.         ShapePane l = makeLine(fromCenterX, fromCenterY, toCenterX, toCenterY);
  134.         this.line = l;
  135.         graphPane.getChildren().add(this.line);
  136.         this.line.toBack();
  137.     }
  138.  
  139.     private void rightOf(ShapePane p1, ShapePane p2, double widthBetween) {
  140.         p2.setLayoutX(p1.getLayoutX() + p1.getWidth() + widthBetween);
  141.         p2.setLayoutY(p1.getLayoutY());
  142.     }
  143.  
  144.     private double centerX(ShapePane pane) {
  145.         return pane.getBoundsInParent().getCenterX();
  146.     }
  147.  
  148.     private double centerY(ShapePane pane) {
  149.         return pane.getBoundsInParent().getCenterY();
  150.     }
  151.  
  152.     private void debugPrintControllerState() {
  153.         log.debug("###");
  154.         log.debug("### ==================================================");
  155.         log.debug("### Start Printing Controller State");
  156.         log.debug("### ==================================================");
  157.         log.debug("### dropShadow      : {}", dropShadow);
  158.         log.debug("### rectangle       : {}", rectangle);
  159.         log.debug("###     centerX     : {}", centerX(rectangle));
  160.         log.debug("###     centerY     : {}", centerY(rectangle));
  161.         log.debug("### roundedRectangle: {}", roundedRectangle);
  162.         log.debug("###     centerX     : {}", centerX(roundedRectangle));
  163.         log.debug("###     centerY     : {}", centerY(roundedRectangle));
  164.         log.debug("### mainWindow      : {}", mainWindow);
  165.         log.debug("### toolBar         : {}", toolBar);
  166.         log.debug("### graphPane       : {}", graphPane);
  167.         log.debug("###      graphPane.boundsInLocal : {}", graphPane.getBoundsInLocal());
  168.         log.debug("###      graphPane.boundsInParent: {}", graphPane.getBoundsInParent());
  169.         log.debug("### ==================================================");
  170.         log.debug("### Done Printing Controller State");
  171.         log.debug("### ==================================================");
  172.         log.debug("###");
  173.     }
  174.  
  175.     @FXML
  176.     private void onRoundRightOfSquare(ActionEvent event) {
  177.         rightOf(rectangle, roundedRectangle, 25);
  178.         debugPrintControllerState();
  179.     }
  180.  
  181.     @FXML
  182.     private void onSquareRightOfRound(ActionEvent event) {
  183.         rightOf(roundedRectangle, rectangle, 25);
  184.         debugPrintControllerState();
  185.     }
  186.  
  187.     @FXML
  188.     private void onPrintControllerState(ActionEvent event) {
  189.         debugPrintControllerState();
  190.     }
  191. }
  192.  
Comments disabled