import java.awt.*; import java.applet.*; public class Simple extends Applet { public void paint(Graphics g) { g.drawString("A simple Applet", 20, 20); } }
Every Applet application must import 2 packages - java.applet. & java.awt.
Abstract Window Toolkit (AWT) classes are imported by java.awt.*. Applets communicate (directly or indirectly) via the AWT with the client. The AWT includes support for a graphical user interface based on a window. Java.applet.* imports the Applet package containing the Applet class. Any applet you generate must be an Applet class subclass.
The class in the program must be declared public because code outside of the program will be accessed to it. Every request in Applet must declare a method for paint. AWT class defines this method and the applet must override it. Every moment an applet requires to redisplay its output, the paint() method is called. Another significant thing to notice about the applet implementation is that an applet execution does not start with the main method. In reality, there is no primary/main method in an applet implementation.
Applet class provides all the support needed to execute applets, such as initializing and destroying applets. It also provides techniques/methods for loading and displaying audio videos and playback pictures.
These 4 methods are overridden by most applets. These four methods are the lifecycle of the Applet.
Note: The stop() method is always called/executed before destroy() method.
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 } }
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); } }
In the same manner as you compiled your console programs, an Applet program is compiled. There are, however, two methods of running an applet.
Create brief HTML file in the same folder to execute an Applet in a web browser. Include the following code in the file's body tag. (Applet tag loads class Applet).
< applet code = "MyApplet" width=400 height=400 > < /applet >
Write a brief HTML file as mentioned above to run an Applet with an applet viewer. If you name it as run.htm, your applet program will operate the following command.
f:/>appletviewer run.htm
Keep sharing blogs like this one; they are quite good. You have given everyone in this blog access to a wealth of information.
Thank you for your valuable information.
Thank you for this wonderful article!
This article was really helpful to me, thank you!
super article!
Leave a Reply
Your email address will not be published. Required fields are marked *