/************************************************************************** /* This class is part of the Java Plotlib Toolkit. /* /* Copyright (c) 1999-2004 by Bernhard Bablok (bablokb@gmx.de) /* /* This program is free software; you can redistribute it and/or modify /* it under the terms of the GNU General Public License as published /* by the Free Software Foundation; either version 2 of the License or /* (at your option) any later version. /* /* This program 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 Library General Public License for more details. /* /* You should have received a copy of the GNU General Public License /* along with this program; see the file COPYING. If not, write to /* the Free Software Foundation Inc., 59 Temple Place - Suite 330, /* Boston, MA 02111-1307 USA /**************************************************************************/ package de.bablokb.plotlib.core; import java.util.*; import java.awt.*; import java.awt.geom.*; /** Plot a list of parallel lines defined by a list of points, the orientation of the lines and the given range. If no range is supplied, use the range defined by the PlottingContext. @version $Revision: 1.3 $ @author $Author: bablokb $ */ public class Grid extends AbstractPlottable { ///////////////////////////////////////////////////////////////////////////// /** List of points of type Point2D. Note that this is not verified. */ private java.util.List iPoints = new ArrayList(); ///////////////////////////////////////////////////////////////////////////// /** Range in mathematical coordinates for the grid. This defaults to the range of the PlottingContext. */ private Rectangle2D iRange = null; ///////////////////////////////////////////////////////////////////////////// /** Default constructor. */ public Grid() { } ///////////////////////////////////////////////////////////////////////////// /** Create a Grid with given list of points. Note that the elements of the list should be objects of the Point2D class, but this is not verified. */ public Grid(java.util.List points) { iPoints = points; } ///////////////////////////////////////////////////////////////////////////// /** Sets properties-object. */ public void setProperties(GridProperties gp) { iProperties = gp; } ///////////////////////////////////////////////////////////////////////////// /** Set list of points. Note that the elements of the list should be objects of the Point2D class, but this is not verified. */ public void setPoints(java.util.List l) { iPoints = l; } ///////////////////////////////////////////////////////////////////////////// /** Query list of points. */ public java.util.List getPoints() { return iPoints; } ///////////////////////////////////////////////////////////////////////////// /** Set the range. */ public void setRange(Rectangle2D range) { iRange = range; } ///////////////////////////////////////////////////////////////////////////// /** Query the range. */ public Rectangle2D getRange() { return iRange; } ///////////////////////////////////////////////////////////////////////////// /** Plot the grid. Implements AbstractPlottable.plotObjects. */ public void plotObjects(PlottingContext pc) { if (iPoints == null) return; Rectangle2D r; if (iRange == null) r = pc.getRange(); else r = iRange; int orientation = ((GridProperties) iProperties).getOrientation(); Point2D.Float start, end; Graphics2D g2 = pc.getG2(); if (orientation == Plotlib.VERTICAL) { start = new Point2D.Float(0.0f,(float) r.getY()); end = new Point2D.Float(0.0f,(float) (start.getY() + r.getHeight())); } else { start = new Point2D.Float((float) r.getX(),0.0f); end = new Point2D.Float((float) (start.getX() + r.getWidth()),0.0f); } r = pc.transform(r); start = (Point2D.Float) pc.transform(start); end = (Point2D.Float) pc.transform(end); Shape oldClip = g2.getClip(); g2.setClip(r); GeneralPath gp = new GeneralPath(); ListIterator lit = iPoints.listIterator(); // iterate all points while (lit.hasNext()) { Point2D p = pc.transform((Point2D) lit.next()); if (orientation == Plotlib.VERTICAL) { gp.moveTo((float) p.getX(),start.y); gp.lineTo((float) p.getX(),end.y); } else { gp.moveTo(start.x,(float) p.getY()); gp.lineTo(end.x,(float) p.getY()); } } g2.draw(gp); g2.setClip(oldClip); } }