Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 882739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:28:22+00:00 2026-05-15T12:28:22+00:00

The idea here is to create a grid of boxes. underneath the black grid

  • 0

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.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 < 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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T12:28:23+00:00Added an answer on May 15, 2026 at 12:28 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The idea here is to create a grid of boxes. underneath the black grid
Here's an idea. I'm trying to create file from PHP script. File may be
Here's the basic idea: There is a java window (main) that opens another java
The idea here is to get better programmers right out of college. I think
My Question is similar to the idea here: Replacing a component class in delphi
I'm just looking a big picture idea here; I can google specifics once provided
This is a weird one, but hopefully someone can give me an idea here.
Here's the idea, I'd like to make a service? that will look for a
Here's the idea: you commit your code to a repository and call a web
Here is the idea: When the user wants to see /controller/action, then I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.