Calling Main method in program, Java -
i have program simulates football. when run main class in eclipse program runs fine. problem want create main menu call main method , start game. have menu made , calls method, starts game not draw game screen. happens when start game menu, if ran directly eclipse works fine. here code:
*package simplesoccer; //imports removed /** * class show main menu , give options view stats, start new game, or exit * * */ public class mainmenu extends jframe { //constructor mainmenu() { super("main menu soccer simulator"); setsize(400, 300); setdefaultcloseoperation(jframe.exit_on_close); container content = getcontentpane(); content.setbackground(color.green); content.setlayout(new flowlayout()); jbutton startgame = new jbutton("start game"); content.add(startgame); startgame.addactionlistener(new buttonlistener()); jbutton viewstats = new jbutton("view stats"); content.add(viewstats); viewstats.addactionlistener(new buttonlistener2()); jbutton exit = new jbutton("exit"); content.add(exit); exit.addactionlistener(new buttonlistener3()); setvisible(true); } //for start game button //main m = new main(); class buttonlistener implements actionlistener { @override public void actionperformed(actionevent a) { main.main(null); } } //for view stats button class buttonlistener2 implements actionlistener { @override public void actionperformed(actionevent a) { stattable.main(null); } } //for exit button class buttonlistener3 implements actionlistener { @override public void actionperformed(actionevent a) { system.exit(0); } } public static void main(string[] args) { mainmenu m = new mainmenu(); } }*
the main class:
/** * @author petr (http://www.sallyx.org/) */ package simplesoccer; *emphasized text* //imports removed public class main { //--------------------------------- globals ------------------------------ // //------------------------------------------------------------------------ static string g_szapplicationname = "simple soccer"; //static string g_szwindowclassname = "mywindowclass"; static soccerpitch g_soccerpitch; // bacause of game restart (g_soccerpitch null while) static lock soccerpitchlock = new reentrantlock(); //create timer static precisiontimer timer = new precisiontimer(prm.framerate); //used when user clicks on menu item ensure option 'checked' //correctly static void checkallmenuitemsappropriately(mymenubar hwnd) { checkmenuitemappropriately(hwnd, idm_show_regions, prm.bregions); checkmenuitemappropriately(hwnd, idm_show_states, prm.bstates); checkmenuitemappropriately(hwnd, idm_show_ids, prm.bids); checkmenuitemappropriately(hwnd, idm_aids_supportspots, prm.bsupportspots); checkmenuitemappropriately(hwnd, id_aids_showtargets, prm.bviewtargets); checkmenuitemappropriately(hwnd, idm_aids_highlite, prm.bhighlightifthreatened); } static void handlemenuitems(int wparam, mymenubar hwnd) { switch (wparam) { case id_aids_noaids: prm.bstates = false; prm.bregions = false; prm.bids = false; prm.bsupportspots = false; prm.bviewtargets = false; prm.bhighlightifthreatened = false; checkallmenuitemsappropriately(hwnd); break; case idm_show_regions: prm.bregions = !prm.bregions; checkallmenuitemsappropriately(hwnd); break; case idm_show_states: prm.bstates = !prm.bstates; checkallmenuitemsappropriately(hwnd); break; case idm_show_ids: prm.bids = !prm.bids; checkallmenuitemsappropriately(hwnd); break; case idm_aids_supportspots: prm.bsupportspots = !prm.bsupportspots; checkallmenuitemsappropriately(hwnd); break; case id_aids_showtargets: prm.bviewtargets = !prm.bviewtargets; checkallmenuitemsappropriately(hwnd); break; case idm_aids_highlite: prm.bhighlightifthreatened = !prm.bhighlightifthreatened; checkallmenuitemsappropriately(hwnd); break; }//end switch } static bufferedimage buffer; static graphics2d hdcbackbuffer; //these hold dimensions of client window area static int cxclient; static int cyclient; long tstart = system.currenttimemillis(); long tend = system.currenttimemillis(); long tdelta = tend - tstart; double elapsedseconds = tdelta / 1000.0; long start = system.currenttimemillis(); long end = start + 60*1000; // 60 seconds * 1000 ms/sec public static void main(string[] args) { final window window = new window(g_szapplicationname); window.seticonimage(loadicon("/simplesoccer/icon1.png")); window.setcursor(cursor.getpredefinedcursor(cursor.default_cursor)); buffer = new bufferedimage(windowwidth, windowheight, bufferedimage.type_int_rgb); hdcbackbuffer = buffer.creategraphics(); //these hold dimensions of client window area cxclient = buffer.getwidth(); cyclient = buffer.getheight(); //seed random number generator common.misc.utils.setseed(0); window.setdefaultcloseoperation(jframe.dispose_on_close); point center = graphicsenvironment.getlocalgraphicsenvironment().getcenterpoint(); //dimension screen = toolkit.getdefaulttoolkit().getscreensize(); window.setresizable(false); int y = center.y - window.getheight() / 2; window.setlocation(center.x - window.getwidth() / 2, y >= 0 ? y : 0); script1.mymenubar menu = script1.createmenu(idr_menu1); window.setjmenubar(menu); g_soccerpitch = new soccerpitch(cxclient, cyclient); checkallmenuitemsappropriately(menu); final jpanel panel = new jpanel() { @override public void paint(graphics g) { super.paint(g); gdi.startdrawing(hdcbackbuffer); //fill our backbuffer white gdi.fillrect(color.white, 0, 0, windowwidth, windowheight); soccerpitchlock.lock(); g_soccerpitch.render(); soccerpitchlock.unlock(); gdi.stopdrawing(hdcbackbuffer); g.drawimage(buffer, 0, 0, null); } }; panel.setsize(windowwidth, windowheight); panel.setpreferredsize(new dimension(windowwidth, windowheight)); window.add(panel); window.pack(); window.addkeylistener(new keyadapter() { @override public void keyreleased(keyevent e) { cpptojava.keycache.released(e); switch (e.getkeychar()) { case keyevent.vk_escape: { system.exit(0); } break; case 'r': case 'r': { soccerpitchlock.lock(); g_soccerpitch = null; g_soccerpitch = new soccerpitch(cxclient, cyclient); jmenubar bar = script1.createmenu(idr_menu1); window.setjmenubar(bar); bar.revalidate(); soccerpitchlock.unlock(); } break; case 'p': case 'p': { g_soccerpitch.togglepause(); } break; }//end switch }//end switch } @override public void keypressed(keyevent e) { cpptojava.keycache.pressed(e); } }); window.addcomponentlistener(new componentadapter() { @override //has user resized client area? public void componentresized(componentevent e) { //if need update our variables drawing //we using cxclient , cyclient scaled accordingly cxclient = e.getcomponent().getbounds().width; cyclient = e.getcomponent().getbounds().height; //now resize backbuffer accordingly. buffer = new bufferedimage(cxclient, cyclient, bufferedimage.type_int_rgb); hdcbackbuffer = buffer.creategraphics(); } }); //make window visible window.setvisible(true); //timer.smoothupdateson(); //start timer timer.start(); while (true) { //update if (timer.readyfornextframe()) { soccerpitchlock.lock(); g_soccerpitch.update(); soccerpitchlock.unlock(); //render //panel.revalidate(); panel.repaint(); try { //system.out.println(timer.timeelapsed()); thread.sleep(2); } catch (interruptedexception ex) { } } }//end while }
}
when press "new game" button program runs seems skip paint() method, can tell me why happening? thanks
Comments
Post a Comment