... what i am trying to do use thr GridChessBoard clas to paint/drae an
empty board then return to the program which adds the men/pieces to
the board.

... i can get the empty board but it doesn't add the meb/pieces. i don't know
why. i suppose i bit off more thsn i can chew but i got a large mouth. let's do
this thing.

package chessbuard;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//import javax.swing.event.*;
import javax.swing.border.Border;

class ChessBoard extends JFrame implements MouseListener, MouseMotionListener {

    GridLayout Board = new GridLayout(8, 8);
    int drag1, drag2;
    int xMove, yMove, labelw, labelh;
    JLabel label;
    String[] manrow = {"Pawn0", "Pawn1", "Pawn2", "Pawn3", "Pawn4", "Pawn5", "Pawn6", "Pawn7"};
    String[] manrowx = {"Pawn0", "Pawn1", "Pawn2", "Pawn3", "Pawn4", "Pawn5", "Pawn6", "Pawn7"};
    String[] piecerow = {"Rook", "Knight", "Bishop", "Queen", "King", "Bishop", "Knight", "Rook"};
    String[] piecerowx = {"Rook", "Knight", "Bishop", "Queen", "King", "Bishop", "Knight", "Rook"};

// update these labels position, which is parent jpanel, at each move
    JPanel[][] grids;
    JLabel[][] pieces;
    Dimension pieceSize = new Dimension(100, 100);
    Container con;
    Component DraggingComp;
    GridLayout board;

    public ChessBoard() {
        super("ChessBoard");
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        board = new GridLayout();
        board.setHgap(4);
        board.setVgap(4);
        Dimension boardSize = new Dimension(800, 800);
        //GridChessBoard brd = new GridChessBoard();
        con = getContentPane();
        con.setLayout((LayoutManager)new GridChessBoard());
        con.addMouseListener(this);
        con.addMouseMotionListener(this);
        label = new JLabel("Pawn3");
        Dimension d = label.getPreferredSize();
        labelw = d.width;
        labelh = d.height;
        label.setBounds(10, 10, labelw, labelh);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        setVisible(true);
        for (int i = 0; i < 8; ++i) {
            for (int j = 0; j < 8; ++j) {
                     addSquares(i, j);
            }
        }

        setSize(boardSize);

    } // end constructor

    public JFrame addAction(JFrame frame) {
        frame.getContentPane().addMouseMotionListener(this);
        return frame;
    }

    public void seeIt(Component c) {
        con.add(c);
        ((JPanel) con).revalidate();
        con.setVisible(true);
    }

    public void moveComponent(Component c, int newX, int newY, int width, int height) {
        System.out.println("in moveComponent");
        c.setBounds(newX, newY, labelw, labelh);
        c.setSize(pieceSize); // debug, just to see
        con.add(c);
        ((JPanel) con).revalidate();
        con.setVisible(true);
    }

    public String getMan(int i) {
        return manrow[i];
    }

    public String getPiece(int i) {
        return piecerow[i];
    }

    public boolean isOdd(int i) {
        return i % 2 != 0;
    }

    public boolean isEven(int i) {
        return i % 2 == 0;
    }

    public JLabel setBlue(JLabel b1) {
        b1.setBackground(Color.blue);
        return b1;
    }

    public JLabel setYellow(JLabel b1) {
        b1.setBackground(Color.yellow);
        return b1;
    }

    public void addColors() {
        for (int i = 0; i < 64; i++) {
            JLabel square = new JLabel("see");
            add(square);
            int row = (i / 8) % 2;
            if (row == 0) {
                square.setBackground(i % 2 == 0 ? Color.blue : Color.yellow);
            } else {
                square.setBackground(i % 2 == 0 ? Color.yellow : Color.blue);
            }
        }
    }

    public JLabel addBlankRow(int i, int j) {
        JLabel square = new JLabel();
        if (isEven(j)) {
            square.setBackground(i % 2 == 0 ? Color.yellow : Color.blue);
            square.setName("blank");  // added
        } else {
            square.setBackground(i % 2 == 0 ? Color.blue : Color.yellow);
            square.setName("blank");  // added
        }
        return square;
    }

    public JLabel addManRow(int i, int j) {
        String[] m = i == 1 ? manrowx : manrow;
        JLabel square = new JLabel();
        square.setLayout(new FlowLayout());

        int col = j + 1;
        if (isEven(col)) {
            square.setBackground(i % 2 == 0 ? Color.blue : Color.yellow);
            square.add(setYellow(new JLabel(m[j])), JLabel.CENTER);
            square.setName(m[j]);// added
        } else {
            square.setBackground(i % 2 == 0 ? Color.yellow : Color.blue);
            square.add(setBlue(new JLabel(m[j])), JLabel.CENTER);
            square.setName(m[j]);  // added
        }
        return square;
    }

    public JLabel addPieceRow(int i, int j) {

        String[] m = i == 1 ? piecerow : piecerowx;
        JLabel square = new JLabel();
        square.setLayout(new FlowLayout());


        int col = j;
        if (isEven(col) && (col < 8)) {

            square.add(setYellow(new JLabel(m[j])), JLabel.CENTER);
            square.setBackground(Color.yellow);
            square.setName(m[j]);  // added
        } else {
            square.add(setBlue(new JLabel(m[j])), JLabel.CENTER);
            square.setBackground(Color.blue);
            square.setName(m[j]);  // added

        }
        return square;
    }

