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

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

Output:
Draw a line and rectangle in Java Applet

Recommended Posts:

Advanced Java
    1. Difference between Local Applet and Remote Applet
    2. Applet Life Cycle in Java
    3. Difference Between Applet and Application
    4. Creating an Executable Applet in Java
    5. Displaying Numerical values in Applet
    6. An Applet Program to Add Two Numbers
    7. Event Handling in Applet
    8. AWT Classes in Java
    9. Draw a line and rectangle in Java Applet
    10. Draw Circles and Ellipses in Java Applet
    11. Draw Arc in Java Applet
    12. Develop an Applet for Drawing a Human Face
    13. Draw a Polygon in Java Applet
    14. Draw Line Graph in Java Applet
    15. Java Applet Program to Draw National Flag
    16. Demonstrate to Draw Bar Charts in Java