Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sample;
- import javafx.event.Event;
- import javafx.event.EventHandler;
- import javafx.geometry.Pos;
- import javafx.scene.input.MouseEvent;
- import javafx.scene.shape.*;
- import javafx.scene.layout.StackPane;
- import javafx.scene.paint.Color;
- import java.awt.event.MouseListener;
- public class Cell extends StackPane implements EventHandler<MouseEvent> {
- //row, col in grid
- private int x, y;
- private int size;
- //Shape objects
- private Rectangle rect;
- private Circle object;
- /**
- * Creates cell object given x, y and size
- * @param x horizontal coordinate of Cell in the grid
- * @param y vertical coordinate of Cell in the grid
- * @param mazeSize Size of Maze
- */
- public Cell(int x, int y, int mazeSize)
- {
- //Set maze size
- size = mazeSize;
- //Individual cell sizes. Based on maze size
- double size = 800.00/mazeSize;
- //Initialize Cell Sizes
- this.setPrefSize(size, size);
- this.setStyle("-fx-background-color: black;");
- this.setAlignment(Pos.CENTER);
- //Set Cell position
- this.x = x;
- this.y = y;
- //Draw white rectangle and add button click
- rect = new Rectangle(size - 2, size - 2, Color.WHITE);
- this.getChildren().add(rect);
- ///If at starting Cell add player
- if(x == 0 && y == 0)
- {
- object = new Circle(size/5, Color.RED);
- this.getChildren().add(object);
- }
- //If at n, n add goal
- else if(x == mazeSize - 1 && y == mazeSize - 1)
- {
- object = new Circle(size/5, Color.GOLD);
- this.getChildren().add(object);
- }
- rect.setOnMouseClicked(this);
- }
- /**
- * Changes Cell color. If cell is White, go Black. Vice versa.
- * WHITE represents open space. BLACK represents wall
- */
- private void setCellType()
- {
- if(rect.getFill().equals(Color.WHITE))
- rect.setFill(Color.BLACK);
- else
- rect.setFill(Color.WHITE);
- }
- /**
- * Removes mouse click in Cell
- */
- public void removeMouseClick()
- {
- rect.setOnMouseClicked(null);
- }
- /**
- * Returns manhattan distance for heuristic
- * @return integer manhattan distance
- */
- public int manhattanDistance()
- {
- return Math.abs(x - (size - 1)) + Math.abs(y - (size - 1));
- }
- @Override
- public void handle(MouseEvent mouseEvent)
- {
- setCellType();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement