draw Grid - Java 2D Graphics

Java examples for 2D Graphics:Line

Description

draw Grid

Demo Code


//package com.java2s;
import java.awt.*;
import java.awt.geom.Line2D;

public class Main {
    /**/* w  w  w  . j  a  v  a  2s  .co  m*/
     *
     * @param g Graphics to paint grid
     * @param x Coords x
     * @param y Coords y
     * @param cellWidth Width of each cell
     * @param cellHeight Height of each cell
     * @param rows Amount of cells to draw
     * @param columns Amount of columns to draw
     */
    public static void drawGrid(Graphics2D g, int x, int y, int cellWidth,
            int cellHeight, int rows, int columns) {
        for (int i = 0; i <= columns; i++) {
            g.draw(new Line2D.Double(x + cellWidth * i, y, x + cellWidth
                    * i, y + rows * cellHeight));
        }
        for (int i = 0; i <= rows; i++)
            g.draw(new Line2D.Double(x, y + cellHeight * i, x + columns
                    * cellWidth, y + cellHeight * i));
    }
}

Related Tutorials