Showing posts with label Flickering. Show all posts
Showing posts with label Flickering. Show all posts

Wednesday, June 18, 2008

The Flickering Problem

Flickering is a big problem which occurs while drawing on screen using Java.
Though its a big problem, there is a possibility that many people may not have been exposed to this menace.
Flickering occurs normally when drawing images one after another, and can also be seen in another form in case of Swing Components coming from nowhere in case of JLayeredPane coding.
While the origin of Flickering is given to the inability to repaint the screen by the JVM when needed, and more so because of the difficulty in understanding how the repaint calls actually work going from one component to another.
Sometimes its more good to know what can be done to avoid Flickering, than to know how Flickering happens.
The Flickering though not a correct term in case of JLayeredPane is a different case alltogether,
and is out of scope to discuss here.
So i intend to limit my discussion to basic Flickering occuring while drawing images on screen.
One method which is used heavily is to override the paintComponent method-------
public void paintComponent(Graphics g)
{ super.paintComponent(g);
g.drawImage(img,0,0,null);
}
in case you are painting an image referenced by "img" variable.
This way of code works very well for me.
Another thing which can be kept in mind while drawing images on screen is, draw the image on a JPanel...which you have to add to your JFrame.
Once you change the image, remove the JPanel from the JFrame and create a new JPanel with new image and add to your JFrame.....
And most importantly, dont forget to call pack() of your JFrame and repaint() method of your JPanel and JFrame.
These are the precautions which can be used to avoid Flickering but you may still see it somewhere.
As it depends also on the way you are displaying and handling your components, have a relook at your code , try to keep in mind the above precautions and yes, be prepared for some code tweaking and use of different approaches, as handling of Swing components is not all that easy,
but certainly not very difficult as well!!

Saturday, June 14, 2008

Image Handling in Java

Image Handling is a very vast concept in Java which begins from display of images to processing of images, but everybody likes to work with images so i started studying about the subject. Possibly for everybody the starting thing is how to show an image on screen??
So in that category, first came the ImageIcon, the most basic ones and offered very little to tweak. Then it was the turn of Image class...it was good till i heard about BufferedImage class, a derived class from Image class offering better performance most importantly.
Even Image class will work if its just about display of images. But if someone wants a better performance in terms of images, BufferedImage is definetly needed to be used....it offers Buffering of image data and the whole image can be directly transferred to screen thus improving performance. Infact if we first draw an offscreen image and draw the offscreen image on screen, thus using the much talked DoubleBuffering concept, visible distortions will be removed drastically, which we can see otherwise many a times.
As i talk about Double Buffering, its important to talk about Flickering.
I really need to tell here, it was a dreaded thing for me as i tried hard to remove the flickering of images. Infact Flickering is a pretty general term which can be seen not just in Images drawing but also in Swing components sometimes as well.
As its a big problem, I will talk about Flickering in my next post in order to elaborate on it!!

Share this Page:-