Java – Applets

Previous
Next

Applets:

  • Applet allows java programmer to implement GUI programming.
  • “applet” is a package in java API.
  • “Applet” is a pre-defined class provides the  functionality to user-defined Applet classes.
  • “Applet” is a window where we can arrange all the components to implement GUI.
  • Every User-defined Applet class must extends pre-defined Applet.
class MyApplet extends java.applet.Applet
{
	//user-defined applet class.
}

AWT :

  • Stands for Abstract Window Toolkit.
  • A set of Pre-defined(Abstract) classes and interfaces to implement Applets(Window programming) available as a Toolkit.
  • for example, Button, CheckBox, Paint, Graphics………

First Applet Program :

  • Applets are defined to be embedded into HTML pages.
  • Applets can be run by the browser interpreter along with HTML tags.
  • Every browser having an interpreter to execute HTML code.
  • HTML code will not be compiled, it will be executed directly by the interpreter.
  • Hence it is hard to find if error has encountered.
  • Intrepreter produces un-expected output page if any error in the HTML code.
  • JVM cannot execute applet program, hence no need to define main() method inside the applet class.
  • public void paint() method is the pre-defined method in Applet class.
  • Graphics class which is the pre-defined class available in java.awt package.
import java.applet.Applet;
import java.awt.*;
public class FirstApplet extends Applet 
{
	public void paint(Graphics g)
{
		g.drawString("Hello world", 200,200);
	}
}
/*
<applet code = "FirstApplet.class" width="600" height = "600">
</applet>
*/

Running Applets from HTML pages :

<HTML>
	<HEAD>
		<title>My Applet Program</title>
	</HEAD>

	<BODY>
		<applet code = "FirstApplet.class" width="600" height = "600">
		</applet>
	</BODY>
</HTML>

Applet Life cycle :

  • Some of the pre-defined funtions of Applet executes implicitly which can describe the flow of Applet Life cycle.
  • CUI based java application execution starts from main() function which is implicitly invokes my JVM.
  • GUI based java application will be invoked either by “appletviewer” or “browser interpreter”.
  • When applet execution starts, it will instantiated implicitly.
  • At the time of instantiation, constructor must be invoked.
  • Hence the only way to know whether the Applet has been instantiated or not by defining a constructor in the Applet class.
  • Members participate in Applet Life cycle :
    • Constructor()
    • init()
    • start()
    • paint()
    • stop()
    • destory()
import java.applet.Applet;
import java.awt.*;
public class FirstApplet extends Applet 
{
	public FirstApplet()
{
		System.out.println("Applet is instantiated....");
	}
	public void paint(Graphics g)
{
		g.drawString("Hello world", 200,200);
	}
	public void init()
{
		System.out.println("Applet is initialized....");
	}
	public void start()
{
		System.out.println("Control enter into Applet....");	
	}
	public void stop()
{
		System.out.println("Control goes off the Applet...");
	}
	public void destroy()
{
		System.out.println("Resource released.....");
	}
}
/*
<applet code = "FirstApplet.class" width="600" height = "600">
</applet>
*/

Layout :

  • Arrangement of Components by setting Bounds on the window.
  • Java API has many pre-defined layout classes to arrange the components.
    • Flow Layout
    • Grid Layout
    • Border Layout
    • Box layout….

Note : By default Applet arranges the components using FlowLayout structure.

import java.applet.Applet;
import java.awt.*;
public class Flow extends Applet 
{
	public void init()
{
		//this.setLayout(new FlowLayout(FlowLayout.LEFT));
		for(int i=1 ; i<=5 ; i++)
{
			Button b = new Button("BUTTON#"+i);
			this.add(b);
		}
	}
}
/*
<applet code = "Flow.class" width="600" height = "600">
</applet>
*/

Event Listeners:

  • An Event is an operation(action) performed on a component of Applet.
  • When action performed, Component fires one event.
  • Listener class can collect event information and reacts to the event according to defined logic.
  • “Listener” is a pre-defined interface of all the sub Listeners which are….
    • ActionListener
    • TextListener
    • MouseListener
    • MouseMotionListener
    • FocusListener
  • When we create Buttons, these cannot be reacted to Events which are performed.
  • ActionListener interface must be implemented to write EventListener logic for Buttons.
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class ButtonListener extends Applet implements ActionListener
{
	Button b1,b2;
	public void init()
{
		this.setLayout(null);
		this.b1 = new Button("BUTTON#1");
		this.b2 = new Button("BUTTON#2");
		b1.setBounds(150,200,150,50);
		b2.setBounds(300,200,150,50);
		this.add(b1);
		this.add(b2);
		b1.addActionListener(this);
		b2.addActionListener(this);
	}
	public void actionPerformed(ActionEvent ae)
{
		if(ae.getSource() == b1)
			System.out.println("you clicked button1");
		else
			System.out.println("you clicked button2");
	}
}
/*
<applet code = "ButtonListener.class" width="600" height = "600">
</applet>
*/

Frames :

  • Applets are designed to embed with HTML pages and these can be run by Browser Interpreter.
  • Frames are designed to implements Standalong GUI java applications run by JVM directly.
  • main() is allowed while implementing Frames.
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class RadioButton extends Frame
{
	Checkbox r1,r2;
	CheckboxGroup rg;
	TextField t; 
	public RadioButton(String name)
{
		this.setTitle(name);
		this.setSize(600,600);
		this.setLayout(new FlowLayout());
		rg = new CheckboxGroup();
		r1 = new Checkbox("RED",rg,false);
		r2 = new Checkbox("BLUE",rg,true);
		t = new TextField("A TextField",20); 
		this.add(r1);
		this.add(r2);
		this.add(t);
	}
	public static void main(String args[ ])
        {
		RadioButton r = new RadioButton("MY Frame");
		r.show();
	}
}
Previous
Next

Add Comment

Courses Enquiry Form