java boxes mask not working
<p>The idea here is to create a grid of boxes. underneath the black grid is another grid of multi-colored boxes. when you click a box it's mask disappears showing the colored box beneath. You then click a second box if the colors match hurray, if not then the game continues. Here is the code.</p>

<pre><code>import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GuessingGame extends Applet{
  /**
   * 
   */
  private static final long serialVersionUID = 1L;
  private final int START_X = 20;
  private final int START_Y = 40;
  private final int ROWS = 4;
  private final int COLS = 4;
  private final int BOX_WIDTH = 20;
  private final int BOX_HEIGHT = 20;
  //this is used to keep track of boxes that have been matched.
  private boolean matchedBoxes[][];
  //this is used to keep track of two boxes that have been clicked.
  private MaskableBox chosenBoxes[];
  private MaskableBox boxes[][];
  private Color boxColors[][];
  private Button resetButton;


  public void init() {
    boxes = new MaskableBox[ROWS][COLS];
    boxColors = new Color[ROWS][COLS];
    resetButton = new Button("Reset Colors");
     resetButton.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e) {
           randomizeColors();
           buildBoxes();
           repaint();
       }
     });
     add(resetButton);
     //separate building colors so we can add a button later
     //to re-randomize them.
    randomizeColors();
    buildBoxes();
  }

  public void paint(Graphics g) {
    for (int row =0; row &lt; boxes.length; row ++) {
      for (int col = 0; col &lt; boxes[row].length; col++) {
        if(boxes[row][col].isClicked()) {
          //boxes[row][col].setMaskColor(Color.black);
          //boxes[row][col].setMask(!boxes[row][col].isMask());
          //boxes[row][col].setClicked(false);
        //}
          if (!matchedBoxes[row][col]) {
            gameLogic(boxes[row][col]);
            //boxes[row][col].draw(g);
          }
        }
      }
     }
  //loop through the boxes and draw them.
    for (int row = 0; row &lt; boxes.length; row++) {
      for (int col = 0; col &lt; boxes[row].length; col++) {
        boxes[row][col].draw(g);

      }
    }
  }


  public void gameLogic(MaskableBox box) {
    if ((chosenBoxes[0] != null)&amp;&amp;(chosenBoxes[1] != null)) {
      if(chosenBoxes[0].getBackColor() == chosenBoxes[1].getBackColor()) {  
        for (int i=0; 0 &lt; chosenBoxes.length; i++ ) {
          for(int row = 0; row &lt; boxes.length; row++) {         
            for(int col = 0; col &lt; boxes[row].length; col++) {          
              if( boxes[row][col] == chosenBoxes[i] ) {
                System.out.println("boxes [row][col] == chosenBoxes[] at index: " + i  );             
                matchedBoxes[row][col] = true;
                break;
              }
            }
          }
        }
      }else {  
        chosenBoxes[0].setMask(true);
        chosenBoxes[1].setMask(true);
      } 
        chosenBoxes = new MaskableBox[2];

    }else {
      if (chosenBoxes[0] == null) { 
          chosenBoxes[0] = box;
          chosenBoxes[0].setMask(false);
          return;
      }else{     
        if (chosenBoxes[1] == null) {
          chosenBoxes[1] = box;
          chosenBoxes[1].setMask(false);       
        }   
      }
    }
  }


  private void removeMouseListeners() {
    for(int row = 0; row &lt; boxes.length; row ++) {
        for(int col = 0; col &lt; boxes[row].length; col++) {
            removeMouseListener(boxes[row][col]);
        }
    }
  }

  private void buildBoxes() {
    // need to clear any chosen boxes when building new array.
    chosenBoxes = new MaskableBox[2];
    // create a new matchedBoxes array
    matchedBoxes = new boolean [ROWS][COLS];
    removeMouseListeners();
    for(int row = 0; row &lt; boxes.length; row++) {
      for(int col = 0; col &lt; boxes[row].length; col++) {
        boxes[row][col] = 
          new MaskableBox(START_X + col * BOX_WIDTH,
                            START_Y + row * BOX_HEIGHT,
                            BOX_WIDTH,
                            BOX_HEIGHT,
                            Color.gray,
                            boxColors[row][col],
                            true,
                            true,
                            this);
        addMouseListener(boxes[row][col]);
      }
    }
  }





  private void randomizeColors() {
    int[] chosenColors = {0,0,0,0,0,0,0,0};
    Color[] availableColors = {Color.red, Color.blue, Color.green,
        Color.yellow, Color.cyan, Color.magenta, Color.pink, Color.orange };
    for(int row = 0; row &lt; boxes.length; row++) {
      for (int col = 0; col &lt; boxes[row].length; col++) {
        for (;;) {
          int rnd = (int) (Math.random() * 8);
          if (chosenColors[rnd]&lt; 2) {
            chosenColors[rnd]++;
            boxColors[row][col] = availableColors[rnd];
            break;
          }
        }
      }
    }
  }
}
</code></pre>

<p>here is the second batch of code containing maskablebox</p>

<pre><code>import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;

public class MaskableBox extends ClickableBox {
  private boolean mask;
  private Color maskColor;
  Container parent;

  public MaskableBox(int x, int y, int width, int height, Color borderColor,
      Color backColor, boolean drawBorder, boolean mask, Container parent ) {
    super(x, y, width, height, borderColor, backColor, drawBorder, parent);
    this.parent = parent;
    this.mask = mask;
  }

  public void draw(Graphics g) {
    if(mask=false) {
      super.draw(g);
//      setOldColor(g.getColor());
//      g.setColor(maskColor);
//      g.fillRect(getX(),getY(),getWidth(), getHeight());
//      if(isDrawBorder()) {
//        g.setColor(getBorderColor());
//        g.drawRect(getX(),getY(),getWidth(),getHeight());
//      }
//      g.setColor(getOldColor());
    }else {
      if(mask=true) {
        //super.draw(g);
        setOldColor(g.getColor());
        g.setColor(maskColor);
        g.fillRect(getX(),getY(),getWidth(), getHeight());
        if(isDrawBorder()) {
          g.setColor(getBorderColor());
          g.drawRect(getX(),getY(),getWidth(),getHeight());
        }
        g.setColor(getOldColor());
      }
    }
  }


  public boolean isMask() {
    return mask;
  }

  public void setMask(boolean mask) {
    this.mask = mask;
  }

  public Color getMaskColor() {
    return maskColor;
  }

  public void setMaskColor(Color maskColor) {
    this.maskColor = maskColor;
  }
}
</code></pre>

<p>I now get these error messages. </p>

<pre><code>Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
    at GuessingGame.gameLogic(GuessingGame.java:74)
    at GuessingGame.paint(GuessingGame.java:55)
    at java.awt.Container.update(Container.java:1801)
    at sun.awt.RepaintArea.updateComponent(RepaintArea.java:239)
    at sun.awt.RepaintArea.paint(RepaintArea.java:216)
    at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:306)
    at java.awt.Component.dispatchEventImpl(Component.java:4706)
    at java.awt.Container.dispatchEventImpl(Container.java:2099)
    at java.awt.Component.dispatchEvent(Component.java:4460)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
</code></pre>
