Javascript required
Skip to content Skip to sidebar Skip to footer

How to Compile Applet Program in Java Using Cmd

Java Applets

  • Applets are small Java applications which can be accessed on an Internet server, transported over the Internet, and can be installed and run automatically as part of a web document.
  • The applet can create a graphical user interface after a user gets an applet. It has restricted access to resources so that complicated computations can be carried out without adding the danger of viruses or infringing data integrity.
  • Any Java applet is a class that extends the class of java.applet.Applet.
  • There is no main() methods in an Applet class. Using JVM it is regarded. The JVM can operate an applet application using either a Web browser plug-in or a distinct runtime environment.
  • JVM generates an applet class instant and invokes init() to initialize an applet.

Simple Applet

            import            java.awt.*;            import            java.applet.*;            public class            Simple            extends            Applet  {            public void            paint(Graphics g)   {    g.drawString("A simple Applet",            20,            20);   }  }          

Simple Applet

Every Applet application must import 2 packages - java.applet. & java.awt.

Abstract Window Toolkit (AWT) classes are imported by java.awt.*. Applets communicate (directly or indirectly) via the AWT with the client. The AWT includes support for a graphical user interface based on a window. Java.applet.* imports the Applet package containing the Applet class. Any applet you generate must be an Applet class subclass.

The class in the program must be declared public because code outside of the program will be accessed to it. Every request in Applet must declare a method for paint. AWT class defines this method and the applet must override it. Every moment an applet requires to redisplay its output, the paint() method is called. Another significant thing to notice about the applet implementation is that an applet execution does not start with the main method. In reality, there is no primary/main method in an applet implementation.

Benefits of Applets

  • As it operates on the client side, it requires much less response time.
  • Any browser that has JVM operating in it can operate it.

Applet class

Applet class provides all the support needed to execute applets, such as initializing and destroying applets. It also provides techniques/methods for loading and displaying audio videos and playback pictures.

An Applet Skeleton

These 4 methods are overridden by most applets. These four methods are the lifecycle of the Applet.

  • init() :The first technique to be called is init(). This is where you initialize the variable. This technique is only called once during the applet runtime.
  • start() :Method start() is called after ini(). This technique is called after it has been stopped to restart an applet.
  • stop() :Method stop() is called to suspend threads that do not need to operate when the applet is not noticeable.
  • destroy() : The destroy() technique/method is called if you need to remove your applet from memory entirely.

Note:The stop() method is always called/executed before destroy() method.

Example of an Applet Skeleton

            import            java.awt.*;            import            java.applet.*;            public class            AppletTest            extends            Applet  {            public void            init()   {  //initialization   }            public void            start ()   {  //start or resume execution   }            public void            stop()   {  //suspend execution   {            public void            destroy()   {  //perform shutdown activity   }            public void            paint (Graphics g)   {  //display the content of window   }  }

Example of an Applet

            import            java.applet.*;            import            java.awt.*;            public class            MyApplet            extends            Applet  {            int            height, width;            public void            init()   {    height =            getSize().height;    width =            getSize().width;            setName("MyApplet");   }            public void            paint(Graphics g)   {    g.drawRoundRect(10,            30,            120,            120,            2,            3);   }  }          

Example of an Applet

How to run an Applet Program

In the same manner as you compiled your console programs, an Applet program is compiled. There are, however, two methods of running an applet.

  • Running the Applet in a web browser compatible with Java.
  • Use an applet viewer, like the normal instrument, to view applets. In a window, an applet viewer runs your applet.

Create brief HTML file in the same folder to execute an Applet in a web browser. Include the following code in the file's body tag. (Applet tag loads class Applet).

< applet code = "MyApplet" width=400 height=400 >  < /applet >          

Run the HTML file HTML file

Running Applet using Applet Viewer

Write a brief HTML file as mentioned above to run an Applet with an applet viewer. If you name it as run.htm, your applet program will operate the following command.

f:/>appletviewer run.htm          

Applet using Applet Viewer

How to Compile Applet Program in Java Using Cmd

Source: https://www.knowledgehut.com/tutorials/java-tutorial/java-applet