/************************************************************************** /* 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.*; /** This program tests the FunctionPlot-class, the BarPlot-class and printing.

It displays two normal density functions and a bar-plot. The first normal density is a "smooth" curve, since the x-values are very dense (one x for every graphics-unit). The second normal density uses distinct points, therefore the line is not smooth.

FIXME: Printing does not work for Java 1.4

@version $Revision: 1.4 $ @author $Author: bablokb $ */ public class FktTest extends Canvas implements Runnable, ActionListener, Printable { 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 PrinterJob iPrintJob; ///////////////////////////////////////////////////////////////////////////// /** Constructor. */ public FktTest() { iPC = new PlottingContext(); iPC.setRange(new Rectangle2D.Float(XMIN,YMIN,XMAX-XMIN,YMAX-YMIN)); 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); }}); FktTest f = new FktTest(); f.addObjects(); JButton printButton = new JButton("Print"); printButton.addActionListener(f); frame.getContentPane().add(f,"North"); frame.getContentPane().add(printButton,"South"); frame.pack(); frame.show(); } ///////////////////////////////////////////////////////////////////////////// /** Process the print-button event. Creates a printjob and starts a background thread to process it. */ public void actionPerformed(ActionEvent e) { iPrintJob = PrinterJob.getPrinterJob(); PageFormat pf = iPrintJob.defaultPage(); pf = iPrintJob.pageDialog(pf); iPrintJob.setPrintable((Printable) this,pf); iPrintJob.setJobName("Normal Density"); if (iPrintJob.printDialog()) { Thread t = new Thread(this); t.start(); } } ///////////////////////////////////////////////////////////////////////////// /** run()-method of background thread. */ public void run() { Thread.currentThread().setPriority(4); try { iPrintJob.print(); } catch (Exception ex) { } } ///////////////////////////////////////////////////////////////////////////// /** Add objects to the PlotList object. */ private void addObjects() { // first normal distribution: calculate f(y) for every graphics unit FunctionPlotProperties functionPlotProps1 = new FunctionPlotProperties(); functionPlotProps1.setColor(Color.blue); FunctionPlot functionPlot1 = new FunctionPlot(new NormalDensity(MU,SIGMA1)); functionPlot1.setProperties(functionPlotProps1); // second normal distribution: calculate f(y) for distinct values FunctionPlotProperties functionPlotProps2 = new FunctionPlotProperties(); functionPlotProps2.setColor(Color.red); FunctionPlot functionPlot2 = new FunctionPlot(new NormalDensity(MU,SIGMA2)); functionPlot2.setProperties(functionPlotProps2); functionPlot2.setRange(XMIN,XMAX,0.5f); functionPlot2.createData(iPC); // bar plot (reuse data from functionPlot2) BarPlotProperties barPlotProps = new BarPlotProperties(); barPlotProps.setColor(Color.green); barPlotProps.setWidth(Plotlib.WIDE); BarPlot barPlot = new BarPlot(functionPlot2.getData()); barPlot.setProperties(barPlotProps); // add objects to list iList.add(functionPlot1); iList.add(functionPlot2); iList.add(barPlot); createXAxis(); createYAxis(); } ///////////////////////////////////////////////////////////////////////////// /** 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); } ///////////////////////////////////////////////////////////////////////////// /** 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(); iList.plot(iPC); } ///////////////////////////////////////////////////////////////////////////// /** Print all objects. This method transforms the graphics-area to the clipping-bounds, then calls the paint-method to actually draw the objects. */ public int print(Graphics g, PageFormat pf, int pageIndex) { if (pageIndex == 0) { Graphics2D g2 = (Graphics2D) g; Rectangle r = g2.getClipBounds(); g2.translate(r.x,r.y); paint(g2); return Printable.PAGE_EXISTS; } else { System.out.println("print job finished"); return Printable.NO_SUCH_PAGE; } } }