Program to draw Lines on Applet.

The AWT (Abstract Window Toolkit) supports a rich assortment of graphics methods. All graphics are drawn relative to a window. The origin of each window is at the top-left corner and is 0,0. Coordinates are specified in pixels.

The Graphics class defines a number of drawing functions. Each shape can be drawn edge-only or filled. The Graphics class is the abstract base class for all graphics contexts that allow an application to draw onto components.

Lines are drawn by means of the drawLine( ) method which draws a line, using the current color, between the points (x1, y1) and (x2, y2) in the graphics context's coordinate system. The syntax for the drawLine( ) is given below:
void  drawLine (int x1, int y1, int x2, int y2)

The following program draws two lines, first is from (0, 0) to (40, 20) and second is from (20, 70) to (100, 150).

import java.awt.Graphics; // This applet draws a pair of lines using the Graphics class
import java.applet.Applet;

public class DrawLines extends Applet
 public void paint(Graphics g)
  // Draw a line from the upper-left corner to the point at (40, 20)
  g.drawLine(0, 0, 40, 20);
  // Draw a line from (20, 70) to (100, 150)
  g.drawLine(20, 70, 100, 150);

/* <applet code=DrawLines width = 150 height = 150>



No comments:

Post a comment