开发者

Java - why is mask not working?

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 for GuessingGame.java

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.a开发者_如何学GoddActionListener(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 < boxes.length; row ++) {
      for (int col = 0; col < 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 < boxes.length; row++) {
      for (int col = 0; col < boxes[row].length; col++) {
        boxes[row][col].draw(g);

      }
    }
  }


  public void gameLogic(MaskableBox box) {
    if ((chosenBoxes[0] != null)&&(chosenBoxes[1] != null)) {
      chosenBoxes = new MaskableBox[2];
      if(chosenBoxes[0].getBackColor() == chosenBoxes[1].getBackColor()) {  
        for (int i=0; 0 < 2; ++i ) {
          for(int row = 0; row < boxes.length; row++) {         
            for(int col = 0; col < 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);
      } 
    }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 < boxes.length; row ++) {
        for(int col = 0; col < 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 < boxes.length; row++) {
      for(int col = 0; col < 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 < boxes.length; row++) {
      for (int col = 0; col < boxes[row].length; col++) {
        for (;;) {
          int rnd = (int) (Math.random() * 8);
          if (chosenColors[rnd]< 2) {
            chosenColors[rnd]++;
            boxColors[row][col] = availableColors[rnd];
            break;
          }
        }
      }
    }
  }
}

here is the second batch of code containing maskablebox

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;
  }
}

I now get these error messages.

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)


You are getting a NullPointerException on line 74 of GuessingGame.java in method gameLogic, which corresponds to this line :

 if(chosenBoxes[0].getBackColor() == chosenBoxes[1].getBackColor()) { 

and the reason you're getting a NullPointerException there is because in the line before it you do:

chosenBoxes = new MaskableBox[2];

which initailizes an array of MaskableBox with 2 references, but the actual MaskableBox objects neeed to be instantiated as well - does that make sense?

Basically the lesson is this: Creating an array of Objects is not the same as creating an array of objects and initializing each one of the elements in the array.


Because at line 73 you do:

chosenBoxes = new MaskableBox[2];

i.e. you create a new MaskableBox array (each element in the new array will be null). The next line you do is:

chosenBoxes[0].getBackColor() == chosenBoxes[1].getBackColor()

chosenBoxes[0] and chosenBoxes[1] are both null since you just created chosenBoxes on the previous line. Remove the call to create a new MaskableBox array and you won't get the NullPointerException.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