package boundary;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import utils.ScaleImage;
import model.Board;
import model.PlayerModel;
import model.Cell;
import model.Rules;

/**
 * Preview view for the builder board view.
 *
 * @author Eli Skeggs and Maurizio Vitale
 */
public class PlayerPreviewBoardView extends BoardView {
  PlayerApplication app;
  PlayerModel model;

  BufferedImage[] numberCache = new BufferedImage[Rules.maxNumber];
  BufferedImage playableCache = null, numberFallbackCache = null;

  public PlayerPreviewBoardView(PlayerApplication app, PlayerModel model) {
    super(false, Board.width, Board.height);

    this.app = app;
    this.model = model;
  }

  /**
   * Cache the images.
   *
   * @param width The new cell width to cache.
   * @param height The new cell height to cache.
   */
  @Override
  protected void cellSizeChange(int width, int height) {
    numberFallbackCache = solidImage(Color.MAGENTA, width, height);

    for (int i = 0; i < numberCache.length; i++) {
      // scaleImage ensures we aren't resizing unnecessarily
      BufferedImage image = app.loader.getResource((i + 1) + ".png");
      if (image == null) {
        System.err.println("[WARN] Bad number for lookup: " + (i + 1));
        image = numberFallbackCache;
      } else {
        image = ScaleImage.scaleImage(image, width, height);
      }
      numberCache[i] = image;
    }

    BufferedImage playable = app.loader.getResource("playable.png");
    if (playable == null) {
      System.err.println("[WARN] Cannot get playable image");
      playableCache = solidImage(Color.WHITE, width, height);
    } else {
      playableCache = ScaleImage.scaleImage(playable, width, height);
    }
  }

  @Override
  protected void paintCell(Graphics g, int x, int y,
      int x1, int y1, int x2, int y2) {
    Cell cell = model.level.initialBoard.grid[x][y];

    switch (cell.type) {
    case PLAYABLE:
      // TODO: add multiplier
      if (cell.tile == null) {
        g.drawImage(playableCache, x1, y1, null);
      } else {
        int number = cell.tile.number;
        System.err.println("[WARN] Bad number for cache lookup: " + number);
        BufferedImage image = numberCache[number];
        if (image == null) {
          image = numberFallbackCache;
        }
        g.drawImage(image, x1, y1, null);
      }
      break;

    case BUCKET:
      g.setColor(Color.GRAY);
      g.fillRect(x1, y1, x2 - x1, y2 - y1);
      break;

    // TODO: should probably warn in case of default
    case INERT:
    default:
      g.setColor(Color.BLACK);
      g.fillRect(x1, y1, x2 - x1, y2 - y1);
    }
  }
}

@@ -715,286 +715,167 @@
 el;%0A
-  %7D%0A%0A  /**%0A   * Cache the images.%0A   *%0A   * @param width The new cell width to cache.%0A   * @param height The new cell height to cache.%0A   */%0A  @Override%0A  protected void cellSizeChange(int width, int height) %7B%0A    numberFallbackCache = solidImage(Color.MAGENTA, width, height);%0A
+%0A    setCacheSource(app.loader);%0A%0A    Color%5B%5D fallbackColors = new Color%5B%5D %7BColor.GREEN, Color.MAGENTA,%0A      Color.YELLOW, Color.RED, Color.BLUE, Color.GRAY%7D;
 %0A   
@@ -895,27 +895,30 @@
  0; i %3C 
-numberCache
+fallbackColors
 .length;
@@ -935,1203 +935,575 @@
     
-// scaleImage ensures we aren't resizing unnecessarily%0A      BufferedImage image = app.loader.getResource((i + 1) + %22.png%22);%0A      if (image == null) %7B%0A        System.err.println(%22%5BWARN%5D Bad number for lookup: %22 + (i + 1));%0A        image = numberFallbackCache;%0A      %7D else %7B%0A        image = ScaleImage.scaleImage(image, width, height);%0A      %7D%0A      numberCache%5Bi%5D = image;%0A    %7D%0A%0A    BufferedImage playable = app.loader.getResource(%22playable.png%22);%0A    if (playable == null) %7B%0A      System.err.println(%22%5BWARN%5D Cannot get playable image%22);%0A      playableCache = solidImage(Color.WHITE, width, height);%0A    %7D else %7B%0A      playableCache = ScaleImage.scaleImage(playable, width, height);%0A    %7D%0A  %7D%0A%0A  @Override%0A  protected void paintCell(Graphics g, int x, int y,%0A      int x1, int y1, int x2, int y2) %7B%0A    Cell cell = model.level.initialBoard.grid%5Bx%5D%5By%5D;%0A%0A    switch (cell.type) %7B%0A    case PLAYABLE:%0A      // TODO: add multiplier%0A      if (cell.tile == null) %7B%0A        g.drawImage(playableCache, x1, y1, null);%0A      %7D else %7B%0A        int number = cell.tile.number;%0A        System.err.println(%22%5BWARN%5D Bad number for cache lookup: %22 + number);%0A        BufferedImage image = numberCache%5Bnumber%5D;%0A  
+addCache((i + 1) + %22.png%22, fallbackColors%5Bi%5D);%0A    %7D%0A%0A    addCache(%22playable.png%22, Color.WHITE);%0A    addCache(%22bucket.png%22, Color.GRAY);%0A    addCache(%22inert.png%22, Color.BLACK);%0A    addCache(%22x2.png%22, null);%0A    addCache(%22x3.png%22, null);%0A  %7D%0A%0A  @Override%0A  protected void paintCell(Graphics g, int x, int y,%0A      int x1, int y1, int x2, int y2) %7B%0A    Cell cell = model.level.initialBoard.grid%5Bx%5D%5By%5D;%0A%0A    switch (cell.type) %7B%0A    case PLAYABLE:%0A      g.drawImage(getCache(cell.tile == null ? %22playable.png%22 :%0A        (cell.tile.number + 1) + %22.png%22), x1, y1, null);%0A%0A
     
@@ -1512,99 +1512,122 @@
 if (
-image =
+cell.tile !
 = null
-) %7B%0A          image = numberFallbackCache;%0A        %7D%0A        g.drawImage(image
+ && cell.tile.multiplier %3E 1) %7B%0A        g.drawImage(getCache(%22x%22 + cell.tile.multiplier + %22.png%22)
 , x1
@@ -1690,71 +1690,54 @@
   g.
-setColor(Color.GRAY);%0A      g.fillRect(x1, y1, x2 - x1, y2 - y1
+drawImage(getCache(%22bucket.png%22), x1, y1, null
 );%0A 
@@ -1843,72 +1843,53 @@
   g.
-setColor(Color.BLACK);%0A      g.fillRect(x1, y1, x2 - x1, y2 - y1
+drawImage(getCache(%22inert.png%22), x1, y1, null
 );%0A 

Use BoardView caching in PlayerPreviewBoardView