简体   繁体   中英

How to call an applet from another class's main method?

Bear with me as I am not well versed in how Applets work in java. I have created a video game that runs in an applet, with other game object classes and image files in the package directory. I want to create an executable .jar to run the applet, but eclipse is asking for a launch configuration, so I assume I would need another class that deals with initializing the applet and anything else the applet might need.

How would I go about doing this?

Those are two different concepts:

  1. A normal java program started by a main() method (it can be executed by calling a class, an executable jar, an exe file - there are lots of possibilities...)
  2. A Java applet running in a web browser, managed by the java plugin of the web browser and subject to the applet lifecycle .

So basically you have to decide, what is important to you:

  1. Do you want to run your game inside a webbrowser? (-> then write an applet)
  2. Do you want a standalone application? (in form of class files, a runnable jar or exe)
  3. Do you want to have both? There are also ways to do that:
    • If you implemented an applet, then you can also add a main() method and the necessary information to the Manifest . Then when somebody starts your applet.jar your game is also started
    • There is a way to write an draggable, offline applet. Basically you can drag & drop your applet on your desktop and start it by double clicking it - even without internet connection

Applets are Panels (see Applet class extends Panel) that provide a particular life-cycle so that you can attach them to the web-browser and the browser triggers life-cycle methods. If you want to run an applet outside a web-browser you need to provide another "window" to attach it to and a way to trigger life-cycle's methods.

You can have a normal java Frame as "main window" and add the applet to it. Then call applet's init method to start it.

For a more fine grained management of applet's life-cycle you'd probably want to write your own AppletContext.

See my answer here for a basic example (this one uses JApplet but the concept is the same for Applet): Java JApplet to JFrame

Hope it can help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM