swing - at java.awt.AWTEventMulticaster.mouseExited(Unknown Source) occur after using the GUI for several times -
good morning, i'm implementing gui game , when play game sometime endless number of exception game freezes, on problem or how fix appreciated
here 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){ (int = 0; < 8; i++) { (int j = 0; j < 8; j++) { if(e.getsource().equals(boardframe[i][j])){ possiblemoves = board.getpossiblemoves(new point(j,i)); (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){ (int = 0; < 8; i++) { (int j = 0; j < 8; j++) { if(e.getsource().equals(boardframe[i][j])){ (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 winner"); this.getcontentpane().add(winner); } }
the exception massage endless number of @ java.awt.awteventmulticaster.mouseexited(unknown source)
im pretty sure board class 100% made teacher assistants in our university , passed test
thanks in advance
i see potential source of problems:
(int = 0; < 8; i++) { (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 keyed in error involved swing mouse handling. appear adding mouselistener multiple times components. picture this, when mouselistener called, adds mouselistener same component. next mousepress, mouselistener called twice , two mouselisteners added, 4, 8, 16, ... lead geometric increase in number of mouselisteners added whenever 1 called, , overwhelm system.
solution:
- don't this. don't add same listener component inside of listener code.
- again, don't use mouselisteners @ jbuttons. use actionlisteners.
Comments
Post a Comment