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 pairs 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);

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);
}
}



Output:
Draw a line in Java Applet

Draw a rectangle in Java Applet:

Example:
[java]
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);
}
}
[/java]

[html]

[/html]

Output:
Draw a line and rectangle in Java Applet