Results 1 to 12 of 12
Thread: Drawing a grid
- 01-10-2010, 04:00 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 5
- Rep Power
- 0
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); } }
-
I haven't looked at all your code, but this line here:
tries to draw a line from a point to itself. That's a line of length 0.Java Code:graphics.drawLine(i, j, i, j);
- 01-10-2010, 04:30 AM #3
Member
- Join Date
- Feb 2009
- Posts
- 5
- Rep Power
- 0
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.
-
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.
-
- 01-10-2010, 04:38 AM #6
Member
- Join Date
- Feb 2009
- Posts
- 5
- Rep Power
- 0
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.
-
No idea what site this is.
What exactly do you mean here? It's hard to advise without more precise specifications.I'm trying to get a grid of dots for a CAD program.
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 04:47 AM.
- 01-10-2010, 04:45 AM #8
Member
- Join Date
- Feb 2009
- Posts
- 5
- Rep Power
- 0
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.
-
Sorry, it's probably me, but I'm still not getting it. Can you provide any more detail?
-
Also, your for loops are frcked up. This will never loop:
Because i will never == w at the beginning of the loop, so the loop will never begin.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); } }
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); } }
- 01-10-2010, 05:05 AM #11
Member
- Join Date
- Feb 2009
- Posts
- 5
- Rep Power
- 0
THANK YOU
very much
-
You're welcome. And I was wrong. A line from x, y to x, y will draw a very small point. Sorry about the misdirection.
Similar Threads
-
How can I square(^2) the pic in the grid
By racewithferrari in forum New To JavaReplies: 2Last Post: 11-03-2009, 05:27 PM -
How can I square(^2) the pic in the grid
By racewithferrari in forum New To JavaReplies: 1Last Post: 11-01-2009, 10:16 PM -
Database Grid
By leoleo4614 in forum AWT / SwingReplies: 1Last Post: 12-13-2008, 07:02 AM -
how to get the grid values
By jazz2k8 in forum Advanced JavaReplies: 2Last Post: 11-06-2008, 02:11 PM -
Help with Grid Layout
By coco in forum AWT / SwingReplies: 1Last Post: 08-06-2007, 08:03 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks