Draw Grids
Posted on: October 16, 2008 at 12:00 AM
To draw the grids, we have defined rows and columns. The method g.drawLine(0, k * heightOfRow, width, k * heightOfRow) draws the number of rows.

Draw Grids

     

This section illustrates you how to draw grids.

To draw the grids, we have defined rows and columns. The method g.drawLine(0, k * heightOfRow, width, k * heightOfRow) draws the number of rows. The method  g.drawLine(k*widthOfRow, 0, k*widthOfRow, height) draws the number of columns.

 

 

 

 

Now create a canvas and add it to the frame by the following code:

Grids grid = new Grids(w, h, rows, cols);
add(grid);

Here is the code of DrawGrids.java

import java.awt.*;
import java.awt.event.*;

class Grids extends Canvas {
  int width, height, rows, columns;
 
  Grids(int w, int h, int r, int c) {
  setSize(width = w, height = h);
  rows = r;
  columns = c;
  }
  public void paint(Graphics g) {
  int k;
  width = getSize().width;
  height = getSize().height;

  int htOfRow = height / (rows);
  for (k = 0; k < rows; k++)
  g.drawLine(0, k * htOfRow , width, k * htOfRow );

  int wdOfRow = width / (columns);
  for (k = 0; k < columns; k++)
  g.drawLine(k*wdOfRow , 0, k*wdOfRow , height);
  }
}
public class DrawGrids extends Frame {
  DrawGrids(String title, int w, int h, int rows, int columns) {
  setTitle(title);
  Grids grid = new Grids(w, h, rows, columns);
  add(grid);
}

public static void main(String[] args) {
  new DrawGrids("Draw Grids"200200210).setVisible(true);
  }
}

Output will be displayed  as

Download Source Code

 

Related Tags for Draw Grids:
cgridmethodcolumnheightwidthnumbercolumnslineidrowdefinerowstodrawrawwseliidsinmmedefinedumnscolkhaandwidssrithavfinlumndolo


More Tutorials from this section

Ask Questions?    Discuss: Draw Grids  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.