java - Why does my applet flash for every repaint? -
i found java game code online , trying modify it. converted jframe applet, game started blink every time repaint screen. tried double buffering no difference.
source:
private void paintdisks(graphics g) { try { (disk d : disk) paintdisk(g, d); } catch (exception ex) { //system.out.println(ex.getmessage()); paintdisks(g); // retry disks never not painted } } private void paintdisk(graphics g, disk d) { if (d == null) return; if (diskimg[d.player] == null) { g.setcolor(colour[d.player]); g.filloval((int)d.x - 1, (int)d.y - 1, 32, 32); } else { g.drawimage(diskimg[d.player], (int)d.x - 1, (int)d.y - 1, 32, 32, this); } } @override public void paint(graphics g) { // paint real panel stuff super.paint(g); graphics gr; if (offscreenbuffer==null || (! (offscreenbuffer.getwidth(this) == this.size().width && offscreenbuffer.getheight(this) == this.size().height))) { offscreenbuffer = this.createimage(size().width, size().height); } gr = offscreenbuffer.getgraphics(); gr.clearrect(0,0,offscreenbuffer.getwidth(this),offscreenbuffer.getheight(this)); // paint disks paintdisks(gr); // paint curser ontop of disks paintcurser(gr); g.drawimage(offscreenbuffer, 0, 0, this); } @override public void run() { while (true) { repaint(); try { thread.sleep(9, 1); } catch (interruptedexception ex) { system.out.println(ex.getmessage()); } } } }
short answer: not call super.paint()
in board.paint()
method.
long answer: applet container own display properties including background color set via setbackground(color.white);
part of constructor. invoking super.paint(g)
causing applet paint white background display graphics, invoke contained component painting. cause of flicker - each paint cycle, painting on-screen display white copying offscreenbuffer
image on-screen display.
probably best explicit, forget applet background, remove super.paint(g)
, , paint steps yourself. you'll need replace clearrect()
setcolor()
, fillrect()
.
also should implement update()
well.
@override public void update(graphics g) { paint(g); } @override public void paint(graphics g) { // no super.paint(g) if (offscreenbuffer==null || (! (offscreenbuffer.getwidth(this) == this.size().width && offscreenbuffer.getheight(this) == this.size().height))) { offscreenbuffer = this.createimage(size().width, size().height); } graphics gr = offscreenbuffer.getgraphics(); // blank canvas gr.setcolor(color.white); gr.fillrect(0,0,offscreenbuffer.getwidth(this),offscreenbuffer.getheight(this)); // paint disks paintdisks(gr); // paint curser ontop of disks paintcurser(gr); g.drawimage(offscreenbuffer, 0, 0, this); }
Comments
Post a Comment