Related Topics

JAVA Programming
import java.applet.Applet;
import java.awt.*;
public class MyFirstApplet extends Applet {
public void init() {
// Initialization code goes here
}
public void start() {
// Start code goes here
}
public void stop() {
// Stop code goes here
}
public void destroy() {
// Cleanup code goes here
}
public void paint(Graphics g) {
g.drawString("Hello, world!", 50, 50);
}
}
This applet defines a class MyFirstApplet
that extends the Applet
class. The init()
, start()
, stop()
, and destroy()
methods are overridden to provide applet-specific functionality:
init()
is called once when the applet is first created, and can be used to perform any necessary initialization such as setting up GUI components or loading resources.start()
is called when the applet is started or resumed, and can be used to start any threads or animations that need to run.stop()
is called when the applet is stopped or paused, and can be used to stop any threads or animations that are running.destroy()
is called when the applet is being destroyed or removed from the web page, and can be used to clean up any resources that were allocated in theinit()
method.
Finally, the paint()
method is overridden to provide the applet’s graphical content. In this case, the drawString()
method is used to draw the text “Hello, world!” on the applet’s display area.
To run this applet in a web browser, you would need to embed it in an HTML page using the <applet>
tag. For example:
<html>
<head>
<title>My First Applet</title>
</head>
<body>
<applet code="MyFirstApplet.class" width="300" height="200"></applet>
</body>
</html>
This HTML page includes an <applet>
tag that specifies the name of the applet’s class (MyFirstApplet.class
) and the width and height of the applet’s display area. When the page is loaded in a web browser that supports Java applets, the applet’s init()
method will be called to perform initialization, followed by the start()
method to start any animations or threads, and the paint()
method to draw the applet’s content.
public void paint(Graphics g) {
g.drawLine(10, 10, 50, 50);
}
In this example, the paint()
method is overridden to draw a line on the applet’s display area. The drawLine()
method is called with the coordinates of the start and end points of the line.
Other methods in the Graphics
class can be used to draw shapes such as rectangles, ovals, and polygons, as well as text and images. The setColor()
method can be used to set the color used for drawing, and the setFont()
method can be used to set the font used for text.
The Graphics
class also provides methods for manipulating the current transformation matrix, which can be used to rotate, scale, or translate graphical objects.




Popular Category
Topics for You
Go through our study material. Your Job is awaiting.