Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses
Showing posts with label Applet Programming. Show all posts
Showing posts with label Applet Programming. Show all posts

How to add components and event handling to a Java Applet ?.

Program to demonstrate an Applet with components and event handling
package com.hubberspot.applet.example;

import java.applet.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/*


 */

public class AddComponentToApplet extends Applet {
  JPanel panel;
  JTextField textField ;
  String print;

  public void init() {
 textField = new JTextField(18);
 add(textField);
 addMouseListener(new MouseAdapter(){
  
 public void mouseClicked(MouseEvent event) {
    print = "Mouse Clicked: X = " + event.getX()
      + " Y = " + event.getY();
    textField.setText(print);
 }
  });

 addMouseMotionListener(new MouseMotionAdapter(){
    
 public void mouseMoved(MouseEvent event) {
    print = "Mouse Moved: X = " + event.getX()
   + " Y = " + event.getY();
    textField.setText(print);
 }
  });
 }

}
Output of the program :


How to run a simple Applet in Java ?.

Introduction
This blog post will guide you about the basics of Java Applet. This tutorial will teach you how to run a simple Applet in Java. So, What is an Applet in Java ? An Applet is a small java program that runs in a Java enabled web browser. Java Applet is a small piece of java code that is embedded into HTML page, which gets executed when the HTML page loads into the browser. Applet runs on the Client machine, while Servlet runs on Server side. They are rarely used to make website dynamic. Its secure and platform independent. They require JVM and Java enabled web browser such as IE, Mozilla etc, to get execute on client machine. Applets are embedded into HTML documents and has various methods in java.applet.Applet, which governs its life-cycle.

Program to demonstrate how to run a simple Applet in Java

import java.applet.*;
import java.awt.*;

/*
<applet code="SimpleApplet.class" 
         height="250" width="250">
</applet>
*/
public class SimpleApplet extends Applet {
  public void init() {
  }

  public void start() {
  }

  public void stop() {
  }

  public void destroy() {
  }

  public void paint(Graphics g) {
    g.setColor(Color.RED);
    g.drawString("Welcome to Hubberspot!", 50, 100);
  }
}

SimpleApplet.html file to run applet on Web Browser

<html>
  <head>
       <title>Simple Applet</title>
  </head>

  <body>
       <applet 
              code="SimpleApplet.class" 
              height="250" 
              width="250">
       </applet>
  </body>
</html>

Output of program :



Running an Applet in Java
Java Applet are compiled by javac command. It can be run by two ways, but not with using java command. It can be run by using either java tool- appletviewer or by loading Applet into web browser using applet tag in html. Let us look into more details below :

1) Using Web Browser :
To view the applet in a web browser, you will require a java enabled web browser. To enable java in the browser, go to browser advanced setting an enable java. The following steps should be followed :

a) Create an HTML file as above, containing the APPLET tag.
b) Compile the applet source code using javac.
c) Open the html file in the web browser.

2) Using appletviewer :
It is a java tool provided to view applets. It is like a small browser provided to have a preview of applet as they would look in browser.It understands APPLET tag and used in the creation phase. The APPLET tag should be written in source code file enclosing in comments. The following steps should be followed:

a) Write HTML APPLET tag in comments in the source file.
b) Compile the applet source code using javac.
c) Use appletviewer ClassName.class to view the applet.

Java Applets Life-Cycle Methods

Introduction 
This blog post will guide you about the basics of Java Applet. This tutorial will teach you how to create small applets. So, What is an Applet in Java ? An Applet is a small java program that runs in a Java enabled web browser. Java Applet is a small piece of java code that is embedded into HTML page, which gets executed when the HTML page loads into the browser. Applet runs on the Client machine, while Servlet runs on Server side. They are rarely used to make website dynamic. Its secure and platform independent. They require JVM and Java enabled web browser such as IE, Mozilla etc, to get execute on client machine. Applets are embedded into HTML documents and has various methods in java.applet.Applet, which governs its life-cycle. Let us look into few of those :

Applet Life-Cycle Methods

1. public void init()

It is the first method that is invoked or called for an applet. Any initialization needed for an applet is performed in this method. It is launched by the system automatically when Java executes an applet. It performs small tasks such as loading images and reading parameters etc . Applets can be initialized by the constructors but init method is preferred for doing all the applet initialization.


2. public void start()

After Java calls init method, this method is executed next. It gets automatically called after init method completes execution. It is special method, when a user goes to any other website and finally returns back to html page of applet, this method is called again. All the tasks that we need to perform after applet gets loaded and after user revisits applet is written here. Generally, animation performing code or new start to a different thread is written here.

3. public void paint( Graphics g )

This method is called usually after init and start method gets executed. This method is called whenever applet needs to be redrawn in the browser window. If user covers applet window by a different window and later comes back to same window by uncovering, the paint method is called. This method has one parameter of type Graphics which performs actions involved with drawing. It is also used for displaying text, graphics etc.

4. public void stop()

This method is called when the user leaves the page on which applet is running. This method performs tasks of stopping execution of animations and threads. This method is called just after the destroy method.

5. public void destroy()

This method is called when the browser completely ends the applet and it is being removed from memory. This method is called when browser shuts down normally. The destroy method has the code that perform tasks that are required to clean up resources such as graphics object, threads etc allocated to the applet.

Applet Advantages and Restrictions

What is an Applet in Java ?
An Applet is a small java program that runs in a Java enabled web browser. Java Applet is a small piece of java code that is embedded into HTML page, which gets executed when the HTML page loads into the browser.Applets provide powerful client-side functionality. As applets are loaded from remote machines and executed on client-side, there are various security restrictions on applets. Let us look into various Applets advantages and Applets Restrictions in brief :

Applet Advantages
Applets have many advantages over stand-alone application. Some of them are :

  1. As applet is a small java program, so it is platform independent code which is capable to run on any browser.
  2.  Applets can perform various small tasks on client-side machines. They can play sounds, show images, get user inputs, get mouse clicks and even get user keystrokes, etc ... 
  3.  Applets creates and edit graphics on client-side which are different and independent of client-side platform.
  4. As compared to stand-alone application applets are small in size, the advantage of transferring it over network makes it more usable.
  5. Applets run on client browser so they provide functionality to import resources such as images, audio clips based on Url's.
  6. Applets are quite secure because of their access to resources.
  7. Applets are secure and safe to use because they cannot perform any modifications over local system.
  8. Various small tasks such as performing login, inventory checking, task scheduling can be done by applets running over Intranets. 


Applets Restrictions
Applets have many restrictions over the areas of security because they are obtained from remote machines and can harm client-side machines. Some of them are as follows :

  1. If we are running an applet from a provider who is not trustworthy than security is important. 
  2. Applet itself cannot run or modify any application on the local system.
  3. Applets has no access to client-side resources such as files , OS etc. 
  4. Applets can have special privileges. They have to be tagged as trusted applets and they must be registered to APS (Applet Security Manager). 
  5. Applet has little restriction when it comes to communication. It can communicate only with the machine from which it was loaded. 
  6. Applet cannot work with native methods. 
  7. Applet can only extract information about client-machine is its name, java version, OS, version etc ... .
  8. Applets tend to be slow on execution because all the classes and resources which it needs have to be transported over the network. 
 
© 2021 Learn Java by Examples Template by Hubberspot