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 1098943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:39:26+00:00 2026-05-17T00:39:26+00:00

I’m doing another Java project for my class. In this assignment, we have to

  • 0

I’m doing another Java project for my class. In this assignment, we have to create a checkerboard and populate it with the appropriate number of checkers. I built the checkerboard correctly which displays nicely, but I’m having a hard time using the Graphics class to draw.

Here’s the code that I have so far:

import javax.swing.*;
import java.awt.*;

public class Checkerboard extends JFrame {
    //Define the default values for the separate checker pieces
    private final int RED_PIECE = 0;
    private final int BLACK_PIECE = 1;

    /** Construct the default checker board */
    public Checkerboard() {
        this.setSize(600, 600);
        this.setResizable(false);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setTitle("Checkerboard Lab");
        this.setLayout(new GridLayout(8, 8));
        this.setVisible(true);

        for (int a=0; a<2; a++) {
            for (int i=0; i<4; i++) {
                add(new WhiteSpace());
                add(new GraySpace(RED_PIECE));
            }
            for (int j=0; j<4; j++) {
                add(new GraySpace(RED_PIECE));
                add(new WhiteSpace());
            }
        }
        for (int b=0; b<2; b++) {
            for (int k=0; k<4; k++) {
                add(new WhiteSpace());
                add(new GraySpace(RED_PIECE));
            }
            for (int l=0; l<4; l++) {
                add(new GraySpace());
                add(new WhiteSpace());
            }
        }
        for (int c=0; c<2; c++) {
            for (int m=0; m<4; m++) {
                add(new GraySpace());
                add(new WhiteSpace());
            }
            for (int n=0; n<4; n++) {
                add(new GraySpace(BLACK_PIECE));
                add(new WhiteSpace());
            }
        }
        for (int d=0; d<2; d++) {
            for (int o=0; o<4; o++) {
                add(new WhiteSpace());
                add(new GraySpace(BLACK_PIECE));
            }
            for (int p=0; p<4; p++) {
                add(new GraySpace(BLACK_PIECE));
                add(new WhiteSpace());
            }
        }
    }

    /** White Space constructor */
    public class WhiteSpace extends JPanel {
        public WhiteSpace() {
            setBackground(Color.WHITE); //Sets the panel's background color to white
        }
    }

    /** Gray Space constructor */
    /* GraySpace is a little different, since this color space is the only space that will be holding checker
     * pieces. There is a default constructor to create a space without a checker piece on it, and another 
     * constructor that places either a red or black piece on the space, pending an optional parameter.*/
    public class GraySpace extends JPanel {
        //Initial variable for the checker piece
        int checkerPiece;

        //Default GraySpace constructor
        public GraySpace() {
            setBackground(Color.LIGHT_GRAY);
        }

        //The GraySpace constructor with the optional parameter to determine if it holds a checker piece
        public GraySpace(int piece) {
            this.checkerPiece = piece;
            setBackground(Color.LIGHT_GRAY); //Sets the panel's background color to white
        }

        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            //Default width and height variables
            int width = getWidth() -10;
            int height = getHeight() - 10;

            //This switch statement determines which checker piece type appears on the square
            switch (checkerPiece) {
            case RED_PIECE:
                g.setColor(Color.RED);
                g.fillOval(5, 5, width, height);
                break;
            case BLACK_PIECE:
                g.setColor(Color.BLACK);
                g.fillOval(5, 5, width, height);
                break;
            }
        }
    }

    /** Initiate the Checker board */
    public static void main(String[] args) {
        JFrame checkerboard = new Checkerboard();
    }
}

It’s fairly straight forward. I have my Checkerboard class be a subclass of JFrame, which I put colored panels on in an 8×8 square. The panels are inner classes of the Checkerboard class, each extending JPanel (WhiteSpace and GraySpace respectfully). Since GraySpace is the only class that will have to hold a checker, I thought that I’d simply put the Graphics code into the GraySpace inner class.

Anyhow, for my question: How do I go about using Graphics to draw that? I know I have to explicitly declare the paintComponent() method in order to draw the circle, but I don’t have any clue how to specify the dimensions of the GraySpace so it will draw effectively. Any advice?

EDIT: NEW QUESTION!

Alright, so I figured out exactly how I would be adding pieces onto my board, and that works perfectly. My GraySpace inner class has an optional constructor that takes in an int value, and from that determines what color piece will go on the GraySpace panel. I tested that out, and it works.

My problem, however, is actually GETTING the pieces onto the checkerboard. The board must represent a “default” checker game, where all available pieces are on the board. So, three rows of red checkers, three rows of black checkers, with two empty rows separating them. Thus far, I have 4 separate for loops to draw two rows at a time on the board… But it’s not working properly. Any advice? The latest source code is above, replacing my old questions source code. Again, thank you for any advice!

  • 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-17T00:39:27+00:00Added an answer on May 17, 2026 at 12:39 am

    Call getHeight and getWidth on the component you are in. Since paintComponent is a member of your JPanel class, you can just call getHeight and getWidth directly.

    I made a couple of other changes to your method.

    1. Don’t call this.paintComponent, call the base class (super)
    2. You need to set the color before you draw.
    3. I bet fillOval is what you want.

    For example:

    protected void paintComponent(Graphics g) { 
        int h = getHeight();
        int w = getWidth();
        super.paintComponent(g); 
        g.setColor(CHECKER_COLOR);
        g.fillOval(w/2, h/2, w, h); 
    } 
    

    For extra credit, turn on antialiasing and make your checkers look great!

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am doing a simple coin flipping experiment for class that involves flipping a
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have thousands of HTML files to process using Groovy/Java and I need to
I have some data like this: 1 2 3 4 5 9 2 6
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.