Java Graphics Draw Grid drawGrid(Color color, Graphics2D g, int maxX, int maxY, int hWidth, int hSpacing, int vWidth, int vSpacing, boolean emptyIntersections)

Here you can find the source of drawGrid(Color color, Graphics2D g, int maxX, int maxY, int hWidth, int hSpacing, int vWidth, int vSpacing, boolean emptyIntersections)

Description

draw Grid

License

Open Source License

Declaration

public static void drawGrid(Color color, Graphics2D g, int maxX, int maxY, int hWidth, int hSpacing, int vWidth,
            int vSpacing, boolean emptyIntersections) 

Method Source Code

//package com.java2s;
/*//from  ww w  .  ja  v  a2 s .  c o m
 * Copyright 2015 Laszlo Balazs-Csiki
 *
 * This file is part of Pixelitor. Pixelitor is free software: you
 * can redistribute it and/or modify it under the terms of the GNU
 * General Public License, version 3 as published by the Free
 * Software Foundation.
 *
 * Pixelitor is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Pixelitor. If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;

import java.awt.Graphics2D;

public class Main {
    public static void drawGrid(Color color, Graphics2D g, int maxX, int maxY, int hWidth, int hSpacing, int vWidth,
            int vSpacing, boolean emptyIntersections) {
        if (hWidth < 0) {
            throw new IllegalArgumentException("hWidth = " + hWidth);
        }
        if (vWidth < 0) {
            throw new IllegalArgumentException("vWidth = " + vWidth);
        }
        if (hSpacing <= 0) {
            throw new IllegalArgumentException("hSpacing = " + hSpacing);
        }
        if (vSpacing <= 0) {
            throw new IllegalArgumentException("vSpacing = " + vSpacing);
        }

        g.setColor(color);

        Composite savedComposite = g.getComposite();
        if (emptyIntersections) {
            g.setComposite(AlphaComposite.Xor);
        }

        int halfHWidth = hWidth / 2;
        int halfVWidth = vWidth / 2;

        // horizontal lines
        if (hWidth > 0) {
            for (int y = 0; y < maxY; y += vSpacing) {
                int startY = y - halfVWidth;
                g.fillRect(0, startY, maxX, vWidth);
            }
        }

        // vertical lines
        if (vWidth > 0) {
            for (int x = 0; x < maxX; x += hSpacing) {
                g.fillRect(x - halfHWidth, 0, hWidth, maxY);
            }
        }

        if (emptyIntersections) {
            g.setComposite(savedComposite);
        }

    }
}

Related

  1. drawBrickGrid(Color color, Graphics2D g, int size, int maxX, int maxY)
  2. drawGrid(Graphics2D g, double lowerX, double upperX, int numX, double lowerY, double upperY, int numY)
  3. drawIntegralCoordinateGrid(Graphics2D g, Component comp, int delta)
  4. paintGrid(Graphics g, int width, int height, Color darkColor, Color brightColor)