Susie
Susie

Reputation: 555

How Would I Fill In The Center Of A 2D Array With A Different Color Than The Border?

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, then

square = newSquare(jpanel, Color.RED);

else

square = newSquare(jpanel, Color.Black)

Upvotes: 0

Views: 793

Answers (2)

StanislavL
StanislavL

Reputation: 57381

You can have just one square. First fill() it with inner color. Then set stroke to you Graphics (use BasicStroke and specify width=5 (or more pixels) and use draw().

Upvotes: 1

Samuel Liew
Samuel Liew

Reputation: 79022

If your paintComponent method allows drawing of unfilled squares, first draw a filled square and then another unfilled square (same size and position) with a different colour over it.

Upvotes: 0

Related Questions