/************************************************************************** /* 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.*; /** This is the base class for primitive plottable objects, i.e. objects which are no composite objects and therefore have their own properties object.

Since all primitive Plottable objects have to save the state before plotting themselves, a default implementation of plot() is provided. The method plot() saves the state, then calls plotObjects() and after that restores the previous state again. Subclasses of AbstractPlottable therefore only have to implement the plotObjects() method and have to override the saveState() and restoreState() methods.

Subclasses should also implement a typesafe setProperties() method. @version $Revision: 1.3 $ @author $Author: bablokb $ */ public abstract class AbstractPlottable implements Plottable { /** Properties object. */ protected BasicProperties iProperties = null; ///////////////////////////////////////////////////////////////////////////// /** Color object for save/restore of state. */ private Color iColorState = null; ///////////////////////////////////////////////////////////////////////////// /** Stroke object for save/restore of state. */ protected Stroke iStrokeState = null; ///////////////////////////////////////////////////////////////////////////// /** Set properties object. */ public void setProperties(BasicProperties p) { iProperties = p; } ///////////////////////////////////////////////////////////////////////////// /** Query properties object. */ public BasicProperties getProperties() { return iProperties; } ///////////////////////////////////////////////////////////////////////////// /** Save current stroke. */ private void saveStroke(Graphics2D g2) { int width = iProperties.getWidth(); // iProperties != null int dashType = iProperties.getDashType(); if (width != 0 || dashType != Plotlib.SOLID) { iStrokeState = g2.getStroke(); if (dashType != Plotlib.SOLID) g2.setStroke(new BasicStroke(width,BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND,0,Plotlib.DASH_TABLE[dashType],0)); else g2.setStroke(new BasicStroke(width)); } } ///////////////////////////////////////////////////////////////////////////// /** Save the current state. Subclasses might override this method and then call super.saveState(). */ public void saveState(Graphics2D g2) { if (iProperties != null) { Color color = iProperties.getColor(); if (color != null) { iColorState = g2.getColor(); g2.setColor(color); } saveStroke(g2); } } ///////////////////////////////////////////////////////////////////////////// /** Restore the old state. Subclasses might override this method and then call super.restoreState(). */ public void restoreState(Graphics2D g2) { if (iColorState != null) g2.setColor(iColorState); if (iStrokeState != null) g2.setStroke(iStrokeState); } ///////////////////////////////////////////////////////////////////////////// /** Handle attributes and call plotObjects(). */ public void plot(PlottingContext pc) { Graphics2D g2 = pc.getG2(); if (iProperties != null) saveState(g2); plotObjects(pc); if (iProperties != null) restoreState(g2); } ///////////////////////////////////////////////////////////////////////////// /** Plot current object. Must be overridden by subclasses. */ public abstract void plotObjects(PlottingContext pc); }