Java Swing Button Listeners not working -
i have 2 button listeners game board using java swing.
a tetris grid created , addition functionality within each button listener.
i set board in play.java:
final tetrisgame g = new tetrisgame(11,1); final boardgraphics graphics = new boardgraphics(tetrisboard.board_width, 40, g);
the button listeners created in same play.java:
graphics.btnstart.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { action arc = p.getaction(g); g.update(arc); graphics.colours.clear(); graphics.setcolor(g.getboard().getgrid()); while (arc instanceof store){ arc = p.getaction(g); g.update(arc); graphics.colours.clear(); graphics.setcolor(g.getboard().getgrid()); } graphics.tiles.redraw(); system.out.println(); system.out.println(g.tostring()); system.out.println(); } }); graphics.btnauto.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { while (!g.gameended()){ action arc = p.getaction(g); g.update(arc); graphics.colours.clear(); graphics.setcolor(g.getboard().getgrid()); while (arc instanceof store){ arc = p.getaction(g); g.update(arc); //graphics.colours.clear(); graphics.setcolor(g.getboard().getgrid()); } graphics.tiles.redraw(); system.out.println(); system.out.println(g.tostring()); system.out.println(); /*try { thread.sleep(1000); } catch (interruptedexception e1) { // todo auto-generated catch block e1.printstacktrace(); }*/ } } });
the btnstart works perfectly, pressing once, painting tetrisboard according next move given ai agent.
i btnauto play each move out without user pressing btnstart generate move until end. however, btnauto not paint on grid final state of game, finishing state.
can see why might not repainting grid after each move generated in while loop?
your while loop being called on swing event thread , preventing the thread doing necessary actions including rendering gui , interacting user:
while (!g.gameended()){ action arc = p.getaction(g); // .... }
i use swing timer here instead of while (true)
loop. option use background thread, since desire simple game loop , don't need run long-running in background, think second option more complex no additional benefit.
as aside, i'm curious how you're doing drawing , how you're getting graphics object draw with. you're not calling getgraphics()
on component, you?
edit state in comment:
i have class nested class extends jpanel. drawing of grid , getgraphics() done within nested class.the parent class creates component , sets layout of gui whole
don't graphics object calling getgraphics()
on gui component graphics object obtained not persist. see so, minimize , restore application , tell me happens graphics after doing this. should of drawing in jpanel's paintcomponent override. 1 option call getgraphics()
on bufferedimage , use draw bufferedimage, , display bufferedimage in paintcomponent override. if use second technique, don't forget dispose of bufferedimage's graphics object after done using don't hog system resources.
Comments
Post a Comment