    public JLabel addPieceRowx(int i, int j) {
        String[] m = i == 1 ? manrowx : manrow;
        JLabel square = (new JLabel(m[j]));
        square.setLayout(new FlowLayout());

        int col = j + 1;
        if (isEven(col)) {
            square.setBackground(Color.yellow);
            square.add(setYellow(new JLabel(m[j])), JLabel.CENTER);
            square.setName(m[j]);  // added
        } else {
            square.setBackground(Color.blue);
            square.add(setBlue(new JLabel(m[j])), JLabel.CENTER);
            square.setName(m[j]);  // added
        }
        return square;
    }

    public JLabel addManRowx(int i, int j) {
        String[] m = i == 1 ? manrowx : manrow;
        JLabel square = new JLabel();
        square.setLayout(new FlowLayout());

        int col = j + 1;
        if (isEven(col)) {
            square.setBackground(Color.blue);
            square.add(setBlue(new JLabel(m[j])), JLabel.CENTER);
            square.setName(m[j]);  // added
        } else {
            square.setBackground(Color.yellow);
            square.add(setYellow(new JLabel(m[j])), JLabel.CENTER);
            square.setName(m[j]);  // added
        }
        return square;
    }

    public void addSquares(int i, int j) {

        JLabel square = new JLabel();
        int col = j;
        int row = i;
        switch (row) {
            case 0:
                square = addPieceRow(row, col);
                break;
            case 1:
                square = addManRow(row, col);
                break;
            case 2:
                square = addBlankRow(row, col);
                break;
            case 3:
                square = addBlankRow(row, col);
                break;
            case 4:
                square = addBlankRow(row, col);
                break;
            case 5:
                square = addBlankRow(row, col);
                break;
            case 6:
                square = addManRowx(row, col);
                break;
            case 7:
                square = addPieceRowx(row, col);
                break;
        }
        con.add(square);
    }

    public void mousePressed(MouseEvent me) {
        System.out.println("in mouse pressed");

        // get orriginal coordinates and set text
        xMove = me.getX();
        yMove = me.getY();
        DraggingComp = con.getComponentAt(xMove, yMove);
    }

    public void mouseDragged(MouseEvent me) {

        System.out.println("in mouse dragged");
        DraggingComp = con.getComponentAt(xMove, yMove);
        if (DraggingComp == null) {
            return;
        }
        xMove = me.getX();
        yMove = me.getY();
    //moveComponent(findComponentAt(xMove, yMove), xMove, yMove, labelw, labelh);
    }

    public void mouseReleased(MouseEvent me) {
    }

    public void mouseEntered(MouseEvent me) {
    }

    public void mouseExited(MouseEvent me) {
    }

    public void mouseMoved(MouseEvent me) {
    }

    public void mouseClicked(MouseEvent me) {
    }

   static class GridChessBoard {

        JFrame frame;
        Container con;

        public GridChessBoard() {
            frame = new JFrame("Chess Board made by Hiroshi Iwatani");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            con = frame.getContentPane();

            con.setLayout(new GridLayout(8, 8));
            for (int i = 0; i < 64; i++) {
                JPanel square = new JPanel(new BorderLayout());
                Border blackline;
                blackline = BorderFactory.createLineBorder(Color.black);
                square.setBorder(blackline);
                con.add(square);


                int row = (i / 8) % 2;
                if (row == 0) {
                    square.setBackground(i % 2 == 0 ? Color.green : Color.yellow);
                    //addSquares(i, i%8);
                } else {
                    square.setBackground(i % 2 == 0 ? Color.yellow : Color.green);
                }

            }

            frame.setSize(800, 800);
            frame.setVisible(true);

        }
    }

    public static void main(String[] args) {
        GridChessBoard gcb = new GridChessBoard();
        new ChessBoard();
    }
}

I get a run-time error in this line in your ChessBoard () constructor.

con.setLayout((LayoutManager)new GridChessBoard());

The error is this:

Exception in thread "main" java.lang.ClassCastException: chessbuard.ChessBoard$GridChessBoard cannot be cast to java.awt.LayoutManager

I'm not sure what you are trying to do here. The chess board displays fine, but then it hits that error. What are the ChessBoard and GridChessBoard classes supposed to do? How do the pieces get displayed? Do you put a label on each square with the name of the piece? Looks like ChessBoard contains all of the pieces and places them where they need to go. GridChessBoard displays an empty board, but does it need to do anything else? Looks like it has 64 JPanels in it, but where do the labels get painted onto the JPanels? I'm not seeing where ChessBoard and GridChessBoard are communicating and which class contains and is responsible for what. In particular, I don't see where the grids array in ChessBoard is ever used and where there is ever a connection between the 64 JPanels in grids[][] and the 64 JPanels created in the GridChessBoard constructor. Finally, is the line in red below intentional? Do you want to call this constructor without an object of type ChessBoard?

public static void main(String[] args) {
        GridChessBoard gcb = new GridChessBoard();
        new ChessBoard();
    }

... ok, as it stands now, without the Grid... it works fine but THE COLORS belomg to the pieces and that is not 'kool; they shoulf belong to the board ('kool') so enter Grid ... I am a perfectionist or i would have stopped there. it is a lot of work but i'll get there.

k

... i've decided to forget the lass and conver Grid... to a method which returns a LM but when i try to cast the frsme in Grid ... i get an error 'can't cast fram to LM'??? i *really* need this. ALL my prob lemd will vanish.

k

commented: Did you actually read that gibberish before posting it? Make an effort to read what you type before posting. -2
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.