Introduction to Applets
Applets are small applications that are
- accessed on an Internet server,
- transported over the Internet,
- Automatically installed,
- and run as part of a web document
Java can be used to create two types of programs: applications and applets.
Applet | Application |
It is initialised through init(). | Starts its execution through the main() method |
It is a small program that uses another application (browser) program for its execution. | An application is a program executed on a computer independently. |
Cannot run independently, requires API's | Can run alone but requires JRE. |
Prior installation is not needed. Browsers with an applet plug-in are enough. | Requires prior explicit installation on the local computer. |
The files cannot be read or written on the local computer through the applet. | Applications are capable of performing those operations on the files on the local computer. |
Requires security for the system as the applets are untrusted. | No security concerns exist. |
Created by extending the Applet class | User-defined class with main() |
Uses a GUI interface to interact with users | Uses I/O stream classes |
- Executing the applet within a Java-compatible Web browser, such as Netscape Navigator.
- Using an applet viewer, such as the standard JDK tool, appletviewer.
Applet begins with two import statements.
The first imports the Abstract Window Toolkit (AWT) classes.
The second import statement imports the applet package, which contains the class Applet
Every AWT-based applet must be a subclass (either directly or indirectly) of Applet.
The class SimpleApplet must be declared as public, because it will be accessed by code that is outside the program.
- init( )
- start( )
- paint( )
- stop( )
- destroy( )
- init( )
- start( )
- paint( )
- stop( )
- destroy( )
- Simple applets
- JApplets
Method name | Description |
g.drawLine(x1, y1, x2, y2); | line between points (x1, y1), (x2, y2) |
g.drawOval(x, y, width, height); | Outline the largest oval that fits in a box of size width * height, with top-left at (x, y) |
g.drawRect(x, y, width, height); | outline of a rectangle of size |
g.drawString(text, x, y); | text with bottom-left at (x, y) |
g.fillOval(x, y, width, height); | fill the largest oval that fits in a box of size width * height with top-left at (x, y) |
g.fillRect(x, y, width, height); | fill rectangle of size width * height with top-left at (x, y) |
g.setColor(Color); | Set Graphics to paint any following shapes in the given colour |