java - Why will nothing paint on my screen? -
i working on 2d platformer using swing in java, , developing framework game. testing reapaint() , draw functions, not function , clueless. here code:
window.java
import javax.swing.jframe; import javax.swing.swingutilities; public class window extends jframe { public window() { this.setdefaultcloseoperation(exit_on_close); this.setsize(1000, 1000); this.setvisible(true); this.settitle("infiltrator"); this.setlocationrelativeto(null); this.setcontentpane(new framework()); } public static void main(string[] args) { swingutilities.invokelater(new runnable() { @override public void run() { new window(); } } ); } }
panel.java
import java.awt.graphics; import java.awt.graphics2d; import javax.swing.jpanel; public abstract class panel extends jpanel { public panel() { this.setdoublebuffered(true); this.setfocusable(true); /* * if(false) { bufferedimage blankcursorimg = new bufferedimage(16, 16, bufferedimage.type_int_argb); cursor blankcursor = toolkit.getdefaulttoolkit().createcustomcursor(blankcursorimg, new point(0, 0), null); this.setcursor(blankcursor); } * */ } public static void draw(graphics2d g2d) { } @override public void paintcomponent(graphics g) { graphics2d g2d = (graphics2d)g; super.paintcomponent(g2d); framework.draw(g2d); } }
framework.java
import java.awt.color; import java.awt.graphics2d; import javax.swing.jframe; public class framework extends panel { private static long begintime, takentime, sleeptime; private static long secinnano = 1000000000l; private static long secinmilli = 1000000l; private static long fps = 60l; private static long fpstime = secinnano / fps; public static enum gamestate {starting, mainmenu, playing, over}; public static gamestate gamestate; public framework () { super(); thread gamethread = new thread() { @override public void run(){ mainloop(); } }; gamestate = gamestate.starting; gamethread.start(); } private void mainloop() { while (gamestate != gamestate.over) { begintime = system.nanotime(); switch (gamestate) { case starting: loadandinit(); break; case mainmenu: mainmenu.update(); break; case playing: break; default: system.out.println("ok, messed up."); break; } repaint(); takentime = system.nanotime() - begintime; sleeptime = (fpstime - takentime) / secinmilli; if (sleeptime < 10) sleeptime = 10; try { thread.sleep(sleeptime); } catch (interruptedexception ex) { } } } private static void loadandinit() { gamestate = gamestate.mainmenu; } public static void draw(graphics2d g2d) { g2d.setcolor(color.black); g2d.drawstring("dearp", 0, 0); }
}
you're adding instance of framework window, framework doesn't override paintcompontents. has static draw method called panel, never create instance of panel.
you convert draw method paintcomponent(), , delete you're panel class.
also should not start thread inside constructor framework. can cause run method see incorrect values member variables. create new start method starts thread.
Comments
Post a Comment