@Override
 public Dimension getPreferredSize() {
   preferredScrollableSize = new Dimension();
   preferredScrollableSize.setSize(
       TileSet.TILE_DIMENSION * tileSet.getWidth(), TileSet.TILE_DIMENSION * tileSet.getHeight());
   return preferredScrollableSize;
 }
  @Override
  public String toString() {
    String output = "";

    for (int y = 0; y < tileSet.getHeight(); y++) {
      for (int x = 0; x < tileSet.getWidth(); x++) output += passabilitySet[x][y];
      output += '\n';
    }
    return output;
  }
 /** Set tile set to be used */
 public void refreshTileSet() {
   tileSet = parent.activeTileSet;
   x = 0;
   y = 0;
   passabilitySet = tileSet.getPassabilitySet();
   dbImage = null;
   repaint();
 }
  /**
   * Update a single tile
   *
   * @param x
   * @param y
   */
  public void paintTile(int x, int y) {
    Graphics g = dbImage.getGraphics();

    tileSet.drawEditorTile(g, x * TileSet.TILE_DIMENSION, y * TileSet.TILE_DIMENSION, x, y);

    g.setColor(Color.BLACK);
    String p = "" + passabilitySet[x][y];

    int xpos;
    int ypos;

    if (pTiles != null) {
      xpos = x * TileSet.TILE_DIMENSION;
      ypos = y * TileSet.TILE_DIMENSION;
      if (passabilitySet[x][y] == TileSet.OVERLAY)
        g.drawImage(
            pTiles,
            xpos,
            ypos,
            xpos + TileSet.TILE_DIMENSION,
            ypos + TileSet.TILE_DIMENSION,
            0,
            0,
            32,
            32,
            null);
      else if (passabilitySet[x][y] == TileSet.IMPASSABLE)
        g.drawImage(
            pTiles,
            xpos,
            ypos,
            xpos + TileSet.TILE_DIMENSION,
            ypos + TileSet.TILE_DIMENSION,
            32,
            0,
            64,
            32,
            null);
    } else {
      xpos = x * TileSet.TILE_DIMENSION + (TileSet.TILE_DIMENSION / 2);
      ypos = y * TileSet.TILE_DIMENSION + (TileSet.TILE_DIMENSION / 2);
      for (int i = 0; i < 9; i++)
        g.drawString(p, xpos - 1 * ((i % 3) - 1), ypos - 1 * ((i / 3) - 1));
      g.setColor(Color.WHITE);
      g.drawString(p, xpos, ypos);
    }
    repaint();
  }
  public PassabilityGrid(PassabilityEditor p) {
    parent = p;

    tileSet = parent.activeTileSet;
    x = 0;
    y = 0;
    passabilitySet = tileSet.getPassabilitySet();
    try {
      pTiles = ImageIO.read(new File("data/passabilityTiles.png"));
    } catch (IOException e) {
      e.printStackTrace();
    }
    dbImage = null;
    setVisible(true);
    addMouseListener(this);
    addMouseMotionListener(this);
  }
  /** Draws the actual grid and tiles */
  @Override
  public void paint(Graphics g) {
    if (g == null) return;

    if (dbImage == null) {
      dbImage = createImage(getWidth(), getHeight());

      Graphics g2 = dbImage.getGraphics();
      g2.setColor(Color.GRAY);
      g2.fillRect(0, 0, dbImage.getWidth(null), dbImage.getHeight(null));
      for (int x = 0; x < tileSet.getWidth(); x++)
        for (int y = 0; y < tileSet.getHeight(); y++) {
          paintTile(x, y);
        }
    }

    g.drawImage(dbImage, 0, 0, null);
    g.setColor(Color.BLACK);
    for (int i = 1; i < tileSet.getWidth(); i++)
      g.drawLine(
          i * TileSet.TILE_DIMENSION,
          0,
          i * TileSet.TILE_DIMENSION,
          (int) tileSet.getHeight() * TileSet.TILE_DIMENSION);

    for (int i = 1; i < tileSet.getHeight(); i++)
      g.drawLine(
          0,
          i * TileSet.TILE_DIMENSION,
          (int) tileSet.getWidth() * TileSet.TILE_DIMENSION,
          i * TileSet.TILE_DIMENSION);

    g.setColor(Color.RED);
    g.drawRect(
        x * TileSet.TILE_DIMENSION,
        y * TileSet.TILE_DIMENSION,
        TileSet.TILE_DIMENSION,
        TileSet.TILE_DIMENSION);
  }