JAVA GUI 1
Applet
1. 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. Any applet in Java is a class that extends the java.applet.Applet class.
2. An Applet class does not have any main() method.
3. It is viewed using JVM. The JVM can use either a plug-in of the Web browser or a separate runtime environment to run an applet application.
4. JVM creates an instance of the applet class and invokes init() method to initialize an Applet.
Advantage of Applet
1. Very less response time as it works on the client side.
2. It is Secured.
3. It can be executed by browsers running under many plateforms, including Linux, Windows, Mac OS etc. which has JVM running in it.
Drawback of Applet
1. Plugin is required at client browser to execute applet.
A Simple Applet
import java.awt.*;
import java.applet.*;
public class Simple extends Applet
{
public void paint(Graphics g)
{
g.drawString("A Simple Applet", 20, 20);
}
}
Note: class must be public because its object is created by Java Plugin software that resides on the browser.
1. Every Applet application must declare a paint() method. This method is defined by AWT class and must be overridden by the applet. paint() method is called each time an applet neede to redisplay its output.
2.Another important thing to notice about applet application is that, execution of an applet does not begin at main() method. In fact an applet application does not have any main() method.
Applet Class
Applet class provides all necessary support for applet execution, such as initializing and destroying of applet. It also provide methods that load and display images and methods that load and play audio clips.
Applet Skeleton
Most applets override these four methods. These four methods forms Applet lifecycle.
1. init() : init() is the first method to be called. This is where variable are initialized. This method is called only once during the runtime of applet.
2. start() : start() method is called after init(). This method is called to restart an applet after it has been stopped.
3. stop() : stop() method is called to suspend thread that does not need to run when applet is not visible.
4. destroy() : destroy() method is called when your applet needs to be removed completely from memory.
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()
{
//suspend execution
{
public void destroy()
{
//perform shutdown activity
}
public void paint (Graphics g)
{
//display the content 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, 120, 2, 3);
}
}
How to run an Applet Program
An Applet program is compiled in the same way as you have been compiling your console programs. However there are two ways to run an applet.
1. Executing the Applet within Java-compatible web browser.
1. 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).
< applet code = "MyApplet" width=400 height=400 >
< /applet >
Running Applet using Applet Viewer
To execute an Applet with an applet viewer, write short HTML file as discussed above. If name is run.htm, then the following command will compile and run your applet program.
c:\>javac Myapplet.java
c:\>appletviewer run.htm
Dinesh Kumar S is a 23-year-old System Administrator who enjoys playing games, listening to music and learning new technology. He is friendly and generous, but can also be very lazy and crazy.
Share this
Learn-JAVA