Hi!
I'm making an applet which uses AWT. I'm using double buffering so
normal images won't flicker. I have made my own class ImageButton
which extends canvas. Every time ImageButton is drawn it flickers.
What i should do that my applet won't flicker?
Here is code of ImageButton:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class ImageButton extends Canvas
{
private Image img;
private String teksti="";
private int vaaka=40,pysty=20;
Gfx gfx;
Color vari= new Color(64, 76, 130);
/**
* ImageButton(Image initImg)
*
* Constructor.
*/
public ImageButton(Image initImg)
{
img = initImg;
setSize(img.getWidth(this), img.getHeight(this));
}
public ImageButton(Image initImg, String teksti)
{
img = initImg;
setSize(img.getWidth(this), img.getHeight(this));
this.teksti=teksti;
}
public ImageButton(String teksti){
this.teksti=teksti;
}
/**
* int getWidth()
*
* Returns the width of the ImageButton (width of Image).
*/
public int getWidth()
{
return getSize().width;
}
/**
* int getHeight()
*
* Returns the height of the ImageButton (height of Image).
*/
public int getHeight()
{
return getSize().height;
}
/**
* void paint(Graphics g)
*
*/
public void paint(Graphics g)
{
g.setColor(vari);
Font f = new Font("Helvetica", Font.BOLD, 14);
g.setFont(f);
g.drawImage(img, 0, 0,this);
g.drawString(teksti,vaaka,pysty);
}
}
J L Timonen
Babu Kalakrishnan - 29 Aug 2003 19:16 GMT
> Hi!
>
> I'm making an applet which uses AWT. I'm using double buffering so
> normal images won't flicker. I have made my own class ImageButton
> which extends canvas. Every time ImageButton is drawn it flickers.
> What i should do that my applet won't flicker?
[code snipped]
You aren't really using double buffering at all (going by the code you
posted).
To perform real double buffering :
You create a BufferedImage corresponding to what is to be displayed on the
object.
All your display modifications are done on that BufferedImage - never
directly on the actual Graphics object that represents the screen.
And all you do in the paint method is to repaint the image on to the
Graphics object that is passed to you.
If you take a second look at your code, you'll realize that you're
writing directly to the Graphics object (the DrawString call), so the
whole purpose of double-buffering is defeated.
Look at the examples in the java tutorial for examples for how to do
it right.
BK