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
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!
Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4 all those years ago...)
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.
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?
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.
Grow a forest with seedballs and this tiny ad:
a bit of art, as a gift, the permaculture playing cards