/************************************************************************** /* 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.test; import java.util.*; import java.awt.*; import java.awt.print.*; import java.awt.event.*; import java.awt.geom.*; import javax.swing.*; import de.bablokb.plotlib.core.*; import de.bablokb.plotlib.plot.*; import de.bablokb.plotlib.data.*; /** Test various layout issues. Calculate the Optimal range for function-plots with the DatasetTracker and DefaultRange objects.

FIXME: this needs some rethought and update, since the creation of the data depends on the size of the current graphics-area, and the range depends on the data. So we need to create the data for every paint-event, which is not optimal. Also, change the structure of the methods of this class, to make the logical flow clear. @version $Revision: 1.5 $ @author $Author: bablokb $ */ public class LayoutTest extends Canvas { private static final float XMIN = -0.5f, XMAX = 6f, YMIN = -0.05f, YMAX = 0.55f, MU = 3.0f, SIGMA1 = 1.0f, SIGMA2 = 0.8f; private PlottingContext iPC; private PlotList iList; private NormalDensity iDensity, iDensity2; ///////////////////////////////////////////////////////////////////////////// /** Constructor. */ public LayoutTest() { iPC = new PlottingContext(); // iPC.setRange(new Rectangle2D.Float(XMIN,YMIN,XMAX-XMIN,YMAX-YMIN)); // set range dynamically from the data iList = new PlotList(); } ///////////////////////////////////////////////////////////////////////////// /** Main-method: Create a JFrame, add widgets and show the frame. */ public static void main(String[] args) { JFrame frame = new JFrame("Density of Normal Distribution"); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); }}); LayoutTest f = new LayoutTest(); frame.getContentPane().add(f); frame.pack(); frame.show(); } ///////////////////////////////////////////////////////////////////////////// /** Add objects to the PlotList object. */ private void addObjects() { DatasetTracker t = new DatasetTracker(); iDensity = new NormalDensity(MU,SIGMA1); FunctionPlot p = new FunctionPlot(iDensity); FunctionPlotProperties pp = new FunctionPlotProperties(); pp.setColor(Color.blue); p.setProperties(pp); p.setRange(XMIN,XMAX,(XMAX-XMIN)/getSize().getWidth()); p.createData(iPC); t.add(p.getData()); iList.add(p); iDensity2 = new NormalDensity(MU,SIGMA2); FunctionPlot p2 = new FunctionPlot(iDensity2); FunctionPlotProperties pp2 = new FunctionPlotProperties(); pp2.setColor(Color.red); p2.setProperties(pp2); p2.setRange(XMIN,XMAX,(XMAX-XMIN)/getSize().getWidth()); p2.createData(iPC); t.add(p2.getData()); iList.add(p2); createXAxis(); createYAxis(); createSpecials(); DefaultRange r = new DefaultRange(); // r.setGlueType(DefaultRange.ABSOLUTE); r.setGlueX(0.1f); r.setGlueY(0.1f); r.setDatasets(t); iPC.setRange(r.getRange()); } ///////////////////////////////////////////////////////////////////////////// /** Create X-Axis. The x-axis has major and minor tickmarks, major tickmark labels, but no axis-label. */ private void createXAxis() { // first, setup property objects BasicProperties baseProps = new BasicProperties(); //baseProps.setColor(Color.red); AxisProperties axisProps = new AxisProperties(baseProps); axisProps.setHeadSymbolType(Plotlib.EARROW); axisProps.setHeadSymbolSize(Plotlib.LARGE); axisProps.setTailSymbolType(Plotlib.NONE); TickMarkProperties majorProperties = new TickMarkProperties(baseProps); majorProperties.setTickMarksType(Plotlib.MAJOR); majorProperties.setSize(Plotlib.LARGE); majorProperties.setOrientation(Plotlib.HORIZONTAL); TickMarkProperties minorProperties = new TickMarkProperties(baseProps); minorProperties.setTickMarksType(Plotlib.MINOR); minorProperties.setSize(Plotlib.BIG); minorProperties.setOrientation(Plotlib.HORIZONTAL); // create components: tick-marks TickMarks majorTicks = new TickMarks(); majorTicks.setProperties(majorProperties); float xOffset = -0.025f; for (float x=(float) Math.ceil(XMIN); x<=Math.floor(XMAX); x+=1.0) { Point2D.Float p = new Point2D.Float(x,0); majorTicks.addPoint(p); majorTicks.addLabel(new PLabel(String.valueOf(x), new Point2D.Float(x,xOffset))); } TickMarks minorTicks = new TickMarks(); minorTicks.setProperties(minorProperties); for (float x=XMIN; x<=XMAX; x+=0.1) { Point2D.Float p = new Point2D.Float(x,0); minorTicks.addPoint(p); } // finally, add everything to the axis and the axis to the list Axis axis = new Axis(XMIN,0,XMAX,0); axis.setProperties(axisProps); axis.setMajorTickMarks(majorTicks); axis.setMinorTickMarks(minorTicks); iList.add(axis); } ///////////////////////////////////////////////////////////////////////////// /** Create Y-Axis. The Y-Axis has major tickmarks with labels, and an axis-label, but no minor-tickmarks. */ private void createYAxis() { // first, setup property objects BasicProperties baseProps = new BasicProperties(); //baseProps.setColor(Color.red); AxisProperties axisProps = new AxisProperties(baseProps); axisProps.setHeadSymbolType(Plotlib.NARROW); axisProps.setHeadSymbolSize(Plotlib.LARGE); axisProps.setTailSymbolType(Plotlib.NONE); TickMarkProperties majorProperties = new TickMarkProperties(baseProps); majorProperties.setTickMarksType(Plotlib.MAJOR); majorProperties.setSize(Plotlib.LARGE); majorProperties.setOrientation(Plotlib.VERTICAL); // create components: tick-marks TickMarks majorTicks = new TickMarks(); majorTicks.setProperties(majorProperties); for (float y=0; y<=YMAX; y+=0.1) { Point2D.Float p = new Point2D.Float(0,y); majorTicks.addPoint(p); majorTicks.addLabel(new PLabel(String.valueOf(y), new Point2D.Float(XMIN,y))); } // create components: label PLabel label = new PLabel("f(x)", new Point2D.Float(0.04f,(float) (YMAX-0.04))); // finally, add everything to the axis and the axis to the list Axis axis = new Axis(0,0,0,YMAX); axis.setProperties(axisProps); axis.setMajorTickMarks(majorTicks); axis.setLabel(label); iList.add(axis); } ///////////////////////////////////////////////////////////////////////////// /** Create barplot-type lines */ private void createSpecials() { Lines l = new Lines(); float x = iDensity.getMu()-iDensity.getSigma(); Line2D.Float l2d = new Line2D.Float(x,0.0f,x,(float) iDensity.f(x).getY()); l.add(l2d); x = iDensity.getMu(); l2d = new Line2D.Float(x,0.0f,x,(float) iDensity.f(x).getY()); l.add(l2d); x = iDensity.getMu()+iDensity.getSigma(); l2d = new Line2D.Float(x,0.0f,x,(float) iDensity.f(x).getY()); l.add(l2d); LineProperties lp = new LineProperties(); lp.setDashType(Plotlib.DOT); l.setProperties(lp); iList.add(l); } ///////////////////////////////////////////////////////////////////////////// /** Return preferred size. */ public Dimension getPreferredSize() { return getMinimumSize(); } ///////////////////////////////////////////////////////////////////////////// /** Return minimal size. */ public Dimension getMinimumSize() { return new Dimension((int) ((XMAX-XMIN)*50),(int) ((YMAX-YMIN)*500)); } ///////////////////////////////////////////////////////////////////////////// /** Paint all objects. */ public void paint(Graphics g) { iPC.setSize(getSize()); iPC.setG2((Graphics2D) g); iPC.setDefaults(); addObjects(); iList.plot(iPC); } }