Applet in Java

Advertisements

Scanner Class in Java Factory Method in Java

Applet in Java

Applet is a predefined class in java.applet package used to design distributed application. It is a client side technology. Applets are run on web browser.

Advantage of Applet

  • Applets are supported by most web browsers.
  • Applets works on client side so less response time.
  • Secured: No access to the local machine and can only access the server it came from.
  • Easy to develop applet, just extends applet class.
  • To run applets, it requires the Java plug-in at client side.
  • Android, do not run Java applets.
  • Some applets require a specific JRE. If it required new JRE then it take more time to download new JRE.

Life cycle of applet

  • init()
  • start()
  • stop
  • destroy
Life Cycle of Applet

init(): Which will be executed whenever an applet program start loading, it contains the logic to initiate the applet properties.

start(): It will be executed whenever the applet program starts running.

stop(): Which will be executed whenever the applet window or browser is minimized.

destroy(): It will be executed whenever the applet window or browser is going to be closed (at the time of destroying the applet program permanently).

Design applet program

We can design our own applet program by extending applet class in the user defined class.

Syntax

class className extends Applet
{
......
// override lifecycle methods
......
}

Note: Whenever an applet program is running inti() and start() will be executed one after another, but stop() and destroy() will be executed if the browser is minimized and closed by the end user, respectively.

Note: Applet program may or may not contain life cycle methods.

Running of applet programs

Applet program can run in two ways.

  • Using html (in the web browser)
  • Using appletviewer tool (in applet window)

Running of applet using html

In general no Java program can directly execute on the web browser except markup language like html, xml etc.

Html support a predefined tag called <applet> to load the applet program on the browser window.

Syntax

<applet code="udc.class">
height="100px"
width="100px"
</applet>

Example of applet program to run applet using html

Java code, JavaApp.java

import java.applet.*;
import java.awt.*;
public class JavaApp extends Applet   
{
public void paint(Graphics g)
{
Font f=new Font("Arial",Font.BOLD,30);
g.setFont(f);
setForeground(Color.red);
setBackground(Color.white);
g.drawString("Student",200,200);
}
}

Html code, myapplet.html

<html>
<title> AppletEx</Title>
<body>
<applet code="JavaApp.class"
          height="70%"
	  width="80%">
</applet>
</body>
</html>

If applet code not run on browser then allow blocked contents.

Applet

Running of applet using appletviewer

Some browser does not support <applet> tag so that Sun MicroSystem was introduced a special tool called appletviewer to run the applet program.

In this Scenario Java program should contain <applet> tag in the commented lines so that appletviewer tools can run the current applet program.

Example of Applet

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

/*<applet code="LifeApp.class" height="500",width="800">
</applet>*/

public class LifeApp extends Applet
{
	String s= " ";
	public void init()
	{
		s=s+ " int ";
	}
	public void start()
	{
		s=s+ "start ";
	}
	public void stop()
	{
		s=s+ "stop ";
	}
	public void destroy()
	{ 
		s=s+ " destory ";
	}
	public void paint(Graphics g)
	{
		Font f=new Font("Arial",Font.BOLD,30);
		setBackgroundColor(Color."red");
		g.setFont(f);
        g.drawString(s,200,250);
	}
}

Execution of applet program

javac LifeApp.java
appletviewer LifeApp.java

Note: init() always execute only once at the time of loading applet window and also it will be executed if the applet is restarted.


Scanner Class in Java Factory Method in Java

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements