Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 07-08-2009
Member
 
Join Date: Jul 2009
Posts: 1,113
Java Draw Grid program

Hi,

Is it possible to Draw grid in Java program?

Thanks,
Visala28
Reply With Quote
  #2  
Old 07-08-2009
Member
 
Join Date: May 2008
Posts: 33
Re: Java Draw Grid program

Yes it is possible.

You can try this sample code:

Code:
import java.awt.*;
import java.awt.event.*;

/**
 * Program to draw grids.
 * @author   Ian Darwin, 
 */
class GridsCanvas extends Canvas {
   int width, height;
   int rows;
   int cols;

   GridsCanvas(int w, int h, int r, int c) {
      setSize(width=w, height=h);
      rows = r;
      cols = c;
   }

   public void paint(Graphics g) {
      int i;
      width = getSize().width;
      height = getSize().height;

      // draw the rows
      int rowHt = height/(rows);
      for (i = 0; i < rows; i++)
         g.drawLine(0, i*rowHt, width, i*rowHt);

      // draw the columns
      int rowWid = width/(cols);
      for (i = 0; i < cols; i++)
         g.drawLine(i*rowWid, 0, i*rowWid, height);
   }
}

/** This is the demo class. */
public class Grids extends Frame {
   /* Construct a GfxDemo2 given its title, width and height.
    * Uses a GridBagLayout to make the Canvas resize properly.
    */
   Grids(String title, int w, int h, int rows, int cols) {
      setTitle(title);

      // Now create a Canvas and add it to the Frame.
      GridsCanvas xyz = new GridsCanvas(w, h, rows, cols);
      add(xyz);

        addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
            setVisible(false);
            dispose();
            System.exit(0);
         }
      });

      // Normal end ... pack it up!
      pack();
   }

   public static void main(String[] a) {
      new Grids("Test", 300, 300, 5, 10).setVisible(true);
   }
}
Reply With Quote
  #3  
Old 07-08-2009
Member
 
Join Date: Jul 2009
Posts: 1,113
Re: Java Draw Grid program

Hey thanks a lot for a working example & quick reply.

Regards,
Visala28
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Java Draw Grid program"
Thread Thread Starter Forum Replies Last Post
How to run java applet program using cmd Rao's Software Development 3 07-01-2012 03:35 PM
Dr. Java program bamggut29 Software Development 2 26-11-2011 04:24 AM
Don't know how to run the java bean program Reema_n Software Development 5 23-12-2009 11:01 AM
Link List Example in Java Sample program in Java trickson Software Development 2 04-08-2009 08:23 PM
How do i excute java program from asp.net softte Software Development 2 09-05-2009 02:06 PM


All times are GMT +5.5. The time now is 12:11 AM.