Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Converting an applet to a Panel on a frame  RSS feed

 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having trouble converting a Sun sample from a perfectly working Clock applet to a Clock on a Panel on a JFrame. I thought it would be a simple task, but it seems to be more of a headache that I thought! This is just a Sun sample that I am trying to convert:
working applet:

my feeble attempt

I'm not sure if this is a threading issue or a Swing issue


thanks,

Jamie
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not going to go through all that code, especially considering the lack of a description of the results you're getting, but I can make an observation that may help: An application does not use a Graphics context in the same way that an applet does. Remember, an applet runs within the context of the browser, while an application runs in its own (Swing) or using o/s resources (AWT).

Also, applets have methods that are automatically called at various stages of their existence. The same is not true of an application; you must explicitly call methods in an application.

Good luck!
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most of the time, it's pretty easy to turn an applet into an application. You can just say



This works as long as the applet doesn't try to use its AppletStub or AppletContext, which will be null here. The only problem methods like that that I see here are the getParameter() calls, which might have to be changed (although maybe not; things seem to be written nicely so that they default gracefully on null.)

Anyway, I gave this a try, and it works perfectly for me.
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeff Bosch:
I'm not going to go through all that code, especially considering the lack of a description of the results you're getting, but I can make an observation that may help: An application does not use a Graphics context in the same way that an applet does. Remember, an applet runs within the context of the browser, while an application runs in its own (Swing) or using o/s resources (AWT).

Also, applets have methods that are automatically called at various stages of their existence. The same is not true of an application; you must explicitly call methods in an application.

Good luck!


Leave it to a bartender to post a load of code with little description! In my feeble attempt, I was getting some null pointer exceptions and the frame/panel/clock was not being properly painted. But really, It was more about converting the working applet than fixing my code. I just posted my attempt to show my attempt. Anyways, it looks as though I was missing a call to super.paintComponent( g ). I'll have to comb through the code to see if there is anything else I was missing. Thanks for your help Craig and everyone else.

One last question, does an applet not need to have its paint methods explicitly called?

thanks, Jamie
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can call an Applets paint method if you like. It isn't necessary to call it unless you want to do some custom painting in the Applet. It will paint over all child components. In the AWT we often override the update method; but usually not in Swing.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!