Results 1 to 12 of 12

Thread: Drawing a grid

  1. #1
    CrystalMoth is offline Member
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Default Drawing a grid

    I am tring to draw a grid but it does not come up. My code compiles but no grid.

    My Code:


    Java Code:
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Insets;
    import javax.swing.JPanel;
    
    public class Grid extends JPanel
    {
    	private static final long serialVersionUID = 1L;
    	
    	public void paintComponent(Graphics g)
    	{
    		super.paintComponent(g);
    		Graphics2D graphics = (Graphics2D) g;
    		graphics.setColor(Color.blue);
    		
    		Dimension size = getSize();
    		Insets insets = getInsets();
    		int w = size.width - insets.left - insets.right;
    		int h = size.height - insets.top - insets.bottom;
    		
    		for(int i=0; i==w; i=i+32)
    		{
    			for(int j=0; j==h; j=j+32)
    			{
    				graphics.drawLine(i, j, i, j);
    			}
    		}
    	}
    }
    Java Code:
    import javax.swing.JFrame;
    
    
    public class Start extends JFrame
    {
    	private static final long serialVersionUID = 1L;
    	
    	public static void main(String[] args)
    	{
    		Grid grid = new Grid();
                 	JFrame frame = new JFrame("Grid");
                 	frame.add(grid);
                	frame.setSize(640, 480);
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I haven't looked at all your code, but this line here:
    Java Code:
    graphics.drawLine(i, j, i, j);
    tries to draw a line from a point to itself. That's a line of length 0.

  3. #3
    CrystalMoth is offline Member
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Default

    I'm trying to draw a grid, so to draw a point I draw a line from one coordinate to the same coordinate. I try to repeat this to draw a grid.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Also, if you want to show a "grid" such as a chess board, then you'll want to not draw lines, but instead fill rectangles -- but not every rectangle, just those where row + column is an even number.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by CrystalMoth View Post
    I'm trying to draw a grid, so to draw a point I draw a line from one coordinate to the same coordinate. I try to repeat this to draw a grid.
    This doesn't make any sense. If you draw a line from a point to itself, you have nothing (as your program quite readily demonstrates). Think about what would happen if you did this with pen and paper. The pen would never move.

  6. #6
    CrystalMoth is offline Member
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Default

    I tried the program from zetcode .com and it does this but with random coordinates, I'm trying to get a grid of dots for a CAD program.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by CrystalMoth View Post
    I tried the program from zetcode .com and it does this but with random coordinates,
    No idea what site this is.

    I'm trying to get a grid of dots for a CAD program.
    What exactly do you mean here? It's hard to advise without more precise specifications.

    For instance, when I think of "grid" I reflexively think of a chessboard, e.g.,
    Java Code:
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class Grid extends JPanel {
      private static final long serialVersionUID = 1L;
      public static final int GRID_COUNT = 8;
    
      public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D graphics = (Graphics2D) g;
        graphics.setColor(Color.blue);
    
        Dimension size = getSize();
        Insets insets = getInsets();
        int w = size.width - insets.left - insets.right;
        int h = size.height - insets.top - insets.bottom;
        
        int sqrWidth = (int)((double)w / GRID_COUNT);
        int sqrHeight = (int)((double)h / GRID_COUNT);
        for (int row = 0; row < GRID_COUNT; row++) {
          for (int col = 0; col < GRID_COUNT; col++) {
            if ((row + col) % 2 == 0) {
              int x = (int) (row * (double) w / GRID_COUNT);
              int y = (int) (col * (double) h / GRID_COUNT);
    
              graphics.fillRect(x, y, sqrWidth, sqrHeight);
            }
          }
        }
      }
    
      public static void main(String[] args) {
        Grid grid = new Grid();
        grid.setPreferredSize(new Dimension(400, 400));
        JFrame frame = new JFrame("Grid");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(grid);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
    }
    Last edited by Fubarable; 01-10-2010 at 05:47 AM.

  8. #8
    CrystalMoth is offline Member
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Default

    I want a grid consisting of dots. The dots appear at intervals of every 32 pixels. After one for loop to make the dots on the first line, starting from (0,0), then in the next for loop draw aother line of dots32 pixels down from the first row, and 32 px intervals untuil the entire screen is covered with single pixel dots.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Sorry, it's probably me, but I'm still not getting it. Can you provide any more detail?

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Also, your for loops are frcked up. This will never loop:

    Java Code:
        for (int i = 0; i == w; i = i + 32) {
          for (int j = 0; j == h; j = j + 32) {
            System.out.println("i = " + i + ", j = " + j);
            graphics.drawLine(i, j, i + 5, j + 5);
          }
        }
    Because i will never == w at the beginning of the loop, so the loop will never begin.

    Better would be:
    Java Code:
        for (int i = 0; i <= w; i = i + 32) {
          for (int j = 0; j <= h; j = j + 32) {
            graphics.drawLine(i, j, i, j);
          }
        }

  11. #11
    CrystalMoth is offline Member
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Default

    THANK YOU
    very much

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. How can I square(^2) the pic in the grid
    By racewithferrari in forum New To Java
    Replies: 2
    Last Post: 11-03-2009, 06:27 PM
  2. How can I square(^2) the pic in the grid
    By racewithferrari in forum New To Java
    Replies: 1
    Last Post: 11-01-2009, 11:16 PM
  3. Database Grid
    By leoleo4614 in forum AWT / Swing
    Replies: 1
    Last Post: 12-13-2008, 08:02 AM
  4. how to get the grid values
    By jazz2k8 in forum Advanced Java
    Replies: 2
    Last Post: 11-06-2008, 03:11 PM
  5. Help with Grid Layout
    By coco in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 09:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •