Advertisement
John_Conner

JavaFx-RandomLinePathGenerator

Jul 25th, 2014
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.12 KB | None | 0 0
  1. // The Main class is below
  2.  
  3. import javafx.application.Application;
  4. import javafx.geometry.Insets;
  5. import javafx.geometry.Pos;
  6. import javafx.scene.Scene;
  7. import javafx.scene.control.Button;
  8. import javafx.scene.input.KeyCode;
  9. import javafx.scene.layout.BorderPane;
  10. import javafx.scene.layout.Pane;
  11. import javafx.scene.paint.Color;
  12. import javafx.stage.Stage;
  13.  
  14. /**
  15.  * Created by John on 7/24/2014.
  16.  */
  17. public class RandomPathGenerator extends Application {
  18.     // Direction data fields
  19.     double lineLength = 10;
  20.     KeyCode goUp = KeyCode.UP;
  21.     KeyCode goLeft = KeyCode.LEFT;
  22.     KeyCode goDown = KeyCode.DOWN;
  23.     KeyCode goRight = KeyCode.RIGHT;
  24.  
  25.     // Create a LineDrawing Object and BorderPane
  26.     BorderPane pane;
  27.     double paneWidth = 250;
  28.     double paneHeight = 250;
  29.     LineDrawingObject drawLine;
  30.  
  31.     @Override // Overrides the start method in the Application class
  32.     public void start(Stage primaryStage) {
  33.         // Create a BorderPane
  34.         pane = new BorderPane();
  35.         pane.setPadding(new Insets(5));
  36.  
  37.         // Create a Pane and add it to the BorderPane
  38.         Pane centerPane = new Pane();
  39.         centerPane.setStyle("-fx-border-color: black; -fx-border-width: 1;");
  40.         centerPane.setPrefSize(250, 250);
  41.         pane.setCenter(centerPane);
  42.         BorderPane.setAlignment(centerPane, Pos.CENTER);
  43.  
  44.         // Create a Button and add it to the bottom and center it
  45.         Button btPlay = new Button("Play");
  46.         pane.setBottom(btPlay);
  47.         BorderPane.setAlignment(btPlay, Pos.CENTER);
  48.  
  49.         // Create a LineDrawingObject
  50.         drawLine = new LineDrawingObject(lineLength,
  51.                 Color.BLACK, paneWidth, paneHeight);
  52.         centerPane.getChildren().add(drawLine);
  53.  
  54.         // Generate a random path upon button clicked
  55.         btPlay.setOnAction(e -> {
  56.             // Restart the LineDrawingObject
  57.             centerPane.getChildren().remove(drawLine);
  58.             drawLine = new LineDrawingObject(lineLength,
  59.                     Color.BLACK, paneWidth, paneHeight);
  60.             centerPane.getChildren().add(drawLine);
  61.             // Generate random walk while nothing is blocking path
  62.             generateWalk();
  63.         });
  64.  
  65.         // Create a scene and add it to the stage
  66.         Scene scene = new Scene(pane);
  67.         primaryStage.setTitle("RandomPathGenerator"); // Set the stage title
  68.         primaryStage.setScene(scene); // Place the scene in the stage
  69.         primaryStage.show(); // Display the stage
  70.     }
  71.  
  72.     /** Method for generating a random direction */
  73.     public KeyCode setDirection() {
  74.         double direction = 1 * Math.random() * 4;
  75.  
  76.         switch ((int) direction) {
  77.             case 0: return goUp;
  78.             case 1: return goLeft;
  79.             case 2: return goDown;
  80.             case 3: return goRight;
  81.             default: System.out.printf("Switch was passed: %d",
  82.                     (int) direction);
  83.         }
  84.  
  85.         return goUp;
  86.     }
  87.  
  88.     /** Method for generating random walk until path is blocked */
  89.     public void generateWalk() {
  90.         boolean chk = true;
  91.         /* While the path isn't blocked and the lines are inside the pane
  92.            generate new lines */
  93.         while (true) {
  94.             // Randomly Generate a direction
  95.             KeyCode direction = setDirection();
  96.             /* Check to make sure the direction won't go backwards
  97.                if so then generate a new direction until it doesn't */
  98.             while (chk)
  99.             switch (direction) {
  100.                 case UP:
  101.                     if (drawLine.getLine().endYProperty().doubleValue() - lineLength !=
  102.                         drawLine.getLine().startYProperty().doubleValue()) {
  103.                         chk = false;
  104.                     } else
  105.                         direction = setDirection();
  106.                     break;
  107.                 case LEFT:
  108.                     if (drawLine.getLine().endXProperty().doubleValue() - lineLength !=
  109.                             drawLine.getLine().startXProperty().doubleValue()) {
  110.                         chk = false;
  111.                     } else
  112.                         direction = setDirection();
  113.                     break;
  114.                 case DOWN:
  115.                     if (drawLine.getLine().endYProperty().doubleValue() + lineLength !=
  116.                             drawLine.getLine().startYProperty().doubleValue()) {
  117.                         chk = false;
  118.                     } else
  119.                         direction = setDirection();
  120.                     break;
  121.                 case RIGHT:
  122.                     if (drawLine.getLine().endXProperty().doubleValue() + lineLength !=
  123.                             drawLine.getLine().startXProperty().doubleValue()) {
  124.                         chk = false;
  125.                     } else
  126.                         direction = setDirection();
  127.                     break;
  128.             }
  129.  
  130.             /* If a new line will intersect beginning or ending coordinates
  131.                of an existing line or run of the pane then end the loop */
  132.             for (int i = 0; i < drawLine.getLines().size(); i++) {
  133.                 /* Get the end of the new lawn to be drawn and compare it too
  134.                    the lines in the ArrayList<Line> */
  135.                 double newLineStartX = drawLine.getLine().endXProperty().doubleValue();
  136.                 double newLineStartY = drawLine.getLine().endYProperty().doubleValue();
  137.                 double lineStartX = drawLine.getLines().get(i).startXProperty().doubleValue();
  138.                 double lineStartY = drawLine.getLines().get(i).startYProperty().doubleValue();
  139.                 double lineEndX = drawLine.getLines().get(i).endXProperty().doubleValue();
  140.                 double lineEndY = drawLine.getLines().get(i).endYProperty().doubleValue();
  141.  
  142.                 /* Compare the next line for intersection with a previous line
  143.                    and ensure that the next line won't run off the Pane */
  144.                 switch (direction) {
  145.                     case UP:
  146.                         if (newLineStartX == lineStartX &&
  147.                             newLineStartY - lineLength == lineStartY ||
  148.                             newLineStartX == lineEndX &&
  149.                             newLineStartY - lineLength == lineEndY ||
  150.                             newLineStartY - lineLength <= 0 ) {
  151.                             return;
  152.                         } break;
  153.                     case LEFT:
  154.                         if (newLineStartX - lineLength == lineStartX &&
  155.                             newLineStartY == lineStartY ||
  156.                             newLineStartX - lineLength == lineEndX &&
  157.                             newLineStartY == lineEndY ||
  158.                             newLineStartX - lineLength <= 0 ) {
  159.                             return;
  160.                         } break;
  161.                     case DOWN:
  162.                         if (newLineStartX == lineStartX &&
  163.                             newLineStartY + lineLength == lineStartY ||
  164.                             newLineStartX == lineEndX &&
  165.                             newLineStartY + lineLength == lineEndY ||
  166.                             newLineStartY + lineLength >= paneHeight ) {
  167.                             return;
  168.                         } break;
  169.                     case RIGHT:
  170.                         if (newLineStartX + lineLength == lineStartX &&
  171.                             newLineStartY == lineStartY ||
  172.                             newLineStartX + lineLength == lineEndX &&
  173.                             newLineStartY == lineEndY ||
  174.                             newLineStartX - lineLength >= paneWidth ) {
  175.                             return;
  176.                         } break;
  177.                 }
  178.             }
  179.             // Draw the next line
  180.             drawLine.paintLine(direction);
  181.  
  182.             // Reset chk to true
  183.             chk = true;
  184.         }
  185.     }
  186. }
  187.  
  188.  
  189.  
  190.  
  191. // The LineDrawingObject.java is below
  192.  
  193. /**
  194.  * Created by John on 7/24/2014.
  195.  */
  196.  
  197. import javafx.scene.input.KeyCode;
  198. import javafx.scene.layout.Pane;
  199. import javafx.scene.paint.Color;
  200. import javafx.scene.shape.Line;
  201.  
  202. import java.util.ArrayList;
  203.  
  204. /** This object will draw lines inside of a Pane when an arrow key is
  205.  *  pressed and will draw it in that direction from the current line */
  206. public class LineDrawingObject extends Pane {
  207.     // ArrayList to store the Line Object's
  208.     ArrayList<Line> lines = new ArrayList<>();
  209.     Line line;
  210.     private Color lineColor;
  211.     private double lineLength;
  212.     private double startX;
  213.     private double startY;
  214.     private double endX;
  215.     private double endY;
  216.  
  217.     /** Default Constructor */
  218.     public LineDrawingObject() {
  219.         // Set a default size for this Pane
  220.         this.setWidth(350);
  221.         this.setHeight(350);
  222.         // Set Line properties
  223.         this.startX = getWidth() / 2;
  224.         this.startY = getHeight() / 2;
  225.         this.lineLength = 20;
  226.         this.lineColor = Color.BLACK;
  227.         // Create line and set it's Stroke
  228.         line = new Line(this.startX, this.startY,
  229.                 this.startX, this.startY - this.lineLength);
  230.         line.setStroke(this.lineColor);
  231.         // Add line to ArrayList and Pane
  232.         lines.add(line);
  233.         getChildren().add(line);
  234.     }
  235.  
  236.     /** Second Constructor, allows you to control the line length, color and center it */
  237.     public LineDrawingObject(double lineLength, Color lineColor) {
  238.         // Set a default size for this Pane
  239.         this.setWidth(350);
  240.         this.setHeight(350);
  241.         // Set line properties
  242.         this.startX = getWidth() / 2;
  243.         this.startY = getHeight() / 2;
  244.         this.lineLength = lineLength;
  245.         this.lineColor = lineColor;
  246.         // Create line and set it's Stroke
  247.         line = new Line(this.startX, this.startY,
  248.                 this.startX, this.startY);
  249.         line.setStroke(this.lineColor);
  250.         // Add line to ArrayList and Pane
  251.         lines.add(line);
  252.         getChildren().add(line);
  253.     }
  254.  
  255.     /** Third Constructor, allows you to control the line length, color, and pane size */
  256.     public LineDrawingObject(double lineLength, Color lineColor,
  257.                              double paneWidth, double paneHeight) {
  258.         // Set a default size for this Pane
  259.         this.setWidth(paneWidth);
  260.         this.setHeight(paneHeight);
  261.         // Set line properties
  262.         this.startX = getWidth() / 2;
  263.         this.startY = getHeight() / 2;
  264.         this.lineLength = lineLength;
  265.         this.lineColor = lineColor;
  266.         // Create line and set it's Stroke
  267.         line = new Line(this.startX, this.startY,
  268.                 this.startX, this.startY);
  269.         line.setStroke(this.lineColor);
  270.         // Add line to ArrayList and Pane
  271.         lines.add(line);
  272.         getChildren().add(line);
  273.     }
  274.  
  275.     /** Third Constructor, allows you to control the line length and color, startX, and startY */
  276.     public LineDrawingObject(double lineLength, double startX, double startY, Color lineColor) {
  277.         // Set line properties
  278.         this.startX = startX;
  279.         this.startY = startY;
  280.         this.lineLength = lineLength;
  281.         this.lineColor = lineColor;
  282.         // Create line and set it's Stroke
  283.         line = new Line(this.startX, this.startY,
  284.                 this.startX, this.startY);
  285.         line.setStroke(this.lineColor);
  286.         // Add line to ArrayList and Pane
  287.         lines.add(line);
  288.         getChildren().add(line);
  289.     }
  290.  
  291.     /** Get the list of lines */
  292.     public ArrayList<Line> getLines() {
  293.         return lines;
  294.     }
  295.  
  296.     /** Get the current line object */
  297.     public Line getLine() {
  298.         return this.line;
  299.     }
  300.  
  301.     /** Manually set the current line object */
  302.     public void setLine(Line line) {
  303.         this.line = line;
  304.     }
  305.  
  306.     /** Get the current line object's color */
  307.     public Color getLineColor() {
  308.         return this.lineColor;
  309.     }
  310.  
  311.     /** Set the current line object's color */
  312.     public void setLineColor(Color lineColor) {
  313.         this.lineColor = lineColor;
  314.     }
  315.  
  316.     /** Get length of the lines */
  317.     public double getLineLength() {
  318.         return this.lineLength;
  319.     }
  320.  
  321.     /** Set a new length for the lines */
  322.     public void setLineLength(double lineLength) {
  323.         this.lineLength = lineLength;
  324.     }
  325.  
  326.     /** Get the count of the number of line's currently in existence */
  327.     public int getLineCount() {
  328.         return this.lines.size();
  329.     }
  330.  
  331.     /** Get the startX coordinates */
  332.     public double getStartX() {
  333.         return this.startX;
  334.     }
  335.  
  336.     /** Set the startX coordinates */
  337.     public void setStartX(double startX) {
  338.         this.startX = startX;
  339.     }
  340.  
  341.     /** Get the startY coordinates */
  342.     public double getStartY() {
  343.         return this.startY;
  344.     }
  345.  
  346.     /** Set the startY coordinates */
  347.     public void setStartY(double startY) {
  348.         this.startY = startY;
  349.     }
  350.  
  351.     /** Get the endX coordinates */
  352.     public double getEndX() {
  353.         return this.endX;
  354.     }
  355.  
  356.     /** Set the endX coordinates */
  357.     public void setEndX(double endX) {
  358.         this.endX = endX;
  359.     }
  360.  
  361.     /** Get the endY coordinates */
  362.     public double getEndY() {
  363.         return this.endY;
  364.     }
  365.  
  366.     /** Set the endY coordinates */
  367.     public void setEndY(double endY) {
  368.         this.endY = endY;
  369.     }
  370.  
  371.     /** Clears the scene for random walks */
  372.     public void clearLines() {
  373.         getChildren().clear();
  374.     }
  375.  
  376.     /** Paint the next line based on the key pressed */
  377.     public void paintLine(KeyCode keyCode) {
  378.         // Set line start coordinates to the end of the last line
  379.         setStartX(line.getEndX());
  380.         setStartY(line.getEndY());
  381.  
  382.         // Set line end coordinates
  383.         switch (keyCode) {
  384.             case UP: goUp(); break;
  385.             case LEFT: goLeft(); break;
  386.             case DOWN: goDown(); break;
  387.             case RIGHT: goRight(); break;
  388.         }
  389.  
  390.         // Create line
  391.         line = new Line(getStartX(), getStartY(), getEndX(), getEndY());
  392.         line.setStroke(lineColor);
  393.         this.lines.add(line);
  394.         getChildren().add(line);
  395.     }
  396.  
  397.     /** Set the end coordinates to the left of the last line */
  398.     public void goLeft() {
  399.         setEndX(getStartX() - this.lineLength);
  400.         setEndY(getStartY());
  401.     }
  402.  
  403.     /** Set the end coordinates to the right of the last line */
  404.     public void goRight() {
  405.         setEndX(getStartX() + this.lineLength);
  406.         setEndY(getStartY());
  407.     }
  408.  
  409.     /** Set the end coordinates above the last line */
  410.     public void goUp() {
  411.         setEndX(getStartX());
  412.         setEndY(getStartY() - this.lineLength);
  413.     }
  414.  
  415.     /** Set the end coordinates below the last line */
  416.     public void goDown() {
  417.         setEndX(getStartX());
  418.         setEndY(getStartY() + this.lineLength);
  419.     }
  420. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement