Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to turn an applet into an application?

4 views
Skip to first unread message

Yang Su

unread,
Aug 15, 1998, 3:00:00 AM8/15/98
to
For example, I have an applet :
class drawing extends Applet implement MouseListener {
....
// try to make this applet serve as application too
public static void main(String[] args) {
Frame frame = new Frame("Drawings");

WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
};

frame.addWindowListener(l);

inAnApplet = false;
drawing applet = new drawing();
applet.init();
frame.add(applet);

frame.pack();
frame.show();
}
}


but I am not successful because without the main() function, it works ok,
but with main() function, the mouse listener action reports error like:
Exception occurred during event dispatching:
java.lang.NullPointerException:
at java.applet.Applet.getAppletContext(Applet.java:146)
at java.applet.Applet.showStatus(Applet.java:186)
at drawing.mouseEntered(drawing.java:196)
....

so there must be something wrong with my main() function, can someone
tell me what I need to modify in order to make the applet work as appliation
too?

Thanks.

--

yang
http://inkle.cs.iastate.edu/~yangsu/

Martin Lovell

unread,
Aug 15, 1998, 3:00:00 AM8/15/98
to
It looks like the applet is trying to call the showStatus method of Applet.
Since there is no browser (or AppletContext instance) there is a null
pointer exception.
In the applet, showStatus should not be called if the applet was run as an
application.

One way to make a quick fix is to override the showStatus method in your
applet:

public void showStatus(String msg) {
if (getAppletContext() != null) super.showStatus(msg);
}

If there is an applet context, the showStatus method will be executed,
otherwise, the super-classes' method will not be called.
A better way to do it would be to set a boolean variable of your applet to
indicate whether it is an application or not, and checking this boolean in
the overridden showStatus method:

public void showStatus(String msg) {
if (!runningAsApplication) super.showStatus(msg);
}

public static void main(String[] args) {

runningAsApplication = true;
//.....
//.....
}

It may also be necessary to call some of the Applet methods in the main
function so that the applet is initialized properly. That is, if your
applet overrides the init, start, or stop method, then it may be necessary
for the main function to call these methods...or the frame can do it
(Perhaps: init in constructor, start when visible, stop when not visible).


Yang Su wrote in message <6r2quh$c0g$1...@news.iastate.edu>...

marcu...@my-dejanews.com

unread,
Aug 17, 1998, 3:00:00 AM8/17/98
to


In article <6r2quh$c0g$1...@news.iastate.edu>,


yan...@cs.iastate.edu wrote:
> For example, I have an applet :
> class drawing extends Applet implement MouseListener {
> ....
> // try to make this applet serve as application too
> public static void main(String[] args) {
> Frame frame = new Frame("Drawings");

etc etc

You need to include more of your code. I guess that the problem is in your
code for MouseListener and you haven't included that. Marcus

>

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp Create Your Own Free Member Forum

0 new messages