Hey.
I have been trying to do a drafts board, but i am having major problems with adding the pieces. Heres my Board. Any Suggestions??

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

public class Board
{

    protected JPanel[][] squares;
    protected JFrame boardFrame;
    protected Container container;
    public void paint (Graphics g) { }




    public Board()
    {

        boardFrame = new JFrame("MyDraughts");
        container = boardFrame.getContentPane();
        container.setLayout(new GridLayout(8,8));
        create_squares();
        boardFrame.setSize(400,450);
        boardFrame.setVisible(true);
    }


    private void create_squares()
    {

        squares = new JPanel[8][8];
        for(int i=0;i<8;i++)
        {
            for(int j=0;j<8;j++)
            {
                JPanel p = new JPanel();
                p.setBackground(setColor(i,j));
                squares[i][j]=p;
                container.add(p);
            }
        }
    }

    private Color setColor(int x, int y)
    {
        if((x+y)%2 == 0)
            return Color.WHITE;
        else
            return Color.BLACK;
    }
}

Like im not looking for code or anything. Just a direction, i was thinking of using drawing panels, and adding each circle in a individual drawing panel in an array, and if it was equal to one color add a circle and if not then leave it blank.

Your code works fine, the gui output the 8x8 board correctly. So whats the problem?

You wanna make so that a user will click on a panel and a circle will appear on it?

Im actually just having trouble getting a way for the circles/pieces to display on the board.

I dont think the JPanel Class has a setIcon() method, can you use JButtons? and just set the setRolloverEnabled() to false in your for loop?

if so, you can have Board implement actionlistener, then give each button an actionlistener. Something like this:

private void create_squares()
    {

        squares = new JButton[3][3];
        for(int i=0;i<3;i++)
        {
            for(int j=0;j<3;j++)
            {
                JButton p = new JButton();
                p.setBackground(setColor(i,j));
                p.setRolloverEnabled(false);
                p.addMouseListener(this);
                squares[i][j]=p;
                container.add(p);
            }
        }
    }

and then in the method actionperformed you can find the button clicked, and set an icon with the setIcon(new ImageIcon())

No im afraid we cant use Jbuttons. That was a good idea though. The pieces have to be drawn onto the board from the very beginning, i was hoping to use a circle that would extend from shape, and insert them into specific drawing panels, would this be viable? Thanks for all your help 11 by the way!!

No im afraid we cant use Jbuttons. That was a good idea though. The pieces have to be drawn onto the board from the very beginning, i was hoping to use a circle that would extend from shape, and insert them into specific drawing panels, would this be viable? Thanks for all your help 11 by the way!!

Yeah no probs, thats find with the JButtons, JPanel has addMouseListener() so instead of having Board implement ActionListener, it will implement MouseListener.

Quick question, do you have to use JPanel, you can you make your own class variation? like MyJPanel, which has a JPanel and a boolean thats false by default and true when a circle is placed on a its JPanel?

Thats quite a good idea alright, but im a little confused as to how you would implement that, i understand what you mean by make my own variation of the JPanel with the boolean, but how would i implement the circle? Thanks again!

import javax.swing.JLabel;
import javax.swing.JPanel;

public class MyJPanel 
{
	private JPanel jpanel;
	private JLabel jlabel;
	private boolean hasCircle;
	
	public MyJPanel()
	{
		//Creates the JPanel and JLabel
		//And adds the label to the panel
		//Sets hasCircle to false since no circle at the start
	}

	public void circleAdded()
	{
		//When adding a circle it will, set the boolean to true
	}
	
	public void circleRemoved()
	{
		//When removing a circle it will, set the boolean to false
	}
	
	public JPanel getJPanel()
	{
		//returns the JPanel
	}
	
	public JLabel getJLabel()
	{
		//will return the JLabel
	}
		
	public boolean isCircleThere()
	{
		//will true the boolean
	}
	
}

in your Board class, instead of a Multidimension Array of JPanel, it will be MyJPanel and everywhere you have JPanel it will by MyJPanel

Be a part of the DaniWeb community

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