Select Git revision
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
FirefighterGrid.java 3.30 KiB
package view;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.util.Pair;
import model.ModelElement;
import model.FirefighterBoard;
import util.Position;
public class FirefighterGrid extends Canvas {
private int columns;
private int rows;
private int boxWidth;
private int boxHeight;
private FirefighterBoard board;
// Initialize the grid dimensions and board reference
public void initialize(int columns, int rows, int boxWidth, int boxHeight, FirefighterBoard board) {
this.columns = columns;
this.rows = rows;
this.boxWidth = boxWidth;
this.boxHeight = boxHeight;
this.board = board;
setWidth(columns * boxWidth); // Set canvas width based on grid size
setHeight(rows * boxHeight); // Set canvas height based on grid size
}
// Repaint the grid and all elements (fire and firefighter)
public void repaint() {
if (board == null) return; // Exit if board is not set
GraphicsContext gc = getGraphicsContext2D();
gc.clearRect(0, 0, getWidth(), getHeight()); // Clear the canvas before drawing
// Iterate over the list of updated elements (positions and elements like fire or firefighter)
for (Pair<Position, ModelElement> pair : board.getUpdatedElements()) {
Position position = pair.getKey(); // Get the Position from the Pair
ModelElement element = pair.getValue(); // Get the ModelElement (either FIRE or FIREFIGHTER)
// Set the color based on the element type
if (element == ModelElement.FIRE) {
gc.setFill(Color.RED); // Fire is red
} else if (element == ModelElement.FIREFIGHTER) {
gc.setFill(Color.BLUE); // Firefighter is blue
} else if (element == ModelElement.ROAD) {
gc.setFill(Color.WHITE);
} else if (element == ModelElement.CLOUD) {
gc.setFill(Color.YELLOW);
} else if (element == ModelElement.MOUTAIN) {
gc.setFill(Color.GREEN);
} else if (element == ModelElement.ROCKS) {
gc.setFill(Color.LIGHTSKYBLUE);
} else {
gc.setFill(Color.WHITE); // Empty space is white
}
// Draw the element on the grid at the appropriate position
gc.fillRect(position.getCol() * boxWidth, position.getRow() * boxHeight, boxWidth, boxHeight);
gc.setStroke(Color.LIGHTGRAY); // Grid border color
gc.strokeRect(position.getCol() * boxWidth, position.getRow() * boxHeight, boxWidth, boxHeight);
}
// Optionally, draw the grid lines on top of the elements
drawGridLines(gc);
}
// Helper method to draw the grid lines
private void drawGridLines(GraphicsContext gc) {
gc.setStroke(Color.GRAY);
for (int col = 0; col < columns; col++) {
gc.strokeLine(col * boxWidth, 0, col * boxWidth, getHeight()); // Vertical lines
}
for (int row = 0; row < rows; row++) {
gc.strokeLine(0, row * boxHeight, getWidth(), row * boxHeight); // Horizontal lines
}
}
public int getColumns() {
return columns;
}
public int getRows() {
return rows;
}
}