java - Buffer Strategy IllegalStateException -


i know has been asked before still can't work.

public class gui extends jframe implements runnable{      public static jpanel contentpane;     public static graphics2d graphics;     public static bufferstrategy bufferstrategy;      /**      * launch application.      */     public static void main(string[] args) {         eventqueue.invokelater(new runnable() {             public void run() {                 try {                     gui frame = new gui();                     frame.setvisible(true);                     frame.setresizable(false);                 } catch (exception e) {                     e.printstacktrace();                 }             }         });     }      /**      * create frame.      */     public gui() {         setresizable(false);         settitle("tower defense game");         setignorerepaint(true);         setdefaultcloseoperation(jframe.exit_on_close);         setbounds(100, 100, 450, 300);         contentpane = new jpanel();         contentpane.setborder(new emptyborder(5, 5, 5, 5));         contentpane.setlayout(new borderlayout(0, 0));         setcontentpane(contentpane);       }      @override     public void run() {          createbufferstrategy(2);         bufferstrategy = getbufferstrategy();         graphics = (graphics2d) bufferstrategy.getdrawgraphics();          for(int infinitevar = 0; infinitevar == -1; infinitevar++){              graphics.setbackground(color.white);             graphics.drawline(100, 100, (int) (math.random() * ((200-50) + 1) + 50), (int) (math.random() * ((200-50) + 1) + 50));              try {                 thread.sleep(10);             } catch (interruptedexception e) {                 e.printstacktrace();             }              infinitevar = 0;         }     } } public class initialize {  public static void main(string[] args){      gui.main(args);      gui objgui = new gui();     thread threadgui = new thread(objgui);     threadgui.start(); } } 

i exception in thread "thread-2" java.lang.illegalstateexception: component must have valid peer on line try make buffer strategy. think i'm supposed make frame first, call before make thread makes buffer strategy.

basically, problem starts here...

public static void main(string[] args) {     eventqueue.invokelater(new runnable() {         public void run() {             try {                 gui frame = new gui();                 frame.setvisible(true);                 frame.setresizable(false);             } catch (exception e) {                 e.printstacktrace();             }         }     }); } 

and exasperated here...

public class initialize {      public static void main(string[] args) {          gui.main(args);          gui objgui = new gui();         thread threadgui = new thread(objgui);         threadgui.start();     } } 

basically, happening, gui.main method creating new instance of gui, gets shown on screen, create instance of gui...

gui objgui = new gui(); thread threadgui = new thread(objgui); threadgui.start(); 

to try , use create bufferstrategy with, instance not visible on screen (displayable, or attached native peer), hence problem...

instead, rid of main method in gui, it's not doing favors , apply it's logic in initialize

gui frame = new gui(); // better idea before make frame visible // can change frame borders , cause issues frame.setresizable(false);  frame.setvisible(true);  thread thread = new thread(frame); thread.start(); 

you can add check in run method wait till jframe becomes displayable...

@override public void run() {      while (!isdisplayable()) {         try {             thread.sleep(100);         } catch (interruptedexception ex) {         }     }     //... 

you should read javadocs on bufferstrategy better understand how manage them...


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -