This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem converting an Applet to a JApplet

 
simon fletcher
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic