0
\$\begingroup\$

I am having issues with being able to have a click that occurs in one class interact with another class. I have a CheckerPiece class, where a mousePressed() click will give me the status ('r', 'e', 'b') of a checkerpiece at a specific row and column. In my CheckerBoard class, I need to continuously update the checkerBoard JPanel. How can I use a click in CheckerPiece, get its info(row,column, status) and apply that info to my logical methods (can see if a move can occur). My code is really messy as I am still a beginner in coding. Also, any guidance you can give me would be wonderful, I would be willing to pay for some tutoring for Comp Sci or for this project.

    public CheckerBoard(char [][] boardStatus) throws Exception {
        board = new JPanel();
        int height = boardStatus.length;
        int width = boardStatus[0].length;
        board.setLayout(new GridLayout(width,height));

    for (int row = 0; row < width; row++) {
        for (int column = 0; column < height; column++) {
            board.add(new CheckerPiece(row, column, boardStatus[row][column]));
        }
    }

        board.removeAll();

        char[][] copyBoard = new char[height][width];
        for (int row = 0; row < width; row++) {
            for (int column = 0; column < height; column++) {
                copyBoard[row][column] = 'e';
            }
        }


        jump(black, red);

        for (int row = 0; row < width; row++) {
            for (int column = 0; column < height; column++) {

                board.add(new CheckerPiece(row, column, copyBoard[row][column]));
            }
        }


        board.validate();
        board.repaint();


        board.setVisible(true);
    }

    public void setBoardStatus(char[][] boardStatus) {
        this.boardStatus = boardStatus;
    }



    public void setBoardStatus(int row, int column, char status) {
        boardStatus[row][column] = status;
    }

    public JPanel getPanel() {
        return board;
    }



   public boolean pieceJump(CheckerPiece black, CheckerPiece red) {
        if (red.getRow() == 0) {
            return false;
        }
        if (black.getRow() == 7) {
            return false;
        }

        if (black.getRow() + 1 == red.getRow() && black.getColumn() + 1 == red.getRow()) {
            return true;
        }
        if (black.getRow() + 1 == red.getRow() && black.getColumn() - 1 == red.getRow()) {
            return true;
        }

        return pieceJump(black, red);
    }

    public void jump(CheckerPiece black, CheckerPiece red) {
        if (pieceJump(black, red)) {
            try {
                black.setRow(black.getRow() + 2);
                black.setColumn(black.getColumn() + 2);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

    public CheckerPiece(int row, int column, char status) throws Exception {

        System.out.println("Creating component " + status + " at " + row + "," + column);
        addMouseListener(this);
        setRow(row);
        setColumn(column);
        setStatus(status);
    }

    /** public int getRow(int y) {
        for (int i = 0; i < 8; i++) {
            if (y >= (i*y) && y <= (i+1) * y) {
                return i;
            } 
        } return -1;
    }

    public int getColumn(int x) {
        for (int i = 0; i < 8; i++) {
            if (x >= (i*x) && x <= (i+1) * x) {
                return i;
            } 
        } return -1;
    }

*/

    public void setRow(int row) throws Exception {
        if (row >= 0 && row <= 7) {
            System.out.println("Setting row to:" + row);
            this.row = row;
        } else throw new IllegalCheckerboardArgumentException("Row must be between 0 and 7");

    }



    public int getColumn() {
        return this.column;
    }

    public int getRow() {
        return this.row;
    }


    public void setColumn(int column) throws Exception {
        if (column >= 0 && column <= 7) {
            System.out.println("Setting column to" + column);
            this.column = column;
        } else throw new IllegalCheckerboardArgumentException("Column must be between 0 and 7");

    }



    public char getStatus() {
        return status;
    }



    public void setStatus(char status) throws Exception {
        if (status == 'b' || status == 'e' || status == 'r')
        this.status = status;
        else throw new IllegalCheckerboardArgumentException("Status must be 'b', 'e', or 'r'.");
    }

    public void paintComponent(Graphics g) {
        int x = 0;
        int y = 0;
        int count = 0;
        if (status == 'b' ) {
            g.setColor(Color.GREEN);
            g.fillRect(0 + x, 0 + y, 60, 60);
            g.setColor(Color.BLACK);
            g.fillOval(10+x, 10 + y, 40, 40);
            x += 60;
            count++;
        }
        if (status == 'e') {
        g.setColor(Color.WHITE);
        if (row % 2 != column % 2) {
            g.setColor(Color.GREEN);
        }
        g.fillRect(0 + x,0 + y, 60, 60);
        x+= 60;
        count++;
        }
        if (status == 'r') {
            g.setColor(Color.GREEN);
            g.fillRect(0 + x,0 + y, 60, 60);
            g.setColor(Color.RED);
            g.fillOval(10 + x, 10 + y, 40, 40);
            x += 60;
            count++;
        }
        if (count % 8 == 0) {
            y += 60;
        }


    }

    public int getCount() {
        return count;
    }

    @Override
    public void mouseClicked(MouseEvent e) {




    }

Not sure if this method is correct at all, but some guidance as what needs to be put here would be helpful.

    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub
        x = e.getX();
        y  = e.getY();

        count++;

        if (count % 2 == 1) {
            if (this.getStatus() == 'e') {
                count--;
            }
        try {
            this.setStatus('e');
        } catch (Exception be) {
            System.out.println("no");
        }
        repaint();
        }

        if ((count - 2) % 4 == 0) {
            if (this.getStatus() == 'e') {
                try {
                    this.setStatus('b');
                } catch (Exception lo) {
                    System.out.println("no");
                }
            } repaint();
        }

        if (count % 4 == 0) {

            if (this.getStatus() == 'e') {
                try {
                    this.setStatus('r');
                } catch (Exception eb) {
                    System.out.println("No");
                }
                repaint();
            }

        }


    }
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.