Skip to main content

Practical-48

Write a applet program to implement various methods of Graphics class.

1. Introdution

An applet is a java program that can be embedded in a web page. The Graphics class of java.awt package provides basic functionality for creating and manipulating graphics in a window or applet. Through the use of its various methods, programmers can draw shapes and display graphical images on the applet. This tutorial introduces the basics of the Graphics class, including the methods used to create and manipulate shapes and graphical images.

Code
import java.awt.*;
import java.applet.*;

public class MyApplet extends Applet {
public void paint(Graphics g) {
// code to draw a rectangle
g.drawRect(30, 30, 90, 20);
// code to draw a line
g.drawLine(30, 30, 150, 30);
// code to draw an oval
g.drawOval(135, 20, 40, 40);
}
}
Output

A blue rectangle, a black line and an oval will be drawn on the applet.

Explanation

This code creates an applet named MyApplet, which is an extension of the Applet class. It uses the paint() method to draw a rectangle, a line and an oval on the applet window. The drawRect() , drawLine() and drawOval() methods are used to draw these three shapes, and they accept the x- and y-coordinates of the starting point and the dimension of the rectangular bounds.