Showing posts with label Swing. Show all posts
Showing posts with label Swing. 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!!

Thursday, June 12, 2008

My Experiences with Java GUI and Swing

My experiences with Java regarding its GUI arose out of some serious coding in Swing. As i went deep into some serious stuff coding in Java, i always wanted to try out new features in Java ,in which i had little knowledge. After trying out basics of working of Java, i jumped into programming of Swing, as GUI coding always looked fascinating to me!!
But certainly, i had no clue about the dangerous territory where i was heading.
This was the fact i realized, when i started coding the Swing stuff.
The elements just never looked to obey my orders in the code. It was a real hard work to really control the looks of GUI. Then learning the Layouts, the rules which the components follow, and combining them with Event Handling Functionalities..
My first real GUI program with a bit of good coding was to display a list of files and directories in a user selected directory from the JFileChooser class, all in a GUI format. Initially i felt whats the big deal in it, but for a beginner, it was certainly a bit hard. But as i got success in it, my confidence grew in coding GUI programs.
Certainly that confidence helped me alot to take up more challenging coding in GUI and Image Handling in Java, which ultimately helped me to learn more about the way Java works.......

Share this Page:-