aspose file tools*
The moose likes Applets and the fly likes Problem converting an Applet to a JApplet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Problem converting an Applet to a JApplet" Watch "Problem converting an Applet to a JApplet" New topic
Author

Problem converting an Applet to a JApplet

simon fletcher
Ranch Hand

Joined: Aug 04, 2012
Posts: 50
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.



<html>
<body>
<title> Mondrian </title>
<applet code="Broadway.class" width=300 height=300>
</applet>
</body>
</html>
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42918
    
  68
Instead of overriding paint you now need to override paintComponent; that's one of the differences between AWT and Swing. See http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html and http://www.oracle.com/technetwork/java/painting-140037.html for more detail on that.
simon fletcher
Ranch Hand

Joined: Aug 04, 2012
Posts: 50
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.




Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42918
    
  68
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.
simon fletcher
Ranch Hand

Joined: Aug 04, 2012
Posts: 50
Thanks.

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem converting an Applet to a JApplet