Hey,

Guys and gulls i need help with the following asap.

I'm trying to code a checkers board in java and populate it with checkers and then paint this onto a JPanel. I then add the JPanel to my JFrame.

I am able to draw my checkers board and populate it with checkers. I do this by creating a class which extends JPanel. I then use the paint method to draw my board and checkers and then paint this onto the JPanal. I then have another class which creates the JFrame and adds the JPanel to it. However whenever I add the JPanel to the JFrame, the JPanel wont size to the size I set it at.

Below is my code. This one has really been racking my brains so help would greatly be appreciated.

Cheers

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



public class Board extends  JPanel
{   
    public Board()
    {
        this.setSize(200, 200);
    }

    public void paint(Graphics g) 
    {
        super.paint(g);

        int row; 
        int col; 
        int x,y; 
        String type = "";

        for ( row = 1; row < 9; row++ ) 
        {

            for ( col = 1; col < 9; col++) 
            {

                x=col * 50;
                y=row * 50;

                if ( (row % 2) == (col % 2) )
                {
                    g.setColor(Color.WHITE);
                    type = "white";
                }
                else
                {
                    g.setColor(Color.BLACK);
                    type = "black";             
                }

                g.fillRect(x, y, 50, 50);



                g.setColor(Color.RED);
                if (type == "black" && (row==1 || row ==2 || row==3))
                {
                    g.drawOval(x+5, y+5, 40, 40);
                    g.fillOval(x+5, y+5, 40, 40);
                }
                else if (type == "black" && (row==6 || row ==7 || row==8))
                {
                    g.setColor(Color.BLUE);
                    g.drawOval(x+5, y+5, 40, 40);
                    g.fillOval(x+5, y+5, 40, 40);
                }
            }
        }
    }

}

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


public class main extends JFrame
{


    public static void main(String args[]) 
    {
        Board ex = new Board();
        ex.setLayout(null);
        System.out.println(ex.getSize());

        JFrame frame = new JFrame();
        frame.setSize(600,600);
        frame.setLocationRelativeTo(null);
        frame.add(ex);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }   

}

Recommended Answers

This is a pretty common mystery area, because different layout managers have different behaviour in terms of overriding the size to fit the layout. You may have better luck with setMinimumSize(...)

Jump to Post

All 2 Replies

This is a pretty common mystery area, because different layout managers have different behaviour in terms of overriding the size to fit the layout. You may have better luck with setMinimumSize(...)

@rossi1114:

i can't understand your problem
will you please tell me what actually you want your output?
your code is working perfectly, so what's your demand?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.