Academic Java    Java Tutorial  >  Applets  >  drawLine() grid

drawLine() grid Example

Java drawLine() grid EXAMPLE output This applet draws a grid of horizontal and vertical lines.

The four parameters of drawLine() provide the start and end coordinates of the line.
// drawLine() Example 2
import java.awt.*;
import javax.swing.*;

class DrawLineExample2 extends JApplet {

   @Override public void paint(Graphics g) {
      super.paint(g);

      int s=300;
      int n=6;       // n is number of divisions
      int d=300/n;   // size of each division

      int i=1;
      while (i<n) {

         g.drawLine(0, d*i, s, d*i);
         g.drawLine(d*i, 0, d*i, s);
         i++;

      }

   }
}


* applet drawLine() Examples