java - How to fix bad Double-Buffering -
i tried following double buffering tutorial, , don't know did wrong. works before before did tutorial, there still , occasional flicker here , there. have 2 files game , gameloop
game:
import java.awt.graphics; public class game extends gameloop { public void init() { setsize(854,480); thread th = new thread(this); th.start(); offscreen = createimage(854,480); d = offscreen.getgraphics(); } public void paint(graphics g) { d.clearrect(0, 0, 854, 480); d.drawimage(disk, x, y, this); g.drawimage(offscreen , 0, 0, this); } public void update(graphics gfx) { paint(gfx); } }
gameloop
import java.applet.applet; import java.awt.graphics; import java.awt.image; import java.awt.mouseinfo; import java.awt.point; import java.awt.pointerinfo; import java.awt.event.keyevent; import java.awt.event.keylistener; import java.awt.event.mouseevent; import java.awt.event.mouselistener; import java.awt.event.mousemotionlistener; import java.awt.image.bufferedimage; import java.io.file; import java.io.ioexception; import javax.imageio.imageio; public class gameloop extends applet implements runnable, mouselistener, mousemotionlistener { public int x, y, counter, mousex, mousey; public image offscreen; public graphics d; public boolean up, down, left, right, pressed; public bufferedimage disk1, disk2, disk3, disk4, disk; public int ballspeedx = -6; public int ballspeedy = -3; public void run() { x = 400; y = 200; try { disk1 = imageio.read(new file("disk1.png")); disk2 = imageio.read(new file("disk2.png")); disk3 = imageio.read(new file("disk3.png")); disk4 = imageio.read(new file("disk4.png")); } catch (ioexception e1) { e1.printstacktrace(); } while(true) { if(x >= (854 - 150)) { ballspeedx = ballspeedx * -1; } if(y >= (480 - 140)) { ballspeedy = ballspeedy * -1; } if(y < (0 - 10)) { ballspeedy = ballspeedy * -1; } if(x < (0- 10)) { ballspeedx = ballspeedx * -1; } x = x + ballspeedx; y = y + ballspeedy; counter ++; if(counter >= 4) counter = 0; if(counter == 0) disk = disk1; if(counter == 1) disk = disk2; if(counter == 2) disk = disk3; if(counter == 3) disk = disk4; system.out.println(counter); repaint(); try { thread.sleep(30); } catch (interruptedexception e) { e.printstacktrace(); } } } public void mouseclicked(mouseevent e) {} public void mouseentered(mouseevent e) {} public void mouseexited(mouseevent e) {} public void mousemoved(mouseevent m) {} public void mousepressed(mouseevent m) { } public void mousereleased(mouseevent m) { pressed = false; } public void mousedragged(mouseevent e) { pointerinfo = mouseinfo.getpointerinfo(); point b = a.getlocation(); mousex = (int)b.getx(); mousey = (int)b.gety(); ballspeedx = mousex; ballspeedy = mousey; } }
public void update(graphics gfx) { paint(gfx); }
should lower case, you're trying override update(graphics g)
method in applet.
so should be
@override public void update(graphics gfx) { paint(gfx); }
as changing background, background big rectangle covers screen , makes color. in paint
, you're doing clearrect
, clears screen. instead switch fillrect
after setting color.
it might like
public void paint(graphics g) { //setcolor whatever want //fillrect cover screen }
when you're doing though, 1 thing have remember not confuse 2 graphics objects. concept, double buffering works drawing memory first (drawing on offscreen image) , screen. want draw onto offscreen image, faster (and lose flicker).
so make sure you're doing imagegraphicsobject.setcolor
not screengraphicsobject.setcolor
or imagegraphicsobject.fillrectnot
screengraphicsobject.fillrect`. otherwise you're no longer double-buffering.
Comments
Post a Comment