Using java bufferstrategy and swing elements at the same time -


i program simple rts game now, , first experience in game design. problem when use createbufferstrategy(2) swing elements (buttons etc...) not displayed after bufferstrategy.show(); method invoked. game full of buttons, tables , other crap, , don't want code own. java's layouts, , want make gui on this. so, here little code example, not game, demonstration of problem. thanks. btw, understand source of problem. know swing draw mechanic event-based, while using bufferstrategy not event-based. don't know how solve this. thank you. , final - don't want use default swing event-based approach becouse slow games, , far know bufferstratgey approach games. thx.

public static void main(string[] args){     final jframe frame = new jframe();     jpanel menupanel = new jpanel();     final button button = new button("exit");     button.addactionlistener(new actionlistener() {         @override         public void actionperformed(actionevent e) {             system.exit(0);         }     });     menupanel.add(button);     frame.add(menupanel);     frame.setpreferredsize(new dimensionuiresource(800, 600));      //frame.setresizable(false);     //frame.setundecorated(true);     //frame.setignorerepaint(true);      swingutilities.invokelater(new runnable() {         @override         public void run() {             final long delay = 1000/60;             frame.pack();             frame.setvisible(true);             final graphicsdevice device = graphicsenvironment.getlocalgraphicsenvironment().getdefaultscreendevice();             if (device.isfullscreensupported()){                 device.setfullscreenwindow(frame);                 // place. if turn fullscreen mode button disappear. , if stay in windowed mode button exist , work;             }             frame.createbufferstrategy(2);             final bufferstrategy bufferstrategy = frame.getbufferstrategy();              thread renderingthread = new thread(){                 public void run(){                     while (true){                         long startrenderingtime = system.currenttimemillis();                         graphics g = bufferstrategy.getdrawgraphics();                         g.setcolor(color.white);                         g.fillrect(0,0,1680,1050);                         //button.paint(g);                         //button.paintall(g);                         // don't know how draw button!                         g.dispose();                         if (!bufferstrategy.contentslost()){                             bufferstrategy.show();                         }                         long endrenderingtime = system.currenttimemillis();                         long actiontime = endrenderingtime-startrenderingtime;                         try {                             if (actiontime>delay){                                 sleep(5);                             } else {                                 sleep(delay-actiontime);                             }                         } catch (interruptedexception e){                             return;                         }                     }                 }             };             renderingthread.start();         }     }); } 

you're running infinite loop on swings edt, blocking swing doing anything.

i don't see need bufferstrategy when want display swing elements. combine custom rendering swing components create component renders stuff , add normal layout.

your component overwrites paintcomponent() , draws whatever needs to. can trigger update of component calling repaint() on it. performs enough. note swing components double buffered default, there no need work bufferstrategy there.

if want stick active rendering, call swings rendering chain selectively, example frame's contentpane , call paint() in rendering loop. hacking way may cause unwanted side effects.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -