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

  • Home
  • SEARCH
  • 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 6793125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:03:48+00:00 2026-05-26T18:03:48+00:00

I am still a beginner in java and I am attempting to create a

  • 0

I am still a beginner in java and I am attempting to create a game. I just created a 20 x 20 board that I added to a 2D array of squares. But, I am confused on some syntax….I still have a hard time with writing proper conditionals and algorithms, especially.

So, what I want to do is fill in 8X8 inside squares with black, and then the outside bordering 2 columns and rows on either side a different color red.

How would I go about factoring this out?

So far, I have two for loops for the rows and columns.

I know I need some sort of if statement I am guessing….like if ([row] == 1, 2, 19, 20 (topmost and bottommost rows)…and likewise for columns

_gameSquares = new Square[GameConstants._numCol][GameConstants._numRow]; 
    for (int col=0; col<GameConstants.numCol; col++) {
        for (int row=0; row<GameConstants.numRow; row++) {
            Square square;
        //  if (row == 1) {
        //  square = new Square(this, java.awt.Color.RED);
        //  }
        //  else
            square = new Square(this, java.awt.Color.BLACK);
            tile.setLocation(col*GameConstants.squareWidth,row*GameConstants.squareHeight);
        _gameSquares[col][row] = square;

        }
    }

        public void paintComponent(java.awt.Graphics g) {
    super.paintComponent(g); 
    java.awt.Graphics2D brush = (java.awt.Graphics2D) g;
    for (int col=0; col<GameConstants.numCol; col++) {
        for (int row=0; row<GameConstants.numRow; row++) {
            _gameSquares[col][row].paint(brush);
        }
        }
  }
  • 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-26T18:03:49+00:00Added an answer on May 26, 2026 at 6:03 pm

    So you want to have the logic depending on which row or column? Lets try this in a structured way.

    Remember that arrays are 0-indexed, so if you have an array with 20 slots, then they are numbered 0,1,2…18,19.

    In this case you want a different color if you are on on of the first two rows or on one of the last two rows or on on of the first two columns or on one of the last two columns (long sentence).

    Color color = Color.BLACK; // Default color
    if ( (row >= 0 && row <= 1) || (row >= 18 && row <= 19)  (col >= 0 && col <= 1) || (col >= 18 && col <= 19)) {
        color = Color.RED;
    }
    square = new Square(...
    

    But to maintain this you should probably break the logic into a separate method to get more readable code, this is an example:

    Color color = Color.BLACK; // Default color
    if ( isBorder(col, row)) {
        color = Color.RED;
    }
    ...
    
    private boolean isBorder(int col, int row) {
      return (row >= 0 && row <= 1) || (row >= 18 && row <= 19)  (col >= 0 && col <= 1) || (col >= 18 && col <= 19);
    }
    

    Of course this could be further cleaned up, but it is a start and now it is easier to read the creation logic of your board.

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

Sidebar

Related Questions

We are instructed to create a Java multiplayer game, and yet I still have
Still new to Objective C, and I'm having some trouble that I just can't
I'm still a beginner in java. I dont have any idea on how to
This is a real beginner question (I'm still learning the Java basics). I can
I'm very much still a beginner at programming but I have come across the
Hi there I would like to start by saying that i'm a beginner, but
I'm a beginner learning WPF, and need to create an Application Bar that launches
I am still a beginner but I want to write a character-recognition-program. This program
I am a beginner in programming with Java Android. I am currently confused with
Still struggling to understand what best practices are with respect to macros. I'm attempting

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.