Example #1
0
  public void paintComponent(Graphics g) {
    super.paintComponent(g);

    drawBorders(this.cellSize, this.gapSize, g);

    List<Cell> solution = maze.getSolution();
    for (List<Cell> row : this.maze.getCells()) {
      for (Cell cell : row) {
        Color color = Color.WHITE;
        if (cell.isVisited()) {
          color = Color.LIGHT_GRAY;
        }
        if (solution != null && solution.contains(cell)) {
          color = Color.YELLOW;
        }
        if (cell.equals(this.maze.getStart())) {
          color = Color.GREEN;
        }
        if (cell.equals(this.maze.getEnd())) {
          color = Color.RED;
        }
        drawCell(cell, this.cellSize, this.gapSize, g, color);
      }
    }

    for (Wall wall : this.maze.getWalls()) {
      drawWall(wall, this.cellSize, this.gapSize, g);
    }
  }
Example #2
0
 private void drawCell(Cell cell, int cellSize, int gapSize, Graphics g, Color color) {
   g.setColor(color);
   int x1 = cell.getX() * (cellSize + gapSize) + gapSize;
   int y1 = cell.getY() * (cellSize + gapSize) + gapSize;
   int width = cellSize;
   int height = cellSize;
   g.fillRect(x1, y1, width, height);
 }
Example #3
0
 private void drawWall(Wall wall, int cellSize, int gapSize, Graphics g) {
   g.setColor(Color.BLACK);
   Cell cell1 = wall.getCell1();
   Cell cell2 = wall.getCell2();
   if (cell1.getX() == cell2.getX()) {
     if (cell1.getY() > cell2.getY()) {
       drawBottomWall(cell1.getX(), cell1.getY(), cellSize, gapSize, g);
     } else {
       drawTopWall(cell1.getX(), cell1.getY(), cellSize, gapSize, g);
     }
   } else {
     if (cell1.getX() < cell2.getX()) {
       drawRightWall(cell1.getX(), cell1.getY(), cellSize, gapSize, g);
     } else {
       drawLeftWall(cell1.getX(), cell1.getY(), cellSize, gapSize, g);
     }
   }
 }