multithreading - Thread errors when running Java game -


i getting strange errors when running java game. i've been working on game weeks project computer science class , due on 6/4/2015, appreciate help. wrote game in class called game , ran static method called run. later, decided add gui menu in class called control. run game now, call main method in control class. menu has button action listener. when button clicked, run method in game class called. if click , run run method directly game works fine. if click button calls run method draws frame, not actual game.

here code gui:

    public class control extends jframe implements actionlistener {      // jpanel     public jpanel pnlbutton = new jpanel();      // buttons     public jbutton btnaddflight = new jbutton("multiplayer");     public jbutton single = new jbutton("singleplayer");      public control() throws ioexception,interruptedexception {         super("bouncy ball");         //set button size         btnaddflight.setbounds(150, 400, 220, 30);         single.setbounds(150,350,220,30);          // jpanel bounds         pnlbutton.setbounds(0, 0, 500, 500);         pnlbutton.setbackground(color.white);          // adding bouncy ball logo jpanel         string path = "gg.jpg";         file file = new file(path);         bufferedimage image = imageio.read(file);         jlabel label = new jlabel(new imageicon(image));         label.setbounds(179,50,150,150);          //action listener setup         single.addactionlistener(this);          //add buttons , title logo jpanel         pnlbutton.add(btnaddflight);         pnlbutton.add(label);         pnlbutton.add(single);          //set , add instructions jpanel         jlabel gg = new jlabel("welcome bouncy ball, game have manipulate");         gg.setfont(new font("serif", font.plain, 18));         gg.setbounds(0,10,500,500);         pnlbutton.add(gg);         jlabel f = new jlabel("the window size wasd win! click buttons start.");         f.setfont(new font("serif", font.plain, 18));         f.setbounds(0,28,500,500);         pnlbutton.add(f);         pnlbutton.setlayout(null);          //add jpanel jframe         this.add(pnlbutton);          // jframe properties         setsize(500, 500);                settitle("bouncy ball");         setlocationrelativeto(null);         setdefaultcloseoperation(jframe.exit_on_close);         setvisible(true);;     }      @override     public void actionperformed(actionevent submitclicked) {         try{             isclicked = true;              game.run();         }         catch(interruptedexception a){}     }      public static void main(string[] args) throws ioexception,interruptedexception{         new control();     } } 

and here run method in game class:

public static void run() throws interruptedexception {     //setting jframe     frame = new keyframe("bouncy ball");     frame.setsize(1000, 1000);      //setting scanner , user level input     scanner scan = new scanner (system.in);     system.out.println("level one, two, three, or four?(must int, 4 multiplayer)");     f = scan.nextint();      //display jframe     frame.setvisible(true);     frame.setdefaultcloseoperation(jframe.exit_on_close);     frame.tofront();      //add game jpanel     game = new game();     frame.add(game);      //setting game basics     b = new ball(game,0,0,30,30);     setuplevel();     objlist.add(0,b);      //main game loop     while (true) {         //request focus         game.requestfocusinwindow();          //update current time         laststarttime = system.currenttimemillis();          //move ball , player         p.move();         b.move();          //refresh jpanel         game.repaint();         thread.sleep(10);          //check if player wins or loses         checkwin();         checkloss();     } } 

if call run method directly works, not if click button in gui. have feeling thread issue, because main thread ends once run called. i'm not 100% percent sure cause though. i'd appreciate responses, i've been working on game weeks project computer science class , due on 6/4/2015.

swing single threaded - painting, events, etc...occur on thread (named edt). game.run method called on edt, in turn executing long running task (eg while (true), thread.sleep) - prevents edt doing else. perform animation, consider using thread, or better yet swing timer.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -