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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:37:47+00:00 2026-05-30T19:37:47+00:00

I’m begining a little project to create a simple checkers game. However it’s been

  • 0

I’m begining a little project to create a simple checkers game. However it’s been a long time since I’ve used the java GUI tools. The goal of the code at this point is to draw the initial board (red pieces at top, black at bottom). However all I get when I run the code is a blank frame. I’m also a little uncertain if my circle drawing code will do what I want (ie create solid red or black circles inside certain squares) Here is the code. Thanks in advance for any help/suggestions

EDIT: I should probably alternate drawing blue and gray squares or else the thing will probably just be a giant blue blob, however I’ll settle for a giant blue blob at this point :p

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

public class CheckersServer 
{
    public static class Board
    {
        private JFrame frame = new JFrame();
        private JPanel backBoard = new JPanel();

    Board()
    {

        frame.setSize(905,905);
        backBoard.setSize(900,900);
        frame.setTitle("Checkers");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        backBoard.setVisible(true);


        boardSquare bs;
        String type = null;
        //Filling in Red Side
        for (int i = 0; i <=1; i++)
        {
            for(int j = 0; j < 9; j++)
            {
                if(j % 2 == 0)
                {
                    type = "Red";
                }
                else 
                {
                    type = "Blank";
                }

                bs = new boardSquare(100*j,100*i,type);
                backBoard.add(bs); 

            }

        }
        //Filling in empty middle
        type = "Blank";
        for (int i = 2; i < 7; i++)
        {
            for(int j = 0; j < 9; j++)
            {

                bs = new boardSquare(100*j,100*i,type);
                backBoard.add(bs); 

            }

        }

        //Filling in Black side
        for (int i = 7; i < 9; i++)
        {
            for(int j = 0; j < 9; j++)
            {
                if(j % 2 != 0)
                {
                    type = "Black";
                }
                else 
                {
                    type = "Blank";
                }

                bs = new boardSquare(100*j,100*i,type);
                backBoard.add(bs); 

            }

        }

        backBoard.repaint();
        frame.add(backBoard);
        frame.repaint();


    }

    private class boardSquare extends JComponent
    {
        private int x; //x position of the rectangle measured from top left corner
        private int y; //y position of the rectangle measured from top left corner

        private boolean isBlack = false;
        private boolean isRed = false;

        public boardSquare(int p, int q, String type)
        {
            x = p;
            y = q;
            if (type.equals("Black"))
            {
                isBlack = true;
                isRed = false;
            }
            else if (type.equals("Red"))
            {
                isRed = true;
                isBlack = false;
            }
            else if (type.equals("Blank"))
            {
                isBlack = false;
                isRed = false;
            }

        }
        public void paintComponent(Graphics g)
        {
            Graphics2D g2 = (Graphics2D) g;
            Rectangle box = new Rectangle(x,y,100,100);
            g2.draw(box);
            g2.setPaint(Color.BLUE);
            g2.fill(box);

            if(isBlack)
            {
                g2.fillOval(x, y,100 ,100 );
                g2.setColor(Color.black);
                g2.drawOval(x, y, 100, 100);
            }

            else if(isRed)
            {
                g2.fillOval(x, y,100 ,100 );
                g2.setColor(Color.red);
                g2.drawOval(x, y, 100, 100);
            }

        }
    }


}


public static void main(String[] args)
{
    Board game = new Board();

}
}
  • 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-30T19:37:49+00:00Added an answer on May 30, 2026 at 7:37 pm

    You have several issues.

    Java UI is layout-based, which means that when you add a component to a parent, the parent’s layout determines where the child component will be placed. You don’t have any code to set up the layout, and so your application is using the defaults (FlowLayout is the default, and this may work in your case, as long as your JFrame and children are the appropriate size).

    The bigger problems are in your boardSquare class. By default, JPanels have a dimension of 10×10. You aren’t specifying the size, and so all your squares are 10×10. You need to tell the squares how big they are. You can do this in the boardSquare constructor:

    setPreferredSize(new Dimension(100, 100));
    

    Finally, in your drawing code, you are doing an offset of x,y when drawing the squares and circles. This is an offset from the top-left corner of the component. Your components (after setting the size) will be 100×100 pixels. But if your x,y are greater than these values, you will be drawing outside of the bounds of the component. Instead, these values should be set to 0,0 because that is the top-left corner of the component you are drawing in.

    By just setting the preferred size of the squares and setting x,y to 0, I was able to get the squares drawing in the frame, though it wasn’t pretty. You will need to work on setting the correct layout before it will be laid out correctly.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I'm trying to create an if statement in PHP that prevents a single post

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.