Draw a line and rectangle in Java Applet

Draw a line in Java Applet:

The line is the simplest shape that we can draw with the Graphics class. The drawLine() method takes two pair of coordinates (x1, y1) and (y1, y2) as arguments and draws a line between them. It has the following syntax:

g.drawLine(x1, y2, x2, y2);  // g is the Graphics object passed to paint() method.

Example:

import java.awt.*;
import java.applet.*;
public class Line extends Applet
{
 public void paint(Graphics g)
{
   g.drawLine(100,10,250, 150);
   g.drawLine(100,150,150,10);
}
}

<html>
   <head>
   </head>
   <body>
         <applet code = "Line.class" width = "420" height = "320"></applet>
   </body>
</html>

Output:
Draw a line in Java Applet

Draw a rectangle in Java Applet:

Example:

import java.awt.*;
import java.applet.*;
public class Rectangle extends Applet
{
 public void paint(Graphics g)
{
g.setColor(Color.black);
g.drawRect(120, 50, 100, 100);
}
}

         <applet code = "Rectangle.class" width = "480" height = "360"></applet>

Output:
Draw a line and rectangle in Java Applet