2

Good Morning, i'm implementing the GUI for a game and when i play the game for sometime i get an endless number of this exception then the game freezes, any help on what is the problem or how to fix it is much appreciated

here is the code:

public class BoardFrame extends JFrame implements MouseListener {


    private void boardWithoutCheckers() {


        for(int i=0; i<8; i++) {
            for(int j=0; j< 8; j++) {
                if(((i + j) % 2) == 0){
                    boardFrame[i][j] = new LightGrayButton();

                    }
                    else {
                        boardFrame[i][j] = new DarkGrayButton();
                    }
                boardFrame[i][j].addMouseListener(this);

                this.getContentPane().add(boardFrame[i][j]);
            }

        }
        this.setVisible(true);
    }



@Override
public void mouseClicked(MouseEvent e) {



    count++;
    if(count == 1){
    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            if(e.getSource().equals(boardFrame[i][j])){
                possibleMoves = board.getPossibleMoves(new Point(j,i));
            for (int k = 0; k < possibleMoves.size(); k++) {

                Point temp = new Point(possibleMoves.get(k).getX(),possibleMoves.get(k).getY());
                boardFrame[temp.getY()][temp.getX()].setBackground(new Color(99,204,94,50));
            }
            firstClick = new Point(j, i);
            break;
            }
            }
        }

    }
    if(count == 2){

        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                if(e.getSource().equals(boardFrame[i][j])){


                for (int k = 0; k < possibleMoves.size(); k++) {
                    if(possibleMoves.get(k).getX() == j && possibleMoves.get(k).getY() == i){
                        if(board.getTurn() == 1){
                        boardFrame[i][j].setIcon(null);
                        boardFrame[i][j].setIcon(new ImageIcon(Earth));
                        boardFrame[firstClick.getY()][firstClick.getX()].setIcon(null);
                        board.move(firstClick, new Point(j,i));



                        }
                        else if(board.getTurn() == 2){
                            boardFrame[i][j].setIcon(null);
                            boardFrame[i][j].setIcon(new ImageIcon(Mars));
                            boardFrame[firstClick.getY()][firstClick.getX()].setIcon(null);
                            board.move(firstClick, new Point(j,i));

                            break;
                        }


                }

                }
                }
                }

    }

        count=0;
        possibleMoves = new ArrayList<Point>();
        for(int i=0; i<8; i++) {
            for(int j=0; j< 8; j++) {
                if(((i + j) % 2) == 0){
                    boardFrame[i][j].setBackground(new Color(15, 81, 162));

                    }
                    else {
                        boardFrame[i][j].setBackground(new Color(77, 77, 77));
                    }
                boardFrame[i][j].addMouseListener(this);
            }

    }
    }

    if(board.isGameOver()){
        JLabel winner = new JLabel("we have a winner");
        this.getContentPane().add(winner);
    }




}

The only exception massage i get only an endless number of it at java.awt.AWTEventMulticaster.mouseExited(Unknown Source)

im pretty sure that the board class is 100% as it is made by the teacher assistants in our university and it passed all the test

Thanks in advance

cdLegend
  • 71
  • 1
  • 2
  • 12
  • In all likelihood the problem lies in code you've not shown us, such as your Board class. Please show your full exception message. Please indicate which line throws the exception. – Hovercraft Full Of Eels May 09 '13 at 15:28
  • The only exception message i get is at java.awt.AWTEventMulticaster.mouseExited(Unknown Source) but an endless number of it also concerning the the board class im pretty sure it is correct as the teacher assistant in our university is the one who implemented it and it passes succesfully all tests – cdLegend May 09 '13 at 15:44
  • Consider creating and posting an [sscce](http://sscce.org) (please see the link). – Hovercraft Full Of Eels May 09 '13 at 15:48
  • Please show the entire stacktrace. – martinez314 May 09 '13 at 15:51
  • hovercraft i edited it again, but the problem is that i dont know where are the problems in boardframe to know what and what not to remove whiskeyspider sorry i dont understand what you mean – cdLegend May 09 '13 at 15:54
  • @cdLegend [What is a stacktrace?](http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors) – martinez314 May 09 '13 at 15:57
  • @whiskeyspider i dont know? – cdLegend May 09 '13 at 16:02
  • @cdLegend Sorry for the confusion. My question above is a link which should explain. – martinez314 May 09 '13 at 16:04
  • so i think i know stacktrace but not the name of it, but the problem here is that the only thing i get is this, nothing more nothing less at java.awt.AWTEventMulticaster.mouseExited(Unknown Source) – cdLegend May 09 '13 at 16:12
  • I think that since we don't have a working program and can't reproduce your unusual error, it will be quite hard for us to debug it. One thing that I will mention is that you shouldn't be using a MouseListener at all here. Since you are using JButtons, you should be using an ActionListener that is added to each button. You will also need to put in some effort to try to isolate your error. This may involve using println statements to see where in the code the error is occurring, using a debugger, and/or cutting out code until you can find the bare minimum code required to reproduce the error. – Hovercraft Full Of Eels May 09 '13 at 16:30
  • should i put all classes here so that you can run the program? – cdLegend May 09 '13 at 16:38
  • If it's a small amount of code, then sure, however if you have a monstrous amount of code, no one will have the time or inclination to go through it all, nor should they. In that situation, if you still need our help, you should seriously consider putting in the time and effort to create and post an [sscce](http://sscce.org). It won't be easy or quick to do this, but it would likely get you a decent answer quicker than by any other means. First though consider using ActionListeners for your JButtons. – Hovercraft Full Of Eels May 09 '13 at 16:41

1 Answers1

4

I see a potential source of problems:

     for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
           if (((i + j) % 2) == 0) {
              boardFrame[i][j].setBackground(new Color(15, 81, 162));

           } else {
              boardFrame[i][j].setBackground(new Color(77, 77, 77));
           }
           boardFrame[i][j].addMouseListener(this); // !! here !!
        }

     }

I was keyed in that your error involved Swing mouse handling. You appear to be adding a MouseListener multiple times to your components. So picture this, when the MouseListener is called, it adds another MouseListener to the same component. With the next mousepress, the MouseListener will be called twice and two MouseListeners will be added, then 4, then 8, then 16, ... This will lead to a geometric increase in the number of MouseListeners added whenever one is called, and will soon overwhelm your system.

Solution:

  1. Don't do this. Don't add the same listener to a component inside of its listener code.
  2. Again, don't use MouseListeners at all for JButtons. Use ActionListeners.
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • thanks alot i changed the mouselistener to actionlistener and removed boardFrame[i][j].addMouseListener(this); and it solved the problem it is working smoothly now, thanks alot for your time and sorry that my question was unclear – cdLegend May 09 '13 at 16:53