Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // The Main class is below
- import javafx.application.Application;
- import javafx.geometry.Insets;
- import javafx.geometry.Pos;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.input.KeyCode;
- import javafx.scene.layout.BorderPane;
- import javafx.scene.layout.Pane;
- import javafx.scene.paint.Color;
- import javafx.stage.Stage;
- /**
- * Created by John on 7/24/2014.
- */
- public class RandomPathGenerator extends Application {
- // Direction data fields
- double lineLength = 10;
- KeyCode goUp = KeyCode.UP;
- KeyCode goLeft = KeyCode.LEFT;
- KeyCode goDown = KeyCode.DOWN;
- KeyCode goRight = KeyCode.RIGHT;
- // Create a LineDrawing Object and BorderPane
- BorderPane pane;
- double paneWidth = 250;
- double paneHeight = 250;
- LineDrawingObject drawLine;
- @Override // Overrides the start method in the Application class
- public void start(Stage primaryStage) {
- // Create a BorderPane
- pane = new BorderPane();
- pane.setPadding(new Insets(5));
- // Create a Pane and add it to the BorderPane
- Pane centerPane = new Pane();
- centerPane.setStyle("-fx-border-color: black; -fx-border-width: 1;");
- centerPane.setPrefSize(250, 250);
- pane.setCenter(centerPane);
- BorderPane.setAlignment(centerPane, Pos.CENTER);
- // Create a Button and add it to the bottom and center it
- Button btPlay = new Button("Play");
- pane.setBottom(btPlay);
- BorderPane.setAlignment(btPlay, Pos.CENTER);
- // Create a LineDrawingObject
- drawLine = new LineDrawingObject(lineLength,
- Color.BLACK, paneWidth, paneHeight);
- centerPane.getChildren().add(drawLine);
- // Generate a random path upon button clicked
- btPlay.setOnAction(e -> {
- // Restart the LineDrawingObject
- centerPane.getChildren().remove(drawLine);
- drawLine = new LineDrawingObject(lineLength,
- Color.BLACK, paneWidth, paneHeight);
- centerPane.getChildren().add(drawLine);
- // Generate random walk while nothing is blocking path
- generateWalk();
- });
- // Create a scene and add it to the stage
- Scene scene = new Scene(pane);
- primaryStage.setTitle("RandomPathGenerator"); // Set the stage title
- primaryStage.setScene(scene); // Place the scene in the stage
- primaryStage.show(); // Display the stage
- }
- /** Method for generating a random direction */
- public KeyCode setDirection() {
- double direction = 1 * Math.random() * 4;
- switch ((int) direction) {
- case 0: return goUp;
- case 1: return goLeft;
- case 2: return goDown;
- case 3: return goRight;
- default: System.out.printf("Switch was passed: %d",
- (int) direction);
- }
- return goUp;
- }
- /** Method for generating random walk until path is blocked */
- public void generateWalk() {
- boolean chk = true;
- /* While the path isn't blocked and the lines are inside the pane
- generate new lines */
- while (true) {
- // Randomly Generate a direction
- KeyCode direction = setDirection();
- /* Check to make sure the direction won't go backwards
- if so then generate a new direction until it doesn't */
- while (chk)
- switch (direction) {
- case UP:
- if (drawLine.getLine().endYProperty().doubleValue() - lineLength !=
- drawLine.getLine().startYProperty().doubleValue()) {
- chk = false;
- } else
- direction = setDirection();
- break;
- case LEFT:
- if (drawLine.getLine().endXProperty().doubleValue() - lineLength !=
- drawLine.getLine().startXProperty().doubleValue()) {
- chk = false;
- } else
- direction = setDirection();
- break;
- case DOWN:
- if (drawLine.getLine().endYProperty().doubleValue() + lineLength !=
- drawLine.getLine().startYProperty().doubleValue()) {
- chk = false;
- } else
- direction = setDirection();
- break;
- case RIGHT:
- if (drawLine.getLine().endXProperty().doubleValue() + lineLength !=
- drawLine.getLine().startXProperty().doubleValue()) {
- chk = false;
- } else
- direction = setDirection();
- break;
- }
- /* If a new line will intersect beginning or ending coordinates
- of an existing line or run of the pane then end the loop */
- for (int i = 0; i < drawLine.getLines().size(); i++) {
- /* Get the end of the new lawn to be drawn and compare it too
- the lines in the ArrayList<Line> */
- double newLineStartX = drawLine.getLine().endXProperty().doubleValue();
- double newLineStartY = drawLine.getLine().endYProperty().doubleValue();
- double lineStartX = drawLine.getLines().get(i).startXProperty().doubleValue();
- double lineStartY = drawLine.getLines().get(i).startYProperty().doubleValue();
- double lineEndX = drawLine.getLines().get(i).endXProperty().doubleValue();
- double lineEndY = drawLine.getLines().get(i).endYProperty().doubleValue();
- /* Compare the next line for intersection with a previous line
- and ensure that the next line won't run off the Pane */
- switch (direction) {
- case UP:
- if (newLineStartX == lineStartX &&
- newLineStartY - lineLength == lineStartY ||
- newLineStartX == lineEndX &&
- newLineStartY - lineLength == lineEndY ||
- newLineStartY - lineLength <= 0 ) {
- return;
- } break;
- case LEFT:
- if (newLineStartX - lineLength == lineStartX &&
- newLineStartY == lineStartY ||
- newLineStartX - lineLength == lineEndX &&
- newLineStartY == lineEndY ||
- newLineStartX - lineLength <= 0 ) {
- return;
- } break;
- case DOWN:
- if (newLineStartX == lineStartX &&
- newLineStartY + lineLength == lineStartY ||
- newLineStartX == lineEndX &&
- newLineStartY + lineLength == lineEndY ||
- newLineStartY + lineLength >= paneHeight ) {
- return;
- } break;
- case RIGHT:
- if (newLineStartX + lineLength == lineStartX &&
- newLineStartY == lineStartY ||
- newLineStartX + lineLength == lineEndX &&
- newLineStartY == lineEndY ||
- newLineStartX - lineLength >= paneWidth ) {
- return;
- } break;
- }
- }
- // Draw the next line
- drawLine.paintLine(direction);
- // Reset chk to true
- chk = true;
- }
- }
- }
- // The LineDrawingObject.java is below
- /**
- * Created by John on 7/24/2014.
- */
- import javafx.scene.input.KeyCode;
- import javafx.scene.layout.Pane;
- import javafx.scene.paint.Color;
- import javafx.scene.shape.Line;
- import java.util.ArrayList;
- /** This object will draw lines inside of a Pane when an arrow key is
- * pressed and will draw it in that direction from the current line */
- public class LineDrawingObject extends Pane {
- // ArrayList to store the Line Object's
- ArrayList<Line> lines = new ArrayList<>();
- Line line;
- private Color lineColor;
- private double lineLength;
- private double startX;
- private double startY;
- private double endX;
- private double endY;
- /** Default Constructor */
- public LineDrawingObject() {
- // Set a default size for this Pane
- this.setWidth(350);
- this.setHeight(350);
- // Set Line properties
- this.startX = getWidth() / 2;
- this.startY = getHeight() / 2;
- this.lineLength = 20;
- this.lineColor = Color.BLACK;
- // Create line and set it's Stroke
- line = new Line(this.startX, this.startY,
- this.startX, this.startY - this.lineLength);
- line.setStroke(this.lineColor);
- // Add line to ArrayList and Pane
- lines.add(line);
- getChildren().add(line);
- }
- /** Second Constructor, allows you to control the line length, color and center it */
- public LineDrawingObject(double lineLength, Color lineColor) {
- // Set a default size for this Pane
- this.setWidth(350);
- this.setHeight(350);
- // Set line properties
- this.startX = getWidth() / 2;
- this.startY = getHeight() / 2;
- this.lineLength = lineLength;
- this.lineColor = lineColor;
- // Create line and set it's Stroke
- line = new Line(this.startX, this.startY,
- this.startX, this.startY);
- line.setStroke(this.lineColor);
- // Add line to ArrayList and Pane
- lines.add(line);
- getChildren().add(line);
- }
- /** Third Constructor, allows you to control the line length, color, and pane size */
- public LineDrawingObject(double lineLength, Color lineColor,
- double paneWidth, double paneHeight) {
- // Set a default size for this Pane
- this.setWidth(paneWidth);
- this.setHeight(paneHeight);
- // Set line properties
- this.startX = getWidth() / 2;
- this.startY = getHeight() / 2;
- this.lineLength = lineLength;
- this.lineColor = lineColor;
- // Create line and set it's Stroke
- line = new Line(this.startX, this.startY,
- this.startX, this.startY);
- line.setStroke(this.lineColor);
- // Add line to ArrayList and Pane
- lines.add(line);
- getChildren().add(line);
- }
- /** Third Constructor, allows you to control the line length and color, startX, and startY */
- public LineDrawingObject(double lineLength, double startX, double startY, Color lineColor) {
- // Set line properties
- this.startX = startX;
- this.startY = startY;
- this.lineLength = lineLength;
- this.lineColor = lineColor;
- // Create line and set it's Stroke
- line = new Line(this.startX, this.startY,
- this.startX, this.startY);
- line.setStroke(this.lineColor);
- // Add line to ArrayList and Pane
- lines.add(line);
- getChildren().add(line);
- }
- /** Get the list of lines */
- public ArrayList<Line> getLines() {
- return lines;
- }
- /** Get the current line object */
- public Line getLine() {
- return this.line;
- }
- /** Manually set the current line object */
- public void setLine(Line line) {
- this.line = line;
- }
- /** Get the current line object's color */
- public Color getLineColor() {
- return this.lineColor;
- }
- /** Set the current line object's color */
- public void setLineColor(Color lineColor) {
- this.lineColor = lineColor;
- }
- /** Get length of the lines */
- public double getLineLength() {
- return this.lineLength;
- }
- /** Set a new length for the lines */
- public void setLineLength(double lineLength) {
- this.lineLength = lineLength;
- }
- /** Get the count of the number of line's currently in existence */
- public int getLineCount() {
- return this.lines.size();
- }
- /** Get the startX coordinates */
- public double getStartX() {
- return this.startX;
- }
- /** Set the startX coordinates */
- public void setStartX(double startX) {
- this.startX = startX;
- }
- /** Get the startY coordinates */
- public double getStartY() {
- return this.startY;
- }
- /** Set the startY coordinates */
- public void setStartY(double startY) {
- this.startY = startY;
- }
- /** Get the endX coordinates */
- public double getEndX() {
- return this.endX;
- }
- /** Set the endX coordinates */
- public void setEndX(double endX) {
- this.endX = endX;
- }
- /** Get the endY coordinates */
- public double getEndY() {
- return this.endY;
- }
- /** Set the endY coordinates */
- public void setEndY(double endY) {
- this.endY = endY;
- }
- /** Clears the scene for random walks */
- public void clearLines() {
- getChildren().clear();
- }
- /** Paint the next line based on the key pressed */
- public void paintLine(KeyCode keyCode) {
- // Set line start coordinates to the end of the last line
- setStartX(line.getEndX());
- setStartY(line.getEndY());
- // Set line end coordinates
- switch (keyCode) {
- case UP: goUp(); break;
- case LEFT: goLeft(); break;
- case DOWN: goDown(); break;
- case RIGHT: goRight(); break;
- }
- // Create line
- line = new Line(getStartX(), getStartY(), getEndX(), getEndY());
- line.setStroke(lineColor);
- this.lines.add(line);
- getChildren().add(line);
- }
- /** Set the end coordinates to the left of the last line */
- public void goLeft() {
- setEndX(getStartX() - this.lineLength);
- setEndY(getStartY());
- }
- /** Set the end coordinates to the right of the last line */
- public void goRight() {
- setEndX(getStartX() + this.lineLength);
- setEndY(getStartY());
- }
- /** Set the end coordinates above the last line */
- public void goUp() {
- setEndX(getStartX());
- setEndY(getStartY() - this.lineLength);
- }
- /** Set the end coordinates below the last line */
- public void goDown() {
- setEndX(getStartX());
- setEndY(getStartY() + this.lineLength);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement