java - How to stop an applet from blinking? -
i have been trying convert 1 of games applet since first time using applet ran problem. whenever open game screen start blinking. i'm pretty sure due not having bufferstrategy whenever try create 1 "bufferstrategy bg = this.getbufferstrategy()" doesn't work. can help?
package main; import java.awt.color; import java.awt.graphics; import javax.swing.japplet; import handling.handler; import other.hud; import other.keyinput; public class game extends japplet implements runnable{ private static final long serialversionuid = 9021342660060318393l; public static final int width = 640; public static final int height = width / 12 * 9; private thread thread; private boolean running = false; private handler handler; private hud hud; private spawner spawner; private menu menu; public enum state{ menu(), help(), settings(), deathscreen(), game(); }; public enum difficulty{ easy(), mediuŠ¼(), hard(); }; public state gamestate = state.menu; public difficulty difficulty = difficulty.easy; private void tick(){ handler.tick(); if(gamestate == state.game){ hud.tick(); spawner.tick(); }else if(gamestate == state.menu){ menu.tick(); } } public void paint(graphics g){ //draw here g.setcolor(color.black); g.fillrect(0, 0, width, height); handler.paint(g); if(gamestate == state.game){ hud.paint(g); }else if(gamestate == state.menu || gamestate == state.help || gamestate == state.settings || gamestate == state.deathscreen){ menu.paint(g); } //stop drawing g.dispose(); } public void init(){ setsize(width,height); handler = new handler(); menu = new menu(this, handler); this.requestfocusinwindow(true); this.setfocusable(true); this.addkeylistener(new keyinput(handler)); this.addmouselistener(menu); hud = new hud(); spawner = new spawner(handler, hud, this); } public void start(){ thread = new thread(this); thread.start(); running = true; } public void destroy(){} public void stop(){ try { system.exit(1); thread.join(); running = false; } catch (interruptedexception e) { e.printstacktrace(); } } public void run() { this.requestfocus(); long lasttime = system.nanotime(); double amountofticks = 60.0; double ns = 1000000000 / amountofticks; double delta = 0; long timer = system.currenttimemillis(); int frames = 0; while(running){ long = system.nanotime(); delta += (now - lasttime) / ns; lasttime = now; while(delta >= 1){ tick(); repaint(); delta--; } frames++; if(system.currenttimemillis() - timer > 1000){ timer += 1000; system.out.println("fps: " + frames); frames = 0; } try { thread.sleep(20); } catch (interruptedexception e) { e.printstacktrace(); } } stop(); } }
instead of calling repaint(), try call
paint(getgraphics()).
another thing, add method applet:
public void update(java.awt.graphics g) {}
Comments
Post a Comment