This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
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.
Joined: Mar 26, 2002
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.
Joined: Mar 04, 2002
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.