/************************************************************************** /* 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.event.*; import java.awt.geom.*; import javax.swing.*; import de.bablokb.plotlib.core.*; /** This class tests grids and borders. It also shows how to use two PlottingContexts and how to divide the drawing area into two separate parts. The left part uses the whole size, the right centers the plot and leaves 10% unused. @version $Revision: 1.5 $ @author $Author: bablokb $ */ public class GridTest extends Canvas { private PlottingContext iLeftPC, iRightPC; private PlotList iLeftList,iRightList; ///////////////////////////////////////////////////////////////////////////// /** Constructor. */ public GridTest() { iLeftPC = new PlottingContext(); iRightPC = new PlottingContext(); iLeftList = new PlotList(); iRightList = new PlotList(); iLeftPC.setRange(new Rectangle2D.Float(-10.0f,-10.0f,40.0f,20.0f)); iRightPC.setRange(new Rectangle2D.Float(10.0f,10.0f,40.0f,20.0f)); } ///////////////////////////////////////////////////////////////////////////// /** Main-method: Create a JFrame, add a GridTest-Object and show the frame. */ public static void main(String[] args) { JFrame frame = new JFrame("Plotlib Test"); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); }}); GridTest t = new GridTest(); t.addPlotObjects(); frame.getContentPane().add(t); frame.pack(); frame.show(); } ///////////////////////////////////////////////////////////////////////////// /** Add objects to the PlotList objects. */ private void addPlotObjects() { addLeftObjects(); addRightObjects(); } ///////////////////////////////////////////////////////////////////////////// /** Add objects to the left PlotList. */ private void addLeftObjects() { // create a green border BorderProperties borderProps = new BorderProperties(); borderProps.setColor(Color.green); borderProps.setWidth(Plotlib.MEDIUM); Border leftBorder = new Border(); leftBorder.setProperties(borderProps); // create a green vertical grid GridProperties verticalGridProps = new GridProperties(); verticalGridProps.setOrientation(Plotlib.VERTICAL); verticalGridProps.setColor(Color.green); ArrayList verticalGridPoints = new ArrayList(); for (float x=-5;x<30;x+=10) verticalGridPoints.add(new Point2D.Float(x,0)); Grid verticalGrid = new Grid(verticalGridPoints); verticalGrid.setProperties(verticalGridProps); // create a green horizontal grid with dotted lines GridProperties horizontalGridProps = new GridProperties(); horizontalGridProps.setColor(Color.green); horizontalGridProps.setDashType(Plotlib.DOT); ArrayList horizontalGridPoints = new ArrayList(); for (float y=-8;y<10;y+=2) horizontalGridPoints.add(new Point2D.Float(0,y)); Grid horizontalGrid = new Grid(); // this time, set horizontalGrid.setPoints(horizontalGridPoints); // points with set-method horizontalGrid.setProperties(horizontalGridProps); // add objects to list iLeftList.add(leftBorder); iLeftList.add(verticalGrid); iLeftList.add(horizontalGrid); } ///////////////////////////////////////////////////////////////////////////// /** Add objects to the right PlotList. */ private void addRightObjects() { // create clipping rectangle Rectangle2D.Float clippingRect = new Rectangle2D.Float(20.0f,15.0f,20.0f,10.0f); // create a blue, thick border BorderProperties borderProps = new BorderProperties(); borderProps.setColor(Color.blue); borderProps.setWidth(Plotlib.WIDE); Border border1 = new Border(); border1.setProperties(borderProps); // create a blue, dotted horizontal grid clipped to a rectangle GridProperties horizontalGridProps = new GridProperties(); horizontalGridProps.setColor(Color.blue); horizontalGridProps.setDashType(Plotlib.DOT); ArrayList horizontalGridPoints = new ArrayList(); for (float y=10;y<=30;y+=2) horizontalGridPoints.add(new Point2D.Float(0,y)); Grid horizontalGrid = new Grid(); horizontalGrid.setPoints(horizontalGridPoints); horizontalGrid.setProperties(horizontalGridProps); horizontalGrid.setRange(clippingRect); // create a blue vertical grid with default lines (not clipped) GridProperties verticalGridProps = new GridProperties(); verticalGridProps.setOrientation(Plotlib.VERTICAL); verticalGridProps.setColor(Color.blue); ArrayList verticalGridPoints = new ArrayList(); for (float x=10;x<=50;x+=10) verticalGridPoints.add(new Point2D.Float(x,0)); Grid verticalGrid = new Grid(verticalGridPoints); verticalGrid.setProperties(verticalGridProps); // draw a border (red, thin, dashed) around the clipping rectangle BorderProperties borderProps2 = new BorderProperties(); borderProps2.setColor(Color.red); borderProps2.setWidth(Plotlib.THIN); borderProps2.setDashType(Plotlib.DASH); Border border2 = new Border(); border2.setBorder(clippingRect); border2.setProperties(borderProps2); // add objects to list iRightList.add(border1); iRightList.add(horizontalGrid); iRightList.add(verticalGrid); iRightList.add(border2); } ///////////////////////////////////////////////////////////////////////////// /** Return preferred size. */ public Dimension getPreferredSize() { return getMinimumSize(); } ///////////////////////////////////////////////////////////////////////////// /** Return minimal size. */ public Dimension getMinimumSize() { return new Dimension(400,200); } ///////////////////////////////////////////////////////////////////////////// /** Paint all objects.

The left side of the panel occupies half of the area. Since we don't change the origin, we only need to set the size of the plotting context to insure the correct translation of mathematical coordinates to Graphics-coordinates. Since the width is only have of the graphics-area, the right side is never touched.

The right side is a bit more complicated. First, we set the (reduced) size of the plotting context analogous to the left side. But since we only want to use the right side of the graphics-area, we have to add a simple affine transformation: a translation. */ public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; Dimension d = getSize(); // The left side of the panel occupies half of the area. // Setting the size is enough: we don't change the origin Dimension l = new Dimension(d); l.width = d.width/2; iLeftPC.setSize(l); iLeftPC.setG2(g2); iLeftPC.setDefaults(); iLeftList.plot(iLeftPC); // The right side of the panel is reduced by 2*5% in each dimension. Dimension r = new Dimension(d); r.width = (int) (0.9*d.width/2); r.height = (int) (0.9*d.height); iRightPC.setSize(r); iRightPC.setG2(g2); iRightPC.setDefaults(); // For the right side, we need a translation, since the origin // changes. The origin starts 55% to the right (right half plus // 5% size reduction) and 5% down. AffineTransform af = AffineTransform.getTranslateInstance(0.55*d.width,0.05*d.height); g2.setTransform(af); iRightList.plot(iRightPC); } }