Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Working with Applications and Applets in Java

The applet is smaller in size and the application is larger in size. An application is known as a program that runs in an operating system installed in the computer. In this article we will talk about both.

The two types of programs are available in the programming language of java including applications and applets. The applet is smaller in size and the application is larger in size. An application is known as a program that runs in an operating system installed in the computer. The desktop and web based applications can be easy and complex depending on the requirements of the user and the type of functionality which one wants to get from the application. The applications in java are built in the form of classes and each class that has been written in java is represented by .java extension. A Java Applet is a program written in the java programming language and it runs in the web browser, it is a java class and there is no main method defined in this class. The applet can be sent through the internet along with the webpage to the user.

Description:

An application can be executed using the command prompt and on a standalone system while in order to execute the applet the web browser is required and applet can be called as the internet application. An application has the ability to access the data from files and related input from the system but an applet cannot access files and related data present in the system. The desktop based applications run in the desktop of the user and there is no special requirement to have the web browser available in the system.

The web based application need a web browser and they run on the web browser and display the related output and work of an application inside the default web browser present in the system and as selected by the user. The applet is inserted inside the webpage of HTML like the other important images, the related audio and different videos are inserted in the webpage. The applet can be executed from different applications and this small program is particularly designed for the purpose of client side programming.

The applets are the files of small size and very much secure and these are good for the small internet applications which can be accessed from the web browser. The program that has been written in Java and which works on different web-pages is known as the applet.

Overview:

The applications available in java are of many types including desktop based, mobile based, web based applications and many others. There is a main method available in the applications of java. The applications in java are available and can be designed and developed for client side and as well as the server side both respectively. In order to access the functionalities of an applet there is a need to import the java.applet.* package and the java class extends the Applet class that is available inside the package of java. There is also a need to extend this java.awt.* package of java. The web browser should be enabled of java in order to use the required applet.

The life cycle of an applet consists of different methods and these include init (), start (), stop (), paint () and destroy () respectively and these methods are defined in the java.applet.Applet class available in the java.applet package. The methods of init and destroy are called just once in the starting time and ending time respectively. The other methods including start and stop can be called many times depending on the requirements and need.

The init method is called when the applet has successfully loaded first time in the browser and this initialization method is called once in a life cycle of applet and after the successful initialization of applet the user is allowed to interact with the related functionality available in the applet. The running state of applet will get started when start method is called after initialization and this start method can be called many times depending on the state of applet either it is required to be started or not. The idle state of applet will stop the running state by calling the stop method and makes the applet it idle and this stop method can be called many times in the life cycle of applet.

The destroy method is called when the applet program gets finished and this method ends the life cycle of applet respectively and this destroy method is also called once in the life cycle of applet like the init method. The paint method is usually called when the area of drawing inside the applet is required to be refreshed. There is a need to write HTML file for the respective applet and there is a tag known as <applet>. There are many attributes available for this tag including code, width, height, codebase, name and others.

Importance:

There are server side programs and as well as the client side programs and complex desktop and web applications can be built in the java programming language. The computer program which runs in the computer is known as an application. The user has full control on the program and can start and run that application depending on the requirements. The web applications are designed and developed for the information of some place, companies and related areas including the processing of online forms and storage of details of data in the database system. The web applications are distributed application and usually communicate through a network or server. The applet being a graphic program runs in the web browser of the computer using the plug-in of java. The applets are used in order to insert the dynamic web content in the webpage. The applets in java are able to perform the required animations and can get the related results of calculation without sending the request to server.

Screenshots:

Following are the screenshots from the running desktop based application and an applet respectively:

Desktop based Application - Calculator

Figure 1: Desktop based Application - Calculator

This is the jframe form created using the SWING in Netbeans. This is the screenshot from an application of java named as conversion application and the code is given of this work in detail after the screenshots.

The events of an application when executed successfully

Figure 2: The events of an application when executed successfully

This calculator shows the work when the input in the text-fields is inserted by the user and the respective functionality button is clicked the results appear in the text-area.

Output of APPLET class

Figure 3: Output of APPLET class

The Figure 3 shows the output of an applet when the class of applet is run.

When Applet is executed in the web browser it asks to install the java plug-in

Figure 4: When Applet is executed in the web browser it asks to install the java plug-in

The webpage of an applet when executed on the web browser, it detects no available java plug-in so in order to run it successfully, there is a need to install the java plug-in for web browser.

When HTML page is run after installing java plug-in

Figure 5: When HTML page is run after installing java plug-in

After the successful installation of java plug-in, the browser asks for the permission due to the security issues.

The security restriction does not allow the applet part to execute in the web browser completely

Figure 6: The security restriction does not allow the applet part to execute in the web browser completely

Due to the security restrictions, the webpage of HTML cannot execute successfully and this warning is shown. The applications are better because they can run without these kinds of restriction and are much secure then applets.

Implementation & Codes:

The codes of java classes and the applet are shown:

Listing 1: Conversion_Application.java

package conversion_application;
public class Conversion_Application {
    public static void main(String[] args) {       
        Calculator c=new Calculator();
        c.setVisible(true);     
    }}

This is the main class of an application and calls the calculator form.

Clear Button Code

Listing 2: Calculator.java

private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        jTextArea1.setText(null);
        jTextField1.setText(null);
        jTextField2.setText(null);
    }//GEN-LAST:event_jButton7ActionPerformed

This is the button code and it basically clears the present text from the text-area and the text-fields.

Listing 3: EXIT Button Code

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        System.exit(0);    }//GEN-LAST:event_jButton6ActionPerformed

This is the code to exit an application by a simple click.

Listing 4: Add (+) Button Code

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        String n1=jTextField1.getText();
        double vn1=Double.parseDouble(n1);
        String n2=jTextField2.getText();
        double vn2=Double.parseDouble(n2);        
        double result=0;
        result=vn1+vn2;
        String res;
        res=String.valueOf(result);
        jTextArea1.append("\n"+"Additon of "+n1+" with "+n2+" = "+res+"\n");
    }//GEN-LAST:event_jButton1ActionPerformed

This is the code to add the two entered numbers by the user and display the result respectively.

Listing 5: Multiply (*) Button Code

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        String n1=jTextField1.getText();
        double vn1=Double.parseDouble(n1);
        String n2=jTextField2.getText();
        double vn2=Double.parseDouble(n2);        
        double result=0;
        result=vn1*vn2;
        String res;
        res=String.valueOf(result);
        jTextArea1.append("\n"+"Multiplication of "+n1+" with "+n2+" = "+res+"\n");
    }//GEN-LAST:event_jButton2ActionPerformed

This is the code to multiply the entered numbers by the user and the result is displayed.

Listing 6: Divide (/) Button Code

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        String n1=jTextField1.getText();
        double vn1=Double.parseDouble(n1);
        String n2=jTextField2.getText();
        double vn2=Double.parseDouble(n2);
        double result=0;
        result=vn1/vn2;
        String res;
        res=String.valueOf(result);
        jTextArea1.append("\n"+"Division of "+n1+" by "+n2+" = "+res+"\n");
    }//GEN-LAST:event_jButton3ActionPerformed

This code divides the number by the other and the result is displayed as shown in the above screenshot.

Listing 7: Subtract (-) Button Code

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        String n1=jTextField1.getText();
        double vn1=Double.parseDouble(n1);
        String n2=jTextField2.getText();
        double vn2=Double.parseDouble(n2);
        double result=0;
        result=vn1-vn2;
        String res;
        res=String.valueOf(result);
        jTextArea1.append("\n"+"Subtracton of "+n1+" and "+n2+" = "+res+"\n");
    }//GEN-LAST:event_jButton4ActionPerformed

This is the code to subtract the number from the other and display the result in the text-area.

Listing 8: Modulus (%) Button Code

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        String n1=jTextField1.getText();
        double vn1=Double.parseDouble(n1);
        String n2=jTextField2.getText();
        double vn2=Double.parseDouble(n2);
        double result=0;
        result=vn1%vn2;
        String res;
        res=String.valueOf(result);
        jTextArea1.append("\n"+"Modulus of "+n2+" with "+n1+" = "+res+"\n");
    }//GEN-LAST:event_jButton5ActionPerformed

This code finds the remainder of the number respectively.

Listing 9: Applet_View.java

package conversion_application;
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
public class Applet_View extends Applet {
    public void init() {        System.out.println("WELCOME");
    }    // TODO overwrite start(), stop() and destroy() methods
    public void paint(Graphics pg){
        setBackground(Color.gray);
        pg.setColor(Color.WHITE);
        pg.drawString("This is an  applet", 30, 60);
    }}

This is the applet class and the result is shown above in the form of a screenshot.

Listing 10: Applet_View.html

<HTML>
<HEAD>
   <TITLE>Applet HTML Page</TITLE>
</HEAD>
<BODY>
<!--
*** GENERATED applet HTML launcher - DO NOT EDIT IN 'BUILD' FOLDER ***
If you need to modify this HTML launcher file (e.g., to add applet parameters), 
copy it to where your applet class is found in the SRC folder. If you do this, 
the IDE will use it when you run or debug the applet.
Tip: To exclude an HTML launcher from the JAR file, use exclusion filters in 
the Packaging page in the Project Properties dialog.
For more information see the online help.
-->
<H3><HR WIDTH="100%">Applet HTML Page<HR WIDTH="100%"></H3>
<P>
<APPLET codebase="classes" code="conversion_application/Applet_View.class" width=350 height=200></APPLET>
</P>
<HR WIDTH="100%"><FONT SIZE=-1><I>Generated by NetBeans IDE</I></FONT>
</BODY>
</HTML>

This is the html code that has been generated after the successful run of applet.

This is all for this article. Hope you liked it.



My main area of specialization is Java and J2EE. I have worked on many international projects like Recorders,Websites,Crawlers etc.Also i am an Oracle Certified java professional as well as DB2 certified

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login