Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

Repainting Panels When Necessary

Thread view: 
Raous - 17 Nov 2005 06:05 GMT
Hello,
I created a program which draws elipses on some panels.

When I activate another window such as a Web Browser and
return(re-activate) to my program,
the graphic objects of mine remain disappeared.

However, I found the other objects such as JButton and JLabel are
recovered and shown.

I am finding an event like WM_PAINT (in Visual C++), which occured when
the object is needed to refresh.

Although I put command such as validate() / repaint() in
"ComponentShown" of each Panel and its parent Pane, and in
"AncestorShown", I couldn't find appropriate command and position yet.

Could anyone help me?
Norb - 17 Nov 2005 07:45 GMT
In which method are you doing your painting?

If you use Swing, put your painting code in paintComponent, and you
don't have to care about repainting when the window is hidden,
minimized or whatever. Just call repaint() when your data changes.

Hope that helps
Norbert
zero - 17 Nov 2005 19:19 GMT
"Raous" <chareraous@hotmail.com> wrote in news:1132207548.714495.249500
@g44g2000cwa.googlegroups.com:

> Hello,
> I created a program which draws elipses on some panels.
[quoted text clipped - 14 lines]
>
> Could anyone help me?

You don't need to bother with componentShown, unless you want to do
something funky with your component (as a stupid example, if you want to
play a sound file every time a component is shown, you will need to
implement ComponentListener).  Failing that, just trust java to call the
correct method to paint your component.

For example, you can write a simple subclass of JPanel that will draw an
oval on itself like this:

class OvalPanel extends JPanel
{
  public void paintComponent(Graphics g)
  {
     super.paintComponent(g);
     g.setColor(Color.BLUE);
     g.drawOval(10, 10, getWidht()-10, getHeight()-10);
  }
}

If you're using awt, the above code becomes

class OvalPanel extends Panel
{
  public void paint(Graphics g)
  {
     super.paint(g);
     g.setColor(Color.BLUE);
     g.drawOval(10, 10, getWidht()-10, getHeight()-10);
  }
}

As you can see, you don't need to call repaint() or bother with the
ComponentListener interface.  You only need repaint() if you want to
change what is drawn on the panel.

Also, I noticed you said "each Panel", while you were talking about
JButton and JLabel earlier.  Panel is an awt component, while JButton and
JLabel are swing.  While it is theoretically perfectly ok to mix swing
and awt, it's usually not a great idea.  So instead of Panel, use JPanel.

As an aside, WM_PAINT is part of the Windows API, and not specifically
for VC++.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.