Double Buffering

Hi guys,I am trying to animate several objects at once but I am getting the famous flickering problem. ~Can any one please abvice me on how to conquer this problem.
[178 byte] By [wise_Kida] at [2007-11-26 17:40:35]
# 1
Use Swing. It double buffers by default. You might also want to use full-screen exclusive mode: http://java.sun.com/docs/books/tutorial/extra/fullscreen/index.html
DrLaszloJamfa at 2007-7-9 0:08:46 > top of Java-index,Java Essentials,Java Programming...
# 2

if you are using an applet you should declere two instance fields an Image and a Graphics, then override the update method.

Something like that

Image offImage;

Graphics offGraphics;

public void update(Graphics g)

{

paing(g)

}

public void paint(Graphics g){

if(offImage==null)

{

offImage=createImage(100,100);

offGraphics=offImage.getGraphics();

}

else

{

offGraphics.setColor(getBackground());

offGraphics.fillRect(0,0,getWidth(),getHeight());

offGraphics.setColor(Color.BLACK);

}

offGraphics.fillRect(10, 10,20,20);

//Simply draw the stuff to the offGraphics

//then draw the image

g.drawImage(offImage,0,0,this);

}

then since the image is drawn as a whole to the screen it will not make flickers.

dagcilibilia at 2007-7-9 0:08:46 > top of Java-index,Java Essentials,Java Programming...
# 3

By the way how can we have no flickering when using JApplet withot doing this double buffering (i mean it says it double buffers automatically but it still flickers with a code like that)

public class denemeJ extends JApplet implements Runnable

{

Thread t;

int x;

public void init()

{

x=0;

t=new Thread(this);

t.start();

}

public void paint(Graphics g)

{

g.clearRect(0,0,getWidth(),getHeight());

g.setColor(Color.BLACK);

g.fillRect(x, 10,20,20);

}

public void run()

{

while(true)

{

repaint();

x++;

try

{

t.sleep(20);

}

catch(Exception e)

{System.out.println(e.getMessage());}

}

}

}

dagcilibilia at 2007-7-9 0:08:46 > top of Java-index,Java Essentials,Java Programming...
# 4

Does this flicker? (I don't do applets.)

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

class Demo extends JComponent implements ActionListener {

private static final long serialVersionUID = 0;

private static final int BOX_W = 10;

private static final int BOX_H = 10;

private int x=0, y=0, dx=2, dy=3;

private Timer timer = new Timer(20, this);

public Demo() {

setPreferredSize(new Dimension(200,307));

timer.start();

}

public void actionPerformed(ActionEvent evt) {

int w = getWidth(), h = getHeight();

if (x < 0 || x + BOX_W> w)

dx = -dx;

if (y < 0 || y + BOX_H> h)

dy = -dy;

x += dx;

y += dy;

repaint();

}

protected void paintComponent(Graphics g) {

super.paintComponent(g);

g.fillRect(x, y, BOX_W, BOX_H);

}

}

public class Launcher implements Runnable {

public static void main(String[] args) {

EventQueue.invokeLater(new Launcher());

}

public void run() {

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(new Demo());

f.pack();

f.setLocationRelativeTo(null);

f.setVisible(true);

}

}

DrLaszloJamfa at 2007-7-9 0:08:46 > top of Java-index,Java Essentials,Java Programming...
# 5

> By the way how can we have no flickering when using

> JApplet withot doing this double buffering

No flicker:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Jenemed extends JApplet

{

public void init() {

setContentPane(new Display().start());

}

private class Display extends JPanel {

private int x;

Display start() {

ActionListener displayUpdater = new ActionListener() {

public void actionPerformed(ActionEvent e) {

x++;

Display.this.repaint();

}

};

new Timer(20, displayUpdater).start();

return this;

}

public void paintComponent(Graphics g) {

g.clearRect(0,0,getWidth(),getHeight());

g.setColor(Color.BLACK);

g.fillRect(x, 10,20,20);

}

}

}

~

yawmarka at 2007-7-9 0:08:46 > top of Java-index,Java Essentials,Java Programming...
# 6
thanks, i haven't seen an animation using timer class before, this way actually seems neater. beautiful...
dagcilibilia at 2007-7-9 0:08:46 > top of Java-index,Java Essentials,Java Programming...