CƠ BẢN VỀ APPLET TRONG JAVA

An applet is a special kind of Java program that runs in a Java enabled browser. This is the first Java program that can run over the network using the browser. Applet is typically embedded inside a web page và runs in the browser.

Bạn đang xem: Cơ bản về applet trong java

In other words, we can say that Applets are small Java applications that can be accessed on an Internet server, transported over Internet, and can be automatically installed and run as apart of a web document.

After a user receives an applet, the applet can produce a graphical user interface. It has limited access to lớn resources so that it can run complex computations without introducing the risk of viruses or breaching data integrity.

To create an applet, a class must class extends java.applet.Applet class.

An Applet class does not have any main() method. It is viewed using JVM. The JVM can use either a plug-in of the Web browser or a separate runtime environment lớn run an applet application.

JVM creates an instance of the applet class và invokes init() method to lớn initialize an Applet.

Note: Java Applet is deprecated since Java 9. It means Applet API is no longer considered important.

Lifecycle of Java Applet

Following are the stages in Applet

Applet is initialized. Applet is started Applet is painted. Applet is stopped. Applet is destroyed.

*

A Simple Applet

import java.awt.*;import java.applet.*;public class Simple extends Applet public void paint(Graphics g) g.drawString("A simple Applet", đôi mươi, 20);

*

Every Applet application must import two packages - java.awt and java.applet.

java.awt.* imports the Abstract Window Toolkit (AWT) classes. Applets interact with the user (either directly or indirectly) through the AWT. The AWT contains support for a window-based, graphical user interface. java.applet.* imports the applet package, which contains the class Applet. Every applet that you create must be a subclass of Applet class.

The class in the program must be declared as public, because it will be accessed by code that is outside the program.Every Applet application must declare a paint() method. This method is defined by AWT class & must be overridden by the applet. The paint() method is called each time when an applet needs khổng lồ redisplay its output. Another important thing to lớn notice about applet application is that, execution of an applet does not begin at main() method. In fact an applet application does not have sầu any main() method.

Advantages of Applets

It takes very less response time as it works on the client side.It can be run on any browser which has JVM running in it.

Applet class

Applet class provides all necessary tư vấn for applet execution, such as initializing và destroying of applet. It also provide methods that load & display images và methods that load và play audio clips.

An Applet Skeleton

Most applets override these four methods. These four methods forms Applet lifecycle.

init() :
init() is the first method to lớn be called. This is where variable are initialized. This method is called only once during the runtime of applet.start() : start() method is called after init(). This method is called lớn restart an applet after it has been stopped.stop() : stop() method is called to lớn suspover thread that does not need lớn run when applet is not visible.destroy() : destroy() method is called when your applet needs to be removed completely from memory.

Xem thêm: Microsoft Office 2016 Full, Office 2007 Full Crack Sinhvienit

Note: The stop() method is always called before destroy() method.

Example of an Applet Skeleton

import java.awt.*;import java.applet.*;public class AppletTest extends Applet{ public void init() //initialization public void start () //start or resume execution public void stop() { //suspkết thúc execution public void destroy() //perform shutdown activity public void paint (Graphics g) //display the nội dung of window

Example of an Applet

import java.applet.*;import java.awt.*;public class MyApplet extends Applet int height, width; public void init() height = getSize().height; width = getSize().width; setName("MyApplet"); public void paint(Graphics g) g.drawRoundRect(10, 30, 120, 1trăng tròn, 2, 3);
*

Parameter in Applet

User-define Parameter can be applied in applet using tags. Each tag has a name và value attribute.

Example:

name = colorValue = red

Syntax:

In an applet code, applet can refer lớn a parameter by its name và then find its value.

The two most important thing khổng lồ handle & phối up the parameter is the tag in the HTML document and an applet code lớn parse this parameter.

init() method is used lớn get hold of the parameters which is defined in the tags. And getParameter() method is used for getting the parameters.

In Applet, Parameters are passed on applet when it is loaded.

Example:

param.java

import java.applet.*;import java.awt.*;public class param extends Applet String str; public void init() str=getParameter("pname"); if (str == null) str = "Welcome to lớn theartinpixels.com"; str = "Hello " + str; public void paint(Graphics g) g.drawString(str, 200, 200); param.html

*

*

How to run an Applet Program

An Applet program is compiled in the same way as you have sầu been compiling your console programs. However there are two ways to run an applet.

Executing the Applet within Java-compatible web browser.Using an Applet viewer, such as the standard tool, applet viewer. An applet viewer executes your applet in a window

For executing an Applet in an web browser, create short HTML file in the same directory. Inside body tag of the file, include the following code. (applet tag loads the Applet class)

Run the HTML file

*

Running Applet using Applet Viewer

To exexinh tươi an Applet with an applet viewer, write short HTML tệp tin as discussed above. If you name it as run.htm, then the following commvà will run your applet program.

f:/>appletviewer run.htm

*

Graphics in Applet

In Applet, java.awt.Graphicsclass provides methods for using graphics.

Below are the Methods of the Graphics class.

Sr No. Methods Description
1 public abstract void drawString(String str, int x, int y) Used khổng lồ draw specified string.
2 public void drawRect(int x, int y, int width, int height) Used khổng lồ draw a rectangle of specified width & height.
3 public abstract void fillRect(int x, int y, int width, int height) Used khổng lồ draw a rectangle with a mặc định colourof specified width và height.
4 public abstract void drawOval(int x, int y, int width, int height) Used khổng lồ draw oval of specified width và height.
5 public abstract void fillOval(int x, int y, int width, int height) Used lớn draw oval with a mặc định colour of specified width & height.
6 public abstract void drawLine(int x1, int y1, int x2, int y2) Used for drawing lines between the point (x1, x1) và (x2, y2).
7 public abstract booleandrawImage(Image img, int x, int y, ImageObhệ thống observer) Used for drawing a specified image.
8 public abstract void drawArc(int x, int y, int width, int height, intstartAngle, intarcAngle) Used for drawing a circular arc.
9 public abstract void fillArc(int x, int y, int width, int height, intstartAngle, intarcAngle) Used for filling circular arc.
10 public abstract void setColor(Màu sắc c) Used khổng lồ set a colour to lớn the object.
11 public abstract void setFont(Font font) Used khổng lồ mix font.

Example:

GraphicsDemo1.java

import java.applet.Applet; import java.awt.*; public class GraphicsDemo1 extends Applet public void paint(Graphics g) g.setColor(Color.black); g.drawString("Welcome khổng lồ theartinpixels.com",50, 50); g.setColor(Color.blue); g.fillOval(170,200,30,30); g.drawArc(90,150,30,30,30,270); g.fillArc(270,150,30,30,0,180); g.drawLine(21,31,đôi mươi,300); g.drawRect(70,100,30,30); g.fillRect(170,100,30,30); g.drawOval(70,200,30,30); GraphicsDemo1.html