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.
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());}
}
}
}
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);
}
}
> 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);
}
}
}
~