SHARE
TWEET

Untitled

a guest Oct 21st, 2019 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package sample;
  2.  
  3. import javafx.event.Event;
  4. import javafx.event.EventHandler;
  5. import javafx.geometry.Pos;
  6. import javafx.scene.input.MouseEvent;
  7. import javafx.scene.shape.*;
  8. import javafx.scene.layout.StackPane;
  9. import javafx.scene.paint.Color;
  10.  
  11. import java.awt.event.MouseListener;
  12.  
  13. public class Cell extends StackPane implements EventHandler<MouseEvent> {
  14.  
  15.     //row, col in grid
  16.     private int x, y;
  17.     private int size;
  18.  
  19.     //Shape objects
  20.     private Rectangle rect;
  21.     private Circle object;
  22.  
  23.     /**
  24.      * Creates cell object given x, y and size
  25.      * @param x horizontal coordinate of Cell in the grid
  26.      * @param y vertical coordinate of Cell in the grid
  27.      * @param mazeSize  Size of Maze
  28.      */
  29.     public Cell(int x, int y, int mazeSize)
  30.     {
  31.         //Set maze size
  32.         size = mazeSize;
  33.  
  34.         //Individual cell sizes. Based on maze size
  35.         double size = 800.00/mazeSize;
  36.  
  37.         //Initialize Cell Sizes
  38.         this.setPrefSize(size, size);
  39.         this.setStyle("-fx-background-color: black;");
  40.         this.setAlignment(Pos.CENTER);
  41.  
  42.         //Set Cell position
  43.         this.x = x;
  44.         this.y = y;
  45.  
  46.         //Draw white rectangle and add button click
  47.         rect = new Rectangle(size - 2, size - 2, Color.WHITE);
  48.         this.getChildren().add(rect);
  49.  
  50.         ///If at starting Cell add player
  51.         if(x == 0 && y == 0)
  52.         {
  53.             object = new Circle(size/5, Color.RED);
  54.             this.getChildren().add(object);
  55.         }
  56.  
  57.         //If at n, n add goal
  58.         else if(x == mazeSize - 1 && y == mazeSize - 1)
  59.         {
  60.             object = new Circle(size/5, Color.GOLD);
  61.             this.getChildren().add(object);
  62.         }
  63.         rect.setOnMouseClicked(this);
  64.     }
  65.  
  66.     /**
  67.      * Changes Cell color. If cell is White, go Black. Vice versa.
  68.      * WHITE represents open space. BLACK represents wall
  69.      */
  70.     private void setCellType()
  71.     {
  72.         if(rect.getFill().equals(Color.WHITE))
  73.             rect.setFill(Color.BLACK);
  74.         else
  75.             rect.setFill(Color.WHITE);
  76.     }
  77.  
  78.     /**
  79.      * Removes mouse click in Cell
  80.      */
  81.     public void removeMouseClick()
  82.     {
  83.         rect.setOnMouseClicked(null);
  84.     }
  85.  
  86.     /**
  87.      * Returns manhattan distance for heuristic
  88.      * @return  integer manhattan distance
  89.      */
  90.     public int manhattanDistance()
  91.     {
  92.         return Math.abs(x - (size - 1)) + Math.abs(y - (size - 1));
  93.     }
  94.  
  95.     @Override
  96.     public void handle(MouseEvent mouseEvent)
  97.     {
  98.         setCellType();
  99.     }
  100. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top