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);
        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) {
                    GridChessBoard.addSquares(i, j);
                    ^^^^^^ ^ ^^^

... it should then 'addSquares  to the newly installed Grid ... i'm sittring  here trying 
everything i know (not much). i there's *got* to be a way. can you hlp???

k

            }
        }

        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();
        ChessBoard cb = new ChessBoard();
    }
}

Recommended Answers

All 2 Replies

Remember how components work - you can add other components to them either through absolute positioning or through various layouts.

You're still trying to add an object (which does NOT have any direct implementation of a LayoutManager) as a LayoutManager.

Want to know why?

First of all in order to give a valid argument for a method it either--

-Is the type specified
-Is a type that derives from the type specified (has data of, or instanceof to be more precise)
-Implements the interface object that has the same methodology of that object

and GridChessBoard doesn't fit any of those descriptions when you call--

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

--You're casting some inner-class object as a LayoutManager and the only reason I believe the compiler won't argue with you is because of how inner classes are treated when they don't directly extend from anything.

Also when using JFrames I'm almost certain you can simply call--

setLayout(LayoutManager arg);
add(Component c);

--etc for the content pane without having to get the content pane and reference it through a container.

My advice is to simply use 2 separate containers. One container takes up 80% of your JFrame and contains all of the chessboard pieces with a grid layout of 8x8. The other 20% will be controlled by another container that will be a 16x2 thinner container that simply displays the pieces that are no longer on the board.

For example, if a piece is taken on the board it will be removed from the first container and added to the second container.

You can name the first container ChessBoard and the other container ChessPieceHolder so you only have to control one frame to do both jobs for you.

In order for you to make one component share a certain amount of space with another in your initial JFrame you'll have to either--

-Use absolute positioning (which I strongly discourage, but may be more self-explanatory).
-Learn the GridBagLayout manager (which will be incredibly hard, but encouraged).

--Here are some ideas for you.

-Take some free time to use powerpoint (or some other program) to draw the images for your pieces (as .jpg or .png or some other recognized image file).
-Think about how components work and how you want your board to look.
-Learn how to use absolute positioning or the GridBagLayout manager

Hopefully this helps you understand. If needed I'll try to do an example of my own chessboard and supply the source code on my free time, but I will be busy studying for 2 finals coming up next week XP

Alex is correct:

You're still trying to add an object (which does NOT have any direct implementation of a LayoutManager) as a LayoutManager.

There is already a LayoutManager that will allow you to achieve your goal. It's called the GridLayout and you should certainly check the Java Docs on that one. Here is code from the docs:

import java.awt.*;
 import java.applet.Applet;
 public class ButtonGrid extends Applet {
     public void init() {
         setLayout(new GridLayout(3,2));
         add(new Button("1"));
         add(new Button("2"));
         add(new Button("3"));
         add(new Button("4"));
         add(new Button("5"));
         add(new Button("6"));
     }
 }

Once you've added the GridLayout you can then add components and the will be slotted in automatically. I think you'd likely want to added buttons as the individual boxes on the chess board.

You approach is really not going to work out. Particularly when you attempt to call the static method GridChessBoard.addSquares(i, j); Well I hope this gives you a good start to recovery :icon_wink:

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.