java - why paint method not being called in snake game -
@suppresswarnings("serial") class gui extends jpanel implements actionlistener { public void paint(graphics g) { super.paint(g); system.out.println("in here..."); g.drawrect(frame.getwidth()/2,frame.getheight()/2,(frame.getwidth()/2)+5,(frame.getheight()/2+5)); g.setcolor(color.black); g.fillrect(frame.getwidth()/2,frame.getheight()/2,(frame.getwidth()/2)+5,(frame.getheight()/2+5)); } @override public void actionperformed(actionevent ae) { if(ae.getsource() == slow) { this.setmotiontosnake(slowmotion); this.repaint(); } } }
im writing snake game program. while doing paint method not beingness called. explaining code: im adding 1 menu bar frame in menu bar start 1 menu 3 sub menus in viz slow,medium,fast. whenever slow motion of snake move decided , should able see rectangular box(at least) in frame. thats why im calling repaint method there.
other this.repaint() have used frame.repaint() / repaint(). method not getting invoked.
thanks ur help in advanced.
im using flow layout manager frame.setlayout(new flowlayout());
a flowlayout respects size of components added panel. creating custom component , default size of component (0, 0) there nil paint.
override getpreferredsize() of custom component homecoming proper dimension component.
also, custom painting done overriding paintcomponent(...) method, not paint() method.
read section swing tutorial on custom painting more info , working examples show how this.
java swing actionlistener paintcomponent repaint
No comments:
Post a Comment