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>



Popular posts from this blog

Program to define a class 'employee' with data members as empid, name and salary. Accept data for 5 objects using Array of objects and print it.

Define a class Student with four data members such as name, roll no.,sub1, and sub2. Define appropriate methods to initialize and display the values of data members. Also calculate total marks and percentage scored by student.

Program to input age from user and throw user-defined exception if entered age is negative