1

I have posted my code. I am unable to understand where the root cause of the issue is present. This is the UI code of the java chess Engine I've been trying to make. The application runs for about 4-5 moves and 8-9 moves if I am lucky or fast. Eventually, i run into a series of infinite errors as I move my mouse. I think somehow somewhere my code is creating multiple instances of mouseListeners which I don't think should happen. Please guide me on what to do.

import javax.swing.*;
    import java.awt.*;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;

    public class UserInterface extends JPanel implements MouseListener, MouseMotionListener {
        static int mouseX, mouseY, newMouseX, newMouseY;
        static int squareSize = 32;

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            this.setBackground(new Color(120, 100, 45));
            this.addMouseListener(this);
            this.addMouseMotionListener(this);
            for (int i = 0; i < 64; i += 2) {
                g.setColor(new Color(255, 200, 100));
                g.fillRect((i % 8 + (i / 8) % 2) * squareSize, (i / 8) * squareSize, squareSize, squareSize);
                g.setColor(new Color(150, 50, 30));
                g.fillRect(((i + 1) % 8 - ((i + 1) / 8) % 2) * squareSize, ((i + 1) / 8) * squareSize, squareSize, squareSize);
            }
            Image chessPiecesImage;
            chessPiecesImage = new ImageIcon("ChessPieces.png").getImage();

            for (int i = 0; i < 64; i++) {
                int j = -1, k = -1;
                switch (alphaBetaChess.chessBoard[i / 8][i % 8]) {
                    case "P":
                        j = 5;
                        k = 0;
                        break;
                    case "p":
                        j = 5;
                        k = 1;
                        break;
                    case "R":
                        j = 2;
                        k = 0;
                        break;
                    case "r":
                        j = 2;
                        k = 1;
                        break;
                    case "K":
                        j = 4;
                        k = 0;
                        break;
                    case "k":
                        j = 4;
                        k = 1;
                        break;
                    case "B":
                        j = 3;
                        k = 0;
                        break;
                    case "b":
                        j = 3;
                        k = 1;
                        break;
                    case "Q":
                        j = 1;
                        k = 0;
                        break;
                    case "q":
                        j = 1;
                        k = 1;
                        break;
                    case "A":
                        j = 0;
                        k = 0;
                        break;
                    case "a":
                        j = 0;
                        k = 1;
                        break;
                }
                if (j != -1 && k != -1) {
                    g.drawImage(chessPiecesImage, (i % 8) * squareSize, (i / 8) * squareSize, (i % 8 + 1) * squareSize, (i / 8 + 1) * squareSize, j * 64, k * 64, (j + 1) * 64, (k + 1) * 64, this);
                }
            }
            repaint();
    //      g.setColor(Color.black);
    //      g.fillRect(x, y,500/8,500/8);
        }

        @Override
        public void mouseMoved(MouseEvent e) {
        }

        @Override
        public void mousePressed(MouseEvent e) {
            if (e.getX() < 8 * squareSize && e.getY() < 8 * squareSize) {
                //if inside the board
                mouseX = e.getX();
                mouseY = e.getY();
                repaint();
            }
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            if (e.getX() < 8 * squareSize && e.getY() < 8 * squareSize) {
                //if inside the board
                newMouseX = e.getX();
                newMouseY = e.getY();
                if (e.getButton() == MouseEvent.BUTTON1) {
                    String dragMove;
                    if (newMouseY / squareSize == 0 && mouseY / squareSize == 1 && "P".equals(alphaBetaChess.chessBoard[mouseY / squareSize][mouseX / squareSize])) {
                        //pawn promotion
                        dragMove = "" + mouseX / squareSize + newMouseX / squareSize + alphaBetaChess.chessBoard[newMouseY / squareSize][newMouseX / squareSize] + "QP";
                    } else {
                        //regular move
                        dragMove = "" + mouseY / squareSize + mouseX / squareSize + newMouseY / squareSize + newMouseX / squareSize + alphaBetaChess.chessBoard[newMouseY / squareSize][newMouseX / squareSize];
                    }
                    String userPosibilities = alphaBetaChess.possibleMoves();
                    if (userPosibilities.replaceAll(dragMove, "").length() < userPosibilities.length()) {
                        //if valid move
                        alphaBetaChess.makeMove(dragMove);
                        alphaBetaChess.flipBoard();
                        alphaBetaChess.makeMove(alphaBetaChess.alphaBeta(alphaBetaChess.globalDepth, 1000000, -1000000, "", 0));
                        alphaBetaChess.flipBoard();
                        repaint();
                    }
                }
            }
        }

        @Override
        public void mouseClicked(MouseEvent e) {
        }

        @Override
        public void mouseDragged(MouseEvent e) {
        }

        @Override
        public void mouseEntered(MouseEvent e) {
        }

        @Override
        public void mouseExited(MouseEvent e) {
        }
    }
Aman Goel
  • 21
  • 4
  • What is the error message? Can you edit the source down to the relevant bits? At first glance it seems to fishy that the listener is registered in the paintComponent-method. – Felix Leipold Nov 27 '17 at 16:28
  • 1) See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Nov 27 '17 at 16:37
  • 1
    `protected void paintComponent(Graphics g) { .. this.addMouseListener(this);` Paint methods might be called many times, and you have no control over that. Add the listener **once** when the panel is being constructed. – Andrew Thompson Nov 27 '17 at 16:38
  • See also [Making a robust, resizable Swing Chess GUI](http://stackoverflow.com/q/21142686/418556). – Andrew Thompson Nov 27 '17 at 16:40
  • Thanks, @AndrewThompson and everyone else.. I solved my bug Thank you :) – Aman Goel Nov 27 '17 at 18:57

1 Answers1

1

add the mouse listener and add motion listener where the panel was constructed.This way the motion listener will not be created multiple times.

Aman Goel
  • 21
  • 4