public void startScan(String mode) {
   pathfindStart = System.currentTimeMillis();
   if (!gridSet) {
     grid.clearScan();
     tracing = false;
     if (grid.scanStart(mode)) {
       gridSet = true;
     }
   }
 }
  @Override
  protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D gfx = (Graphics2D) g;
    gfx.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    // Clear screen
    gfx.setColor(Constants.BACKGROUND_COLOR);
    gfx.fillRect(0, 0, getWidth(), getHeight());
    // Render next frame
    grid.draw(gfx);
    // Trace path line
    if (tracing) {
      gfx.setColor(Constants.PATH_COLOR);
      gfx.setStroke(new BasicStroke(2));
      for (int i = 1; i < pathLine.size(); i++) {
        Coordinate p = pathLine.get(i - 1);
        Coordinate n = pathLine.get(i);
        gfx.drawLine(
            (Constants.TILESIZE + Constants.MARGIN) * p.x
                + (Constants.TILESIZE / 2)
                + Constants.MARGIN,
            (Constants.TILESIZE + Constants.MARGIN) * p.y
                + (Constants.TILESIZE / 2)
                + Constants.MARGIN,
            (Constants.TILESIZE + Constants.MARGIN) * n.x
                + (Constants.TILESIZE / 2)
                + Constants.MARGIN,
            (Constants.TILESIZE + Constants.MARGIN) * n.y
                + (Constants.TILESIZE / 2)
                + Constants.MARGIN);
      }
    }
  }
  public void run() {
    long start, end, sleepTime;

    while (running) {
      start = System.currentTimeMillis();
      if (gridSet) {
        grid.scanStep();
      }
      repaint();
      end = System.currentTimeMillis();
      // Sleep to match FPS limit
      sleepTime = (1000 / framerate) - (end - start);
      if (sleepTime > 0) {
        try {
          Thread.sleep(sleepTime);
        } catch (InterruptedException e) {
          thread.interrupt();
        }
      }
    }
  }
 public void clear() {
   if (!gridSet) {
     grid.clearAll();
     tracing = false;
   }
 }