Reading:  

Quick walk through the advanced concepts in Java - Part 3 of series


Working with Applets

Applet is a special type of program that runs in a Web browser and is used to generate the dynamic content. An applet works at client side and runs inside the browser and.

Applet Advantage:

  • It is secured
  • It takes very less time to response because it works at client side.
  • Under many platforms like Windows, Linux, Mac etc applets can be executed by running browsers.

Life Cycle of an Applet:

  • init: This method is used for Applet initialization and only once it is invoked.
  • start: This method is called by default after the init method.
  • stop: This method is used to stop the Applet. When browser is minimized stop is invoked.
  • destroy: This method is used to destroy the Applet and only once it is invoked.
  • paint: This method is used to paint the Applet and which provides object of Graphics class which is useful for drawing arc, circle, rectangle etc. It is invoked after the start() method.

The Applet Class:

Each and every applet is an extension of the java.applet.Applet class. The methods of applet which perform the following:

  • To resize the applet
  • To fetch an image
  • To get applet parameters
  • To fetch and play an audio clip
  • To get the applet class directory network location

The applet class also gives an interface by using this user can get the information about applet and controls the execution of applets.

  • The applet initialization
  • To start the execution of applets
  • To stop the execution of applets
  • To destroy the applet
  • User can request information about applet’s version, author and copyright

java.applet.Applet class methods are:

 

  • public void init():
  • public void start():
  • public void destroy():
  • public void stop():

java.awt.Component class method: 

  • public void paint(Graphics g):

To run an Applet, there are 2 ways:

  • By using html file.
  • By using tool appletViewer.

Example: A simple Applet program with html file

import java.applet.Applet; //importing applet
import java.awt.Graphics; //importing 

public class FirstAppletExamole extends Applet {

	public void paint(Graphics gh) {
		System.out.println(“Inside paint method”);
		gh.drawString("welcome to applet", 255, 340);
	}
}

To invoke an Applet: By using embedded HTML file an applet may be invoked.

For embedding an applet in an HTML file the <applet> tag is used as shown in below HTML file.

<html>  
<body>  
<applet code=" FirstAppletExamole.class" width="400" height="400">  
</applet>  
</body>  
</html>  

Output

Applet Parameter:

User can get information from the HTML file as a parameter. To achieve this Applet class provides a method getParameter().

Example:

import java.applet.Applet;
import java.awt.Graphics;
 

@SuppressWarnings("serial")
public class AppletParametersExample extends Applet {
	  
	public void paint(Graphics gh){  
	String str1= getParameter("new");  
	gh.drawString(str1,90, 100);  
	}
}
 

Html file:

<html>
<body>
<applet code="AppletParametersExample.class" width="300" height="300">
<param name="msg" value="Applet Applet">
</applet>
</body>
</html>

EventHandling:

The container class has a group of event handling methods which are inherited by applets. The methods are processKeyEvent and processMouseEvent for handling events.

Example:

import java.awt.Button;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class EventHandlingExample extends Applet implements ActionListener {
	Button c;
	TextField field;

	public void init() {
		field = new TextField();
		field.setBounds(10, 30, 160, 15);

		c = new Button("CLICK");
		c.setBounds(90, 160, 55, 60);

		add(c);
		add(field);
		c.addActionListener(this);

		setLayout(null);
	}

	public void actionPerformed(ActionEvent a) {
		field.setText("Event Handling");
	}
}

Html file:

<html>
<body>
<applet code="EventHandlingExample.class" width="300" height="300">
</applet>
</body>
</html>

Displaying Images:

Applet is used to display images and also in games and animation. The display image format like JPEG, GIF, BMP etc for this java.awt.Graphics class provide a method to display the drawImage().

Required methods of Applet class to display image:

public URL getDocumentBase(): to get the URL of the document in which applet is embedded.

public URL getCodeBase(): to get the base URL.

The ImageObserver object is fourth argument of drawImage() method. The ImageObserver interface is implemented by Component class.

Example:

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
	
public class AppletImageExample extends Applet {
	Image img;  
		  
	  public void init() {  
	    img = getImage(this.getDocumentBase(),"java.jpg");  
	  }  
	    
	  public void paint(Graphics gh) {  
	    gh.drawImage(img, 80, 80, this);  
	  }  
}

Html file:

<html>
<body>
<applet code="AppletImageExample.class" width="300" height="300">
</applet>
</body>
</html>

Playing Audio:

To play an audio file by using AudioClip interface in the java.applet package.The methods of audio interface are:

  • public void play(): to play the audio clip from the beginning one time.
  • public void loop(): to replay audio clip.
  • public void stop(): to stop playing the audio clip.

By using the applet method getAudioClip() to get an AudioClip object.

Example:

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Graphics;

public class AppletAudioExample extends Applet {
	private AudioClip c;
	public void init() {

		c = getAudioClip(getDocumentBase(), "default.au");

	}
	public void paint(Graphics g) {

		// To restart the audio clip
		c.play();

		// To Stop playing audio clip
		c.stop();

		// To Start playing audio clip in a loop
		c.loop();

	}
}

Html file:

<html>
<body>
<applet code="AppletAudioExample.class" width="300" height="300">
</applet>
</body>
</html>


 

Description

This is the last part in our tutorial series on Java. This tutorial is designed as a quick walk through the advanced concepts of Java Languages. This tutorial is subdivided into few chapters as shown below

  1. Data Structures
  2. Collections
  3. Generics
  4. Serialization
  5. Networking 
  6. Working with Emails
  7. Multithreading
  8. Getting started with Applets
  9. JavaDoc

Let us know if you find any issues with this tutorial. Also, if you can provide us with your feedback, that always help us improve.

 



Prerequisites

You must have read Part 1 and Part 2 of our tutorial series on Java.

Audience

Beginners or students looking to brush up their Java knowledge

Learning Objectives

To get an understanding on some of the advanced topics of Java.

Author: Subject Coach
Added on: 10th Mar 2015

You must be logged in as Student to ask a Question.

None just yet!