LiveWire Network Peer Answers Peer Support Teen Forums Tech Forums College Forums 834 users online 158068 members 2037 active today Advertise Here Sign In
TeenCollegeTechPhotos | Quizzes | LiveSecret | Video | Dictionary | News | FAQ
You have 1 new message.
Emergency Help
Until you sign up you can't do much. Yes, it's free.

Sign Up Now
Membername:
Password:
Already have an account?
Invite Friends
Active Members
Groups
Contests
Moderators
7 online / 33 MPM
Fresh Topics
  LiveWire / Technical Forums / Programming & Application Development / Viewing Topic

Java tip
repainting + gui's
Replies: 2Last Post Nov. 16, 2004 11:24pm by Suke
Single page for this topic Email Print Favorite
( Suke )


Technician
Reply
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)


10:39 pm on Oct. 25, 2004 | Joined Oct. 2004 | 23 Days Active
Join to learn more about Suke Ohio, United States | Straight Male | 79 Posts | 309 Points
sakurag


Dairy Product Addict

Ad Free
Reply
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.  

-------
I've got spurs that jingle jangle jingle.
As I go riding merrily along.
And they sing, "Oh, ain't you glad you're single?"
And that song ain't so very far from wrong.


9:15 am on Oct. 26, 2004 | Joined Oct. 2002 | 460 Days Active
Join to learn more about sakurag Washington, United States | Straight Male | 1892 Posts | 8074 Points
( Suke )


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

11:24 pm on Nov. 16, 2004 | Joined Oct. 2004 | 23 Days Active
Join to learn more about Suke Ohio, United States | Straight Male | 79 Posts | 309 Points
Single page for this topic Email Print Favorite

Quick Reply

You are signed in as our guest.

Looking for something else?
 

  LiveWire / Technical Forums / Programming & Application Development / Viewing Topic