Draw a grid by drawing lines in Java
Description
The following code shows how to draw a grid by drawing lines.
Example
/* w w w . j a va2 s .c om*/
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
class MyCanvas extends JComponent {
public void paint(Graphics g) {
int rows = 20;
int cols = 30;
int width = getSize().width;
int height = getSize().height;
// draw the rows
int rowHt = height / (rows);
for (int i = 0; i < rows; i++)
g.drawLine(0, i * rowHt, width, i * rowHt);
// draw the columns
int rowWid = width / (cols);
for (int i = 0; i < cols; i++)
g.drawLine(i * rowWid, 0, i * rowWid, height);
}
}
public class Main {
public static void main(String[] a) {
JFrame window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setBounds(30, 30, 450, 450);
window.getContentPane().add(new MyCanvas());
window.setVisible(true);
}
}
The code above generates the following result.