Java Graphics Draw Grid drawIntegralCoordinateGrid(Graphics2D g, Component comp, int delta)

Here you can find the source of drawIntegralCoordinateGrid(Graphics2D g, Component comp, int delta)

Description

draws a coordinate grid with one length unit distance in each direction.

License

Open Source License

Parameter

Parameter Description
g the Graphics2D context
comp the component to draw on

Declaration


public static void drawIntegralCoordinateGrid(Graphics2D g, Component comp, int delta) 

Method Source Code


//package com.java2s;
/*/*ww  w.j  a v  a2  s.c o m*/
************************************************************************
*******************  CANADIAN ASTRONOMY DATA CENTRE  *******************
**************  CENTRE CANADIEN DE DONN?ES ASTRONOMIQUES  **************
*
*  (c) 2011.                            (c) 2011.
*  Government of Canada                 Gouvernement du Canada
*  National Research Council            Conseil national de recherches
*  Ottawa, Canada, K1A 0R6              Ottawa, Canada, K1A 0R6
*  All rights reserved                  Tous droits r?serv?s
*
*  NRC disclaims any warranties,        Le CNRC d?nie toute garantie
*  expressed, implied, or               ?nonc?e, implicite ou l?gale,
*  statutory, of any kind with          de quelque nature que ce
*  respect to the software,             soit, concernant le logiciel,
*  including without limitation         y compris sans restriction
*  any warranty of merchantability      toute garantie de valeur
*  or fitness for a particular          marchande ou de pertinence
*  purpose. NRC shall not be            pour un usage particulier.
*  liable in any event for any          Le CNRC ne pourra en aucun cas
*  damages, whether direct or           ?tre tenu responsable de tout
*  indirect, special or general,        dommage, direct ou indirect,
*  consequential or incidental,         particulier ou g?n?ral,
*  arising from the use of the          accessoire ou fortuit, r?sultant
*  software.  Neither the name          de l'utilisation du logiciel. Ni
*  of the National Research             le nom du Conseil National de
*  Council of Canada nor the            Recherches du Canada ni les noms
*  names of its contributors may        de ses  participants ne peuvent
*  be used to endorse or promote        ?tre utilis?s pour approuver ou
*  products derived from this           promouvoir les produits d?riv?s
*  software without specific prior      de ce logiciel sans autorisation
*  written permission.                  pr?alable et particuli?re
*                                       par ?crit.
*
*  This file is part of the             Ce fichier fait partie du projet
*  OpenCADC project.                    OpenCADC.
*
*  OpenCADC is free software:           OpenCADC est un logiciel libre ;
*  you can redistribute it and/or       vous pouvez le redistribuer ou le
*  modify it under the terms of         modifier suivant les termes de
*  the GNU Affero General Public        la ?GNU Affero General Public
*  License as published by the          License?? telle que publi?e
*  Free Software Foundation,            par la Free Software Foundation
*  either version 3 of the              : soit la version 3 de cette
*  License, or (at your option)         licence, soit (? votre gr?)
*  any later version.                   toute version ult?rieure.
*
*  OpenCADC is distributed in the       OpenCADC est distribu?
*  hope that it will be useful,         dans l?espoir qu?il vous
*  but WITHOUT ANY WARRANTY;            sera utile, mais SANS AUCUNE
*  without even the implied             GARANTIE : sans m?me la garantie
*  warranty of MERCHANTABILITY          implicite de COMMERCIALISABILIT?
*  or FITNESS FOR A PARTICULAR          ni d?AD?QUATION ? UN OBJECTIF
*  PURPOSE.  See the GNU Affero         PARTICULIER. Consultez la Licence
*  General Public License for           G?n?rale Publique GNU Affero
*  more details.                        pour plus de d?tails.
*
*  You should have received             Vous devriez avoir re?u une
*  a copy of the GNU Affero             copie de la Licence G?n?rale
*  General Public License along         Publique GNU Affero avec
*  with OpenCADC.  If not, see          OpenCADC ; si ce n?est
*  <http://www.gnu.org/licenses/>.      pas le cas, consultez :
*                                       <http://www.gnu.org/licenses/>.
*
*  $Revision: 5 $
*
************************************************************************
*/

import java.awt.Component;
import java.awt.Container;
import java.awt.Graphics2D;

import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath;
import java.awt.geom.Point2D;

public class Main {
    /**
     * draws a coordinate grid with one length unit distance in each direction.
     *
     * @param g    the Graphics2D context
     * @param comp the component to draw on
     */

    public static void drawIntegralCoordinateGrid(Graphics2D g, Component comp, int delta) {
        AffineTransform at = g.getTransform();
        AffineTransform bt = new AffineTransform();
        try {
            bt = at.createInverse();
        } catch (Exception e) {
            System.out.println("Non-invertible transform");
        }

        Container c = comp.getParent();
        //int xx=comp.getX(),yy=comp.getY(),Dx=comp.getWidth(), Dy=comp.getHeight();
        int xx = c.getX();
        int yy = c.getY();
        int dx = c.getWidth();
        int dy = c.getHeight();
        Point2D.Double[] p = new Point2D.Double[2];
        Point2D.Double[] q = new Point2D.Double[2];
        p[0] = new Point2D.Double(xx, yy);
        p[1] = new Point2D.Double(xx + dx, yy + dy);
        for (int i = 0; i < 2; i++) {
            q[i] = new Point2D.Double();
            bt.transform(p[i], q[i]);
        }
        double lowerX = q[0].x;
        double upperX = q[1].x;
        double y0 = q[0].y;
        double y1 = q[1].y;
        GeneralPath gp = new GeneralPath();
        double x;
        double y;
        x = Math.ceil(lowerX);
        while (x < upperX) {
            gp.moveTo((float) x, (float) y0);
            gp.lineTo((float) x, (float) y1);
            x++;
        }
        double lowerY = y0 < y1 ? y0 : y1;
        double upperY = y0 < y1 ? y1 : y0;
        y = Math.ceil(lowerY);
        while (y < upperY) {
            gp.moveTo((float) lowerX, (float) y);
            gp.lineTo((float) upperX, (float) y);
            y++;
        }
        g.draw(gp);

        //g.setColor(Color.lightGray);
        gp.reset();
        x = Math.ceil(lowerX);
        while (x < upperX) {
            if (x % delta == 0) {
                gp.moveTo((float) x, (float) y0);
                gp.lineTo((float) x, (float) y1);
            }
            x++;
        }
        y = Math.ceil(lowerY);
        while (y < upperY) {
            if (y % delta == 0) {
                gp.moveTo((float) lowerX, (float) y);
                gp.lineTo((float) upperX, (float) y);
            }
            y++;
        }
        g.draw(gp);

    }
}

Related

  1. drawBrickGrid(Color color, Graphics2D g, int size, int maxX, int maxY)
  2. drawGrid(Color color, Graphics2D g, int maxX, int maxY, int hWidth, int hSpacing, int vWidth, int vSpacing, boolean emptyIntersections)
  3. drawGrid(Graphics2D g, double lowerX, double upperX, int numX, double lowerY, double upperY, int numY)
  4. paintGrid(Graphics g, int width, int height, Color darkColor, Color brightColor)