File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Applets and the fly likes Running applets as an application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Applets
Bookmark "Running applets as an application" Watch "Running applets as an application" New topic
Author

Running applets as an application

Mich Robinson
Ranch Hand

Joined: Jun 28, 2009
Posts: 250
    
    1
I'm writing a small game program to run as an applet in a browser however I'd also like to allow it to run as an application. I'm making very, very slow progress on this part and wondered if anyone might be able to help. At the moment I'm running into issues as getCodeBase and getImage are not implemented for applications. I understand I need to replicate these functions but have no idea how to. Can I still keep the normal calls for when the program runs in the browser or do I need 2 sets of code.

There are a whole bunch of reasons for running it as an application but not least I want to analyse the garbage collection while the program is running as I do get the occasional pause. It's a bit frustrating that a language who's main selling point is it's ability to run on any machine can't actually run in a browser and alone on the same machine without recoding. Thanks for reading.

Mike

PS Not sure if a small bit of the code helps:


And the error I'm currently getting:

C:\Java\Game>java MoonBaseAlpha
Exception in thread "main" java.lang.NullPointerExceptio
at java.applet.Applet.getCodeBase(Unknown Source
at MoonBaseAlpha.init(MoonBaseAlpha.java:213)
at MoonBaseAlpha.main(MoonBaseAlpha.java:1390)


Arcade : Alien Swarm
Board : Chess - Checkers - Connect 4 - Othello
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42061
    
  64
This may help: http://faq.javaranch.com/java/AppletsFaq#application


Ping & DNS - my free Android networking tools app
Mich Robinson
Ranch Hand

Joined: Jun 28, 2009
Posts: 250
    
    1
Thanks for getting back to me so soon and that code does look exactly like what I'm after. I haven't tried producing anything with more than one class in it and I'm a little confused by how to go about things. I also haven't tried overwriting methods before (or at least I don't think I have).

  • Do I need to use java packages to run the 2 classes (my original applet and the new main class)?
  • Can I leave the existing applet code alone and let the new main class handle all the differences?
  • Do I need to use packages at all? could I place this class in with my existing applet class and just make one of them private? Not sure how this would work when calling from a browser and as an application.
  • Sadly - I've been trying to understand how to implement my applet


  • PS can I find out about the garbage collection through the appletviewer program? I know I can call something like the command below for an application:


    Cheers Mike
    Ulf Dittmer
    Marshal

    Joined: Mar 22, 2005
    Posts: 42061
        
      64
    Can I leave the existing applet code alone and let the new main class handle all the differences?

    Yes. The source code of the MainFrame class tells you how to change its main method so that it starts the applet properly.

    Do I need to use packages at all? could I place this class in with my existing applet class and just make one of them private? Not sure how this would work when calling from a browser and as an application.

    You don't need packages. Nothing would change as far as the applet is concerned; in fact, the MainFrame class doesn't need to be deployed in the browser at all - it'll only get used if you want to run the code as an application. (And no, you shouldn't make either of them private.)

    Word of advice about the MainFrame class, though: it's quite old, and thus reflects a bygone era of Java coding. You'll get obsolescence warnings about the "handleEvent" method, and you'll need to add implementations of the methods getStream, setStream and setStreamKeys - those were added to the AppletContext interface after this class was written. If you don't know what those methods do, just provide empty implementations.

    can I find out about the garbage collection through the appletviewer program?

    Yes, you can run the appletviewer like "java sun.applet.AppletViewer myPage.html", and then use all the usual command line options with it.
    Mich Robinson
    Ranch Hand

    Joined: Jun 28, 2009
    Posts: 250
        
        1
    Thank you - you've been very helpful. It might take me a while to digest the info and try these things out.

    Mike
    Mich Robinson
    Ranch Hand

    Joined: Jun 28, 2009
    Posts: 250
        
        1
    I took quite a few attempts to work out how to pass the options to the appletviewer so I thought I should show how it's done here just in case anyone else wanted to see what the garbage collection was doing while running an applet.


    I couldn't work out how to run the MainFrame class along with my applet class so I could run my applet as both an applet and as an application. This question does seem to pop up a lot on the java forums but the solutions always are different and I couldn't get any to run. I don't think I have enough experience to get your code sample to work with my code

    I'm just not sure how to run 2 classes together to do what I need.

    Mike
    Ulf Dittmer
    Marshal

    Joined: Mar 22, 2005
    Posts: 42061
        
      64
    OK, you got me interested in the MainFrame class again. I posted an up-to-date version at http://test.javaranch.com/ulf/MainFrame.java. All you need to do is to replace the applet class name I used ("SimpleMandel") with your applet class name (apparently "MoonBaseAlpha"). As long as both class files are in the current directory, you can then start the applet running as an application via "java MainFrame".

    If the applet needs parameters, you can pass those on the command line as described in the comments in the MainFrame.java file.
    Mich Robinson
    Ranch Hand

    Joined: Jun 28, 2009
    Posts: 250
        
        1
    That worked much better and I could see my applet in the window. I did have a few issues though and thought you might be interested in what occurred :

  • line 252 has a typo and needs to say ImageProducer
  • line 67 should indicate that the users class goes there
  • compiling the class produces a warning
    MainFrame.java:244: warning: sun.applet.AppletAudioClip is Sun
    nd may be removed in a future release
    return new sun.applet.AppletAudioClip( url );
  • AudioClip play method throws an error when called.
  • I use the following method to help display using double buffering and this throws an error on paint
    public void update(Graphics g) {
    paint(g);
    }
  • It wouldn't put an image onto the screen and this may be due to the getImage, the drawImage or the getCodeBase. If it's the getCodeBase then that might also be why the sounds aren't working. Perhaps I need to supply some directory to the application or does it work this out?
  • Oddly when killing the window the command prompt didn't appear (in XP) and I needed to terminate the java.exe process.


  • It looks to be very close but I'm guessing that most of these issues are simply because I'm relying on applet methods when running as an application

    Mike

    PS if you want to see the applet in action (it's a retro style shooter) then feel free ( Moon Base Alpha ). It kind of passes the time.
    Ulf Dittmer
    Marshal

    Joined: Mar 22, 2005
    Posts: 42061
        
      64
  • line 252 has a typo and needs to say ImageProducer
  • line 67 should indicate that the users class goes there

  • I've fixed those two.

    compiling the class produces a warning

    Yeah, one really shouldn't use sun.* classes, but the newer javax.sound stuff doesn't provide a way to get an AudioClip object.

    As regards the other problems, you might want to do some proper exception handling - many catch blocks just fail silently (getImage, getCodeBase, getDocumentBase). If you print out the stack trace instead you might see what's going wrong, and possibly be able to fix it. I would assume that images and sounds need to be in the same directory as the class files, or a subdirectory thereof.
    Mich Robinson
    Ranch Hand

    Joined: Jun 28, 2009
    Posts: 250
        
        1
    Thank you for all your help Ulf. I know it's a bit outside the scope of the original question but why did they design the language like this? Wouldn't it of been simpler all round just to provide the same methods no matter what computer / OS or browser you're running in. The method getCodeBase() could just return the current directory when called from an application, getImage() would just return an image play() would just play a sound. I just can't see what was going through their minds when they designed the language this way. Come to think of it I can't see why you need applets and applications - wouldn't it be possible to run an application in a browser and just apply more security restrictions when run this way. Perhaps I'm missing something and the answer is really simple.
    Ulf Dittmer
    Marshal

    Joined: Mar 22, 2005
    Posts: 42061
        
      64
    There's no easy answer to that. Some of it has to do with the fact that the language designers didn't have an idea of how Java would be used when they designed it (the language was conceived for embedded applications, so neither desktop applications nor applets were part of the original plan). Some of it has to do with the fact that there are many ways to run Java code -desktop apps, applets, servlets, EJBs- with different requirements for the container to manage the code's lifecycle, so a one-size-fits-all approach isn't possible. Still another reason -specifically for applets-vs-applications- is that applets are loaded over the network, and so are their resources like images and sounds; whereas a desktop app can retrieve those instantaneously, applets need to allow for the fact that there can be a noticeable network delay.
    Mich Robinson
    Ranch Hand

    Joined: Jun 28, 2009
    Posts: 250
        
        1
    It doesn't look at all straightforward to run the applet as a standalone application when using the above applet methods (no surprise I guess). I was thinking about producing a separate class for the game, using a modified version of your class for the application and yet another for the applet but it just seems like too much effort for a free game!

    I did manage to get the garbage collection info earlier so that aspect is all solved. I might look at using trusted applets which would allow me to store high scores on a users computer. Most likely I'll just ignore the application side and stay with the applet. It leaves me with a smaller code base and that's got to be easier.

    Again thank you for all your help and advice.

    Mike
    Ulf Dittmer
    Marshal

    Joined: Mar 22, 2005
    Posts: 42061
        
      64
    I was thinking about producing a separate class for the game, using a modified version of your class for the application and yet another for the applet

    Don't shy away from using multiple classes. Any decent-sized Java project will have dozens of classes in multiple packages, so that's something that you'll need to get comfortable with.
    Regina Thomas
    Greenhorn

    Joined: May 12, 2006
    Posts: 16
    C:\Java\Game>java MoonBaseAlpha
    Exception in thread "main" java.lang.NullPointerExceptio
    at java.applet.Applet.getCodeBase(Unknown Source
    at MoonBaseAlpha.init(MoonBaseAlpha.java:213)
    at MoonBaseAlpha.main(MoonBaseAlpha.java:1390)


    This error occurs when you are running from most IDE's or if the resource you are looking for is not located in the same directory as the class or jar file.

    Q...


    WiredPages | Remix
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: Running applets as an application