Results 1 to 3 of 3

Thread: Java Draw Grid program

  1. #1
    Join Date
    Jul 2009
    Posts
    1,118

    Java Draw Grid program

    Hi,

    Is it possible to Draw grid in Java program?

    Thanks,
    Visala28

  2. #2
    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);
       }
    }

  3. #3
    Join Date
    Jul 2009
    Posts
    1,118

    Re: Java Draw Grid program

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

    Regards,
    Visala28

Similar Threads

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

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,675,739.58007 seconds with 16 queries