java - Drawing to canvas -
so dabbling in making game , messy worker , go , tidy up. please ignore stupid naming conventions or methods or anything. go later , tidy up.
so trying create main menu, want draw screen , have remain there. writing graphics image atm , displaying image tried drawing canvas , flickered onto screen , went blank. changed runnable set in sleep() seek , error hunt. i've removed error hunting code now. turns out when have sleep() before or after image set on screen longer 50ms rectangle stays on screen , doesn't flicker away 1ms after drawn.
i'm not sure what's going on. not clear , apologies.
import java.awt.canvas; import java.awt.graphics; import javax.swing.jframe; public class test extends thread{ public static void main(string[] args){ test t = new test(); t.start(); } @override public void run() { jframe f = new jframe("test"); canvas c = new canvas(); graphics g; c.setsize(400, 400); f.add(c); f.pack(); f.setvisible(true); c.setvisible(true); seek { sleep(2000); } grab (interruptedexception e) { // todo auto-generated grab block e.printstacktrace(); } g = c.getgraphics(); g.drawrect(10, 10, 40, 68); } }
basically, not how painting in awt works. painting should done within context of component's paint method. in awt canvas seems preferred base of operations component use.
you should never utilize getgraphics, apart been able homecoming null, represent graphics context lastly paint cycle, added wiped newer paint events...which happening you.
take @ painting in awt , swing details how painting works.
having said that, discourage using awt based api replaced swing 15 years ago.
take @ performing custom painting more details painting in swing
java image jframe sleep
No comments:
Post a Comment