This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
I have code from an oldish book called The Black Art Of Java Game Programming. It creates an Applet that basically has a bunch of coloured rectangles (representing a piece of modern art) with a grey square that dances in the middle. If I turn it into a JApplet (by changing "extends Applet" to "extends JApplet" as well as including import javax.swing.*; and import javax.swing.JApplet.*;) everything works the same except that the grey box doesn't repaint itself properly. Grey still remains where the square was before the repaint. I am wondering if anyone can give me a hint as to where this difference comes from. Following is the java code and after that is the html code used to call the applet.
Does JApplet use paintComponent()? I don't see it listed in the API under JApplet methods. I'm thinking maybe I can add a JComponent to the JApplet and override it's paintComponent() method. I'll see if this will work...
...And, it worked!
I created a nested class called BroadwayPanel inside Broadway, overriding its paintComponent() method and not overriding the JApplet's method at all:
and also created and added an instance of it to Broadway.
Joined: Mar 22, 2005
Sorry for the confusion - yes, that's different about JApplet when compared to Applet (or actually for all Swing top-level components compared to AWT components): generally you add something that extends JPanel to its content pane, and do the drawing on that (or add additional components to it). Good to hear you got it working.
Joined: Aug 04, 2012
Eventually I found a reference somewhere saying not to draw right on a top level container. Based on that I decided to extend JComponent at first (as in the posted code) then found that I couldn't set the background colour (which, I then discovered, is stated in API) so extended JPanel instead, and of course that worked.