This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Who�s repaint() method calling to? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Who�s repaint() method calling to?" Watch "Who�s repaint() method calling to?" New topic
Author

Who�s repaint() method calling to?

Sigfred Zamo
Ranch Hand

Joined: Mar 04, 2002
Posts: 44
Hello everybody:
If:

having this applet:

Why is the inner class�s paintComponent method invoked with the repaint() method call in my applet�s run() method?
Thanks in advance.


Sigfred Zamo<br />Software Development<p><a href="http://www.sigfredo.ingenieros.net/index2.html" target="_blank" rel="nofollow">My page</a>
Dave Wingate
Ranch Hand

Joined: Mar 26, 2002
Posts: 262
The paint method of your inner class is invoked because you've added an instance of the inner class as the content pane of your outer class.
setContentPane(new AppletContentPane());
When you call repaint() on a container, there is an implicit call made to repaint all components contained inside your container. In your case, you're calling repaint() on the RaceApplett, which contains an instance of AppletContentpane (your inner class). Hope that helps.


Fun programming etcetera!
Sigfred Zamo
Ranch Hand

Joined: Mar 04, 2002
Posts: 44
I don�t know anything about swing or applets yet, I thought I could obtain a more simple answer..
I studied classes, interfaces, inheritance, nested clasess..
Would it be possible a more straightforward explanation?
Thanks a lot.
Dave Wingate
Ranch Hand

Joined: Mar 26, 2002
Posts: 262
I'll have a try. I'm not sure how simple of an answer you're looking for, but here goes.
If you are new to Appletts, one way to think of an Applett is as a container. In fact there is a java class Container from which Applett inherits certain properties. So an Applett is a Container.
To understand the import of the statement above, you need to realize that Containers contain components. Examples of components include buttons, labesl, and panels.
When your code states
setContentPane(new AppletContentPane());
what you are doing, more or less, is adding a panel to you Applett container.
If the above made sense and you understand that your outer class is a container that contains an instance of your inner class, then you can go on to consider how these two objects are drawn on the screen.
Both the Applett class and the JPanel class have paint methods that are responsible for depicting those classes. When your code calls repaint(), this is essentially a way of saying "Hey, Applett, you need to draw yourself again." In order for the Applett to repaint itself correctly, it must also invoke the paint method of the object that it contains--the JPanel object.
Is that helpful? If not, you might consider going to http://java.sun.com/docs/books/tutorial/applet/overview/index.html.
Sigfred Zamo
Ranch Hand

Joined: Mar 04, 2002
Posts: 44
Well, I supossed It could be explained somehow with inheritance, talking about clases, and so on.
Sure I�ll understand it better later... When the applets lesson come..
Thanks for your effort.
See you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Who�s repaint() method calling to?
 
Similar Threads
getting warning compiling this program on j2sdk1.5
Need help breaking up this code
help with oval
Scroller Applet & NS4 - getsize() method error
Phasor applet-some prbs already compiled code written here