LiveWire Peer Support Network

Printable Version of Topic "Java tip"

- LiveWire Teen Forums & College Forums (http://www.golivewire.com)
-- (http://www.golivewire.com/forums/support-technical.html)
--- Programming & Application Development (http://www.golivewire.com/forums/forum-211-s-0.html)
---- Java tip (http://www.golivewire.com/forums/peer-pesyb-support-a.html)


-- Posted by Suke at 10:39 pm on Oct. 25, 2004

Alright I'm not sure how many people will find this useful but I am sharing a tip on how to properly repaint graphics to a panel when stuff happens to your gui.  For example, when it get's resized, minimized, maxmized, moved over, etc etc.. (if you don't do this, your graphics will simply disappear or flash when you move it)

All you need to do is extend the JPanel class and override the paintcomponent method in it to also call the function to redraw your graphics like so.

Code:

//class ensures proper repaint when stuff happens to panel
public class TreePanel extends JPanel
{
    public TreePanel()
    {
         setBackground(Color.white);
    }
    //override paintComponent method in JPanel
    public void paintComponent(Graphics g)
    {
         super.paintComponent(g); // call the super's paint method as normal
         Graphics2D g2 = (Graphics2D)g; // create a new graphics object
         pq.draw(g2, getWidth()); //repaint tree
    }
}//end TreePanel class

You can find a copy of this program at My prog

Open PQGui and look for the above code, then run it and see how it behaves.

Let me know if this helps you, or if everyone already knows it or a better way..

(Edited by Suke at 12:49 am on Oct. 26, 2004)


-- Posted by sakurag at 9:15 am on Oct. 26, 2004

That's a very neat program.
I've seen that kind of thing done before with trees.  Although I'm not really into writing java apps, that does give me some insight on how to do things.

We have the same idea in Win32.  Though not quite double buffering(unless it happens under the covers), does this really reduce the flickering?  I guess the Graphics object is pretty powerful.

Along these lines, I'd like to add my little tip about flicker reduction, double buffering, etc.

Double buffering is a good idea in a lot of applications.  It's almost a staple when using any sort of graphics.  But be very careful about using it.  Do not double buffer everything, unless you really need to.  Take some time to calculate which area needs to be redrawn, and redraw only it.  This can save a lot of computing power, and allow for smoother repaints, which may or may not be to your liking.  


-- Posted by Suke at 11:24 pm on Nov. 16, 2004

You should go back and comment out that class I overrided and watch how it doesn't work the at all.


www.golivewire.com