Home » How To, Web

Creating Java Applet

Posted by: 28 April 2010 2 Comments

Hi folks,

Today I’ll be talking about How to create Java Applet. An applet is a program written in the Java programming language that can be included in an HTML page, same as an image or Adobe Flash content.

What can HTML NOT do?
An HTML cannot do the following:

  • Interact with hardware
  • Interact with OS and tasks
  • Interact with hard drive

Also, all the dynamic stuff is restricted to the capability of JavaScript and VBScript. We need client code that runs on the system, can do all of the above and is secure too.An APPLET can do this job.

It is a program written in the Java and it can be included in an HTML page. It requires a Java technology-enabled browser to view a page that contains an applet.The applet’s code is transferred to the client’s system it is executed by the browser’s Java Virtual Machine (JVM). To embed the Java Applet in the web page, tag is used.To make you life easy, start building Applets using NetBeans. NetBeans is a free, easy to use IDE that allows you to quickly create and test Java Applications including an Applet.

  1. Start NetBeans
  2. Create New Project Select Java Application, Next
  3. Uncheck Create Main Class, Finish
  4. Source Package in Projects Tab
  5. Click New, Select Java -> JApplet, Next
  6. Specify Name (Optional), Finish
  7. In New Window write:

  8. import javax.swing.JApplet;
    import java.awt.Graphics;

    /**
    *
    * @author Umair
    */
    public class NewJApplet extends JApplet {

    /**
    * Initialization method that will be called after the applet is loaded
    * into the browser.
    */
    @Override
    public void init() {
    // TODO start asynchronous download of heavy resources
    }

    @Override
    public void paint(Graphics g)
    {
    g.drawRect(0, 0,
    getSize().width – 1, getSize().height – 1);
    g.drawString(“Hello world!”, 5, 15);
    }
    // TODO overwrite start(), stop() and destroy() methods

    }
    Press Shift+F6 to run the file.
    Remember, the paint function is a default function that is overridden here. It uses a Graphics object to draw text on the Applet Surface.

  9. Open Project Directory
  10. Goto build directory
  11. Copy Classes directory
  12. Create New Directory and Paste it in
  13. Create New HTML Page in that Directory
  14. Add Code:

  15. <APPLET codebase="classes" code="NewJApplet.class" width=350 height=200></APPLET>

  16. Save and Run the HTML file

Walaahh !!! You first ever Applet is ready and running.

Follow us on Twitter or join our Facebook fanpage for latest Tech News and Tutorials.

Advertisements
Subscribe to TecRux: Subscribe to RSS Feed or get latest updates in your Inbox.
Note: * Click confirmation link sent in email * Don't see the email, check spam folder


Related Posts

 

Article by

Umair H. has written 131 awesome articles.

If you liked this post, you can follow TecRux on Twitter.

Subscribe to feed via RSS or EMAIL to receive instant updates.

  • Mohsin Ali Khan

    Wallah nice!!!
    I liked it :)

  • http://www.tecrux.com/2010/07/18/8-bit-games-on-pc/ 8 bit Games on PC

    [...] don’t support flash.The games on vNES and Nintendo are made as Java Applets. Know more about Java Applets.Please do let us know how you found these sites.               [...]