Class PtPlotSurface

All Implemented Interfaces:
ImageObserver, MenuContainer, Printable, Serializable, Accessible, PlotSurface

public class PtPlotSurface extends JPanel implements PlotSurface
PlotSurface implementation which uses Ptplot classes for axis plotting
Since:
17 Jun 2004
Author:
Mark Taylor (Starlink)
See Also:
  • Field Details

    • PTPLOT_RELEASE

      public static final String PTPLOT_RELEASE
      See Also:
    • _yMax

      protected transient double _yMax
    • _yMin

      protected transient double _yMin
    • _xMax

      protected transient double _xMax
    • _xMin

      protected transient double _xMin
    • _padding

      protected double _padding
      The factor we pad by so that we don't plot points on the axes.
    • _xRangeGiven

      protected transient boolean _xRangeGiven
    • _yRangeGiven

      protected transient boolean _yRangeGiven
    • _rangesGivenByZooming

      protected transient boolean _rangesGivenByZooming
    • _xlowgiven

      protected double _xlowgiven
    • _xhighgiven

      protected double _xhighgiven
    • _ylowgiven

      protected double _ylowgiven
    • _yhighgiven

      protected double _yhighgiven
    • _xBottom

      protected double _xBottom
    • _xTop

      protected double _xTop
    • _yBottom

      protected double _yBottom
    • _yTop

      protected double _yTop
    • _xlog

      protected boolean _xlog
    • _ylog

      protected boolean _ylog
    • _xflip

      protected boolean _xflip
    • _yflip

      protected boolean _yflip
    • _LOG10SCALE

      protected static final double _LOG10SCALE
    • _grid

      protected boolean _grid
    • _wrap

      protected boolean _wrap
    • _wrapHigh

      protected double _wrapHigh
    • _wrapLow

      protected double _wrapLow
    • _background

      protected Color _background
    • _foreground

      protected Color _foreground
    • _topPadding

      protected int _topPadding
    • _bottomPadding

      protected int _bottomPadding
    • _rightPadding

      protected int _rightPadding
    • _leftPadding

      protected int _leftPadding
    • _ulx

      protected int _ulx
      The x value of the upper left corner of the plot rectangle in pixels.
    • _uly

      protected int _uly
      The y value of the upper left corner of the plot rectangle in pixels.
    • _lrx

      protected int _lrx
      The x value of the lower right corner of the plot rectangle in pixels.
    • _lry

      protected int _lry
      The y value of the lower right corner of the plot rectangle in pixels.
    • _yscale

      protected double _yscale
      Scaling used for the vertical axis in plotting points. The units are pixels/unit, where unit is the units of the Y axis.
    • _xscale

      protected double _xscale
      Scaling used for the horizontal axis in plotting points. The units are pixels/unit, where unit is the units of the X axis.
    • _usecolor

      protected boolean _usecolor
    • _tickLength

      public int _tickLength
      Length of tickmarks.
    • _colors

      protected static Color[] _colors
    • _width

      protected int _width
    • _height

      protected int _height
    • _preferredWidth

      protected int _preferredWidth
    • _preferredHeight

      protected int _preferredHeight
    • _sizeHasBeenSet

      protected boolean _sizeHasBeenSet
    • _expThreshold

      protected int _expThreshold
    • _documentBase

      public URL _documentBase
    • _labelFont

      protected Font _labelFont
    • _superscriptFont

      protected Font _superscriptFont
    • _titleFont

      protected Font _titleFont
    • _labelFontMetrics

      protected FontMetrics _labelFontMetrics
    • _superscriptFontMetrics

      protected FontMetrics _superscriptFontMetrics
    • _titleFontMetrics

      protected FontMetrics _titleFontMetrics
  • Constructor Details

    • PtPlotSurface

      public PtPlotSurface()
      Constructs a new surface.
  • Method Details

    • setFont

      public void setFont(Font font)
      Overrides:
      setFont in class JComponent
    • setState

      public void setState(PlotState state)
      Description copied from interface: PlotSurface
      Signals to the plot the characteristics of the plot which will be performed. Setting this has no immediate effect, but when the component supplied by PlotSurface.getComponent() next paints itself it should do so following the specifications made here.
      Specified by:
      setState in interface PlotSurface
      Parameters:
      state - plot characteristics
    • setDataRange

      public void setDataRange(double xlo, double ylo, double xhi, double yhi)
      Description copied from interface: PlotSurface
      Requests a range of data space values to be visible on this plotting surface.
      Specified by:
      setDataRange in interface PlotSurface
      Parameters:
      xlo - (approximate) lower bound of X coordinate
      ylo - (approximate) lower bound of Y coordinate
      xhi - (approximate) upper bound of X coordinate
      yhi - (approximate) upper bound of Y coordinate
    • dataToGraphics

      public Point dataToGraphics(double dx, double dy, boolean insideOnly)
      Description copied from interface: PlotSurface
      Converts a point in data space to graphics space. If the insideOnly flag is true, then null will be returned in place of any result which would give a point lying outside the visible plotting area.

      The coordinates of the returned point must have absolute values no greater than PlotSurface.MAX_COORD.

      Specified by:
      dataToGraphics in interface PlotSurface
      Parameters:
      dx - data space X coordinate
      dy - data space Y coordinate
      insideOnly - true to restrict non-null results to those within the plotting surface
      Returns:
      point in graphics space corresponding to (x,y), or null
    • graphicsToData

      public double[] graphicsToData(int px, int py, boolean insideOnly)
      Description copied from interface: PlotSurface
      Converts a point in graphics space to data space. If the insideOnly flag is true, then null will be returned in place of any result which would give a point lying outside the visible plotting area.
      Specified by:
      graphicsToData in interface PlotSurface
      Parameters:
      px - graphics space X coordinate
      py - graphics space Y coordinate
      insideOnly - true to restrict non-null results to those within the plotting surface
      Returns:
      a 2-element array giving x and y data space coordinates, or null
    • getClip

      public Shape getClip()
      Description copied from interface: PlotSurface
      Returns the clip region in which points may be plotted. The returned shape should be the sort which can be passed to Graphics.setClip(java.awt.Shape) - i.e. probably a Rectangle.
      Specified by:
      getClip in interface PlotSurface
      Returns:
      clip region representing data zone
    • getComponent

      public JComponent getComponent()
      Description copied from interface: PlotSurface
      Returns the graphical component on which the plotting surface is displayed. This will contain things like axes, grids, labels etc. This component will normally override JComponent.paintComponent(java.awt.Graphics) to give a plotting background in accordance with the most recently set PlotState.
      Specified by:
      getComponent in interface PlotSurface
      Returns:
      plot surface display component
    • paintSurface

      public void paintSurface(Graphics g)
      Description copied from interface: PlotSurface
      Paints the plotting surface. This should do roughly the same as getComponent.paintComponent, except that it's public.

      Requiring this here isn't very tidy, but following quite a bit of experimentation I can't work out any other way to do scatter plot image caching while still drawing to a potentially hardware-accelerated graphics context (see ScatterPlot implementation).

      Specified by:
      paintSurface in interface PlotSurface
      Parameters:
      g - graphics context
    • _zoom

      protected void _zoom(int x, int y)
    • addLegend

      public void addLegend(int dataset, String legend)
      Add a legend (displayed at the upper right) for the specified data set with the specified string. Short strings generally fit better than long strings. If the string is empty, or the argument is null, then no legend is added.
      Parameters:
      dataset - The dataset index.
      legend - The label for the dataset.
    • addXTick

      public void addXTick(String label, double position)
      Specify a tick mark for the X axis. The label given is placed on the axis at the position given by position. If this is called once or more, automatic generation of tick marks is disabled. The tick mark will appear only if it is within the X range.
      Parameters:
      label - The label for the tick mark.
      position - The position on the X axis.
    • addYTick

      public void addYTick(String label, double position)
      Specify a tick mark for the Y axis. The label given is placed on the axis at the position given by position. If this is called once or more, automatic generation of tick marks is disabled. The tick mark will appear only if it is within the Y range.
      Parameters:
      label - The label for the tick mark.
      position - The position on the Y axis.
    • clear

      public void clear(boolean axes)
      If the argument is true, clear the axes. I.e., set all parameters controlling the axes to their initial conditions. For the change to take effect, call repaint(). If the argument is false, do nothing.
      Parameters:
      axes - If true, clear the axes parameters.
    • clearLegends

      public void clearLegends()
      Clear all legends. This will show up on the next redraw.
    • deferIfNecessary

      public void deferIfNecessary(Runnable action)
      If this method is called in the event thread, then simply execute the specified action. Otherwise, if there are already deferred actions, then add the specified one to the list. Otherwise, create a list of deferred actions, if necessary, and request that the list be processed in the event dispatch thread. Note that it does not work nearly as well to simply schedule the action yourself on the event thread because if there are a large number of actions, then the event thread will not be able to keep up. By grouping these actions, we avoid this problem. This method is not synchronized, so the caller should be.
      Parameters:
      action - The Runnable object to execute.
    • fillPlot

      public void fillPlot()
      Rescale so that the data that is currently plotted just fits. This is done based on the protected variables _xBottom, _xTop, _yBottom, and _yTop. It is up to derived classes to ensure that variables are valid. This method calls repaint(), which eventually causes the display to be updated.
    • getColor

      public boolean getColor()
      Return whether the plot uses color.
      Returns:
      True if the plot uses color.
    • getColorByName

      public static Color getColorByName(String name)
      Convert a color name into a Color. Currently, only a very limited set of color names is supported: black, white, red, green, and blue.
      Parameters:
      name - A color name, or null if not found.
      Returns:
      An instance of Color.
    • getDataurl

      public String getDataurl()
      Deprecated.
      Get the file specification that was given by setDataurl. This method is deprecated. Use read() instead.
    • getDocumentBase

      public URL getDocumentBase()
      Deprecated.
      Get the document base that was set by setDocumentBase. This method is deprecated. Use read() instead.
    • getGrid

      public boolean getGrid()
      Return whether the grid is drawn.
      Returns:
      True if a grid is drawn.
    • getLegend

      public String getLegend(int dataset)
      Get the legend for a dataset, or null if there is none. The legend would have been set by addLegend().
      Parameters:
      dataset - The dataset index.
      Returns:
      The legend label, or null if there is none.
    • getMaximumSize

      public Dimension getMaximumSize()
      If the size of the plot has been set by setSize(), then return that size. Otherwise, return what the superclass returns (which is undocumented, but apparently imposes no maximum size). Currently (JDK 1.3), only BoxLayout pays any attention to this.
      Overrides:
      getMaximumSize in class JComponent
      Returns:
      The maximum desired size.
    • getMinimumSize

      public Dimension getMinimumSize()
      Get the minimum size of this component. This is simply the dimensions specified by setSize(), if this has been called. Otherwise, return whatever the base class returns, which is undocumented.
      Overrides:
      getMinimumSize in class JComponent
      Returns:
      The minimum size.
    • getTitle

      public String getTitle()
      Get the title of the graph, or an empty string if there is none.
      Returns:
      The title.
    • getXLabel

      public String getXLabel()
      Get the label for the X (horizontal) axis, or null if none has been set.
      Returns:
      The X label.
    • getXLog

      public boolean getXLog()
      Return whether the X axis is drawn with a logarithmic scale.
      Returns:
      True if the X axis is logarithmic.
    • getXRange

      public double[] getXRange()
      Get the X range. The returned value is an array where the first element is the minimum and the second element is the maximum. return The current X range.
    • getXTicks

      public Vector[] getXTicks()
      Get the X ticks that have been specified, or null if none. The return value is an array with two vectors, the first of which specifies the X tick locations (as instances of Double), and the second of which specifies the corresponding labels.
      Returns:
      The X ticks.
    • getYLabel

      public String getYLabel()
      Get the label for the Y (vertical) axis, or null if none has been set.
      Returns:
      The Y label.
    • getYLog

      public boolean getYLog()
      Return whether the Y axis is drawn with a logarithmic scale.
      Returns:
      True if the Y axis is logarithmic.
    • getYRange

      public double[] getYRange()
      Get the Y range. The returned value is an array where the first element is the minimum and the second element is the maximum. return The current Y range.
    • getYTicks

      public Vector[] getYTicks()
      Get the Y ticks that have been specified, or null if none. The return value is an array with two vectors, the first of which specifies the Y tick locations (as instances of Double), and the second of which specifies the corresponding labels.
      Returns:
      The Y ticks.
    • init

      public void init()
      Deprecated.
      Initialize the component, creating the fill button and parsing an input file, if one has been specified. This is deprecated. Call setButtons() and read() instead.
    • paintComponent

      public void paintComponent(Graphics graphics)
      Paint the component contents, which in this base class is only the axes.
      Overrides:
      paintComponent in class JComponent
      Parameters:
      graphics - The graphics context.
    • print

      public int print(Graphics graphics, PageFormat format, int index) throws PrinterException
      Print the plot to a printer, represented by the specified graphics object.
      Specified by:
      print in interface Printable
      Parameters:
      graphics - The context into which the page is drawn.
      format - The size and orientation of the page being drawn.
      index - The zero based index of the page to be drawn.
      Returns:
      PAGE_EXISTS if the page is rendered successfully, or NO_SUCH_PAGE if pageIndex specifies a non-existent page.
      Throws:
      PrinterException - If the print job is terminated.
    • removeLegend

      public void removeLegend(int dataset)
      Remove the legend (displayed at the upper right) for the specified data set. If the dataset is not found, nothing will occur. The PlotBox must be repainted in order for this to take effect.
      Parameters:
      dataset - The dataset index.
    • resetAxes

      public void resetAxes()
      Reset the X and Y axes to the ranges that were first specified using setXRange() and setYRange(). If these methods have not been called, then reset to the default ranges. This method calls repaint(), which eventually causes the display to be updated.
    • samplePlot

      public void samplePlot()
      Do nothing in this base class. Derived classes might want to override this class to give an example of their use.
    • setBackground

      public void setBackground(Color background)
      Set the background color.
      Overrides:
      setBackground in class JComponent
      Parameters:
      background - The background color.
    • setBounds

      public void setBounds(int x, int y, int width, int height)
      Move and resize this component. The new location of the top-left corner is specified by x and y, and the new size is specified by width and height. This overrides the base class method to make a record of the new size.
      Overrides:
      setBounds in class Component
      Parameters:
      x - The new x-coordinate of this component.
      y - The new y-coordinate of this component.
      width - The new width of this component.
      height - The new height of this component.
    • setButtons

      public void setButtons(boolean visible)
      If the argument is true, make a fill button visible at the upper right. This button auto-scales the plot. NOTE: The button may infringe on the title space, if the title is long. In an application, it is preferable to provide a menu with the fill command. This way, when printing the plot, the printed plot will not have a spurious button. Thus, this method should be used only by applets, which normally do not have menus. This method should only be called from within the event dispatch thread, since it interacts with swing.
    • setColor

      public void setColor(boolean useColor)
      If the argument is false, draw the plot without using color (in black and white). Otherwise, draw it in color (the default).
      Parameters:
      useColor - False to draw in back and white.
    • setDataurl

      public void setDataurl(String filespec)
      Deprecated.
      Set the file to read when init() is called. This method is deprecated. Use read() instead.
    • setDocumentBase

      public void setDocumentBase(URL documentBase)
      Deprecated.
      Set the document base to used when init() is called to read a URL. This method is deprecated. Use read() instead.
    • setForeground

      public void setForeground(Color foreground)
      Set the foreground color.
      Overrides:
      setForeground in class JComponent
      Parameters:
      foreground - The foreground color.
    • setGrid

      public void setGrid(boolean grid)
      Control whether the grid is drawn.
      Parameters:
      grid - If true, a grid is drawn.
    • setLabelFont

      public void setLabelFont(String name)
      Set the label font, which is used for axis labels and legend labels. The font names understood are those understood by java.awt.Font.decode().
      Parameters:
      name - A font name.
    • setSize

      public void setSize(int width, int height)
      Set the size of the plot. This overrides the base class to make it work. In particular, it records the specified size so that getMinimumSize() and getPreferredSize() return the specified value. However, it only works if the plot is placed in its own JPanel. This is because the JPanel asks the contained component for its preferred size before determining the size of the panel. If the plot is placed directly in the content pane of a JApplet, then, mysteriously, this method has no effect.
      Overrides:
      setSize in class Component
      Parameters:
      width - The width, in pixels.
      height - The height, in pixels.
    • setTitle

      public void setTitle(String title)
      Set the title of the graph.
      Parameters:
      title - The title.
    • setTitleFont

      public void setTitleFont(String name)
      Set the title font. The font names understood are those understood by java.awt.Font.decode().
      Parameters:
      name - A font name.
    • setWrap

      public void setWrap(boolean wrap)
      Specify whether the X axis is wrapped. If it is, then X values that are out of range are remapped to be in range using modulo arithmetic. The X range is determined by the most recent call to setXRange() (or the most recent zoom). If the X range has not been set, then use the default X range, or if data has been plotted, then the current fill range.
      Parameters:
      wrap - If true, wrapping of the X axis is enabled.
    • setXLabel

      public void setXLabel(String label)
      Set the label for the X (horizontal) axis.
      Parameters:
      label - The label.
    • setXLog

      public void setXLog(boolean xlog)
      Specify whether the X axis is drawn with a logarithmic scale. If you would like to have the X axis drawn with a logarithmic axis, then setXLog(true) should be called before adding any data points.
      Parameters:
      xlog - If true, logarithmic axis is used.
    • setXFlip

      public void setXFlip(boolean xflip)
      Specify whether the X axis numeric labels should be inverted.
      Parameters:
      xflip - If true, numbers on X axis will be the negative of their true values
    • setXRange

      public void setXRange(double min, double max)
      Set the X (horizontal) range of the plot. If this is not done explicitly, then the range is computed automatically from data available when the plot is drawn. If min and max are identical, then the range is arbitrarily spread by 1.
      Parameters:
      min - The left extent of the range.
      max - The right extent of the range.
    • setYLabel

      public void setYLabel(String label)
      Set the label for the Y (vertical) axis.
      Parameters:
      label - The label.
    • setYLog

      public void setYLog(boolean ylog)
      Specify whether the Y axis is drawn with a logarithmic scale. If you would like to have the Y axis drawn with a logarithmic axis, then setYLog(true) should be called before adding any data points.
      Parameters:
      ylog - If true, logarithmic axis is used.
    • setYFlip

      public void setYFlip(boolean yflip)
      Specify whether the Y axis numeric labels should be inverted.
      Parameters:
      yflip - If true, numbers on Y axis will be the negative of their true values
    • setYRange

      public void setYRange(double min, double max)
      Set the Y (vertical) range of the plot. If this is not done explicitly, then the range is computed automatically from data available when the plot is drawn. If min and max are identical, then the range is arbitrarily spread by 0.1.
      Parameters:
      min - The bottom extent of the range.
      max - The top extent of the range.
    • zoom

      public void zoom(double lowx, double lowy, double highx, double highy)
      Zoom in or out to the specified rectangle. This method calls repaint().
      Parameters:
      lowx - The low end of the new X range.
      lowy - The low end of the new Y range.
      highx - The high end of the new X range.
      highy - The high end of the new Y range.
    • _drawPlot

      protected void _drawPlot(Graphics graphics, boolean clearfirst)
      Draw the axes using the current range, label, and title information. If the second argument is true, clear the display before redrawing. This method is called by paintComponent(). To cause it to be called you would normally call repaint(), which eventually causes paintComponent() to be called.

      Note that this is synchronized so that points are not added by other threads while the drawing is occurring. This method should be called only from the event dispatch thread, consistent with swing policy.

      Parameters:
      graphics - The graphics context.
      clearfirst - If true, clear the plot before proceeding.
    • _drawPlot

      protected void _drawPlot(Graphics graphics, boolean clearfirst, Rectangle drawRect)
      Draw the axes using the current range, label, and title information, at the size of the specified rectangle. If the second argument is true, clear the display before redrawing. This method is called by paintComponent(). To cause it to be called you would normally call repaint(), which eventually causes paintComponent() to be called.

      Note that this is synchronized so that points are not added by other threads while the drawing is occurring. This method should be called only from the event dispatch thread, consistent with swing policy.

      Parameters:
      graphics - The graphics context.
      clearfirst - If true, clear the plot before proceeding.
      drawRect - A specification of the size.
    • _drawPoint

      protected void _drawPoint(Graphics graphics, int dataset, long xpos, long ypos, boolean clip)
      Put a mark corresponding to the specified dataset at the specified x and y position. The mark is drawn in the current color. In this base class, a point is a filled rectangle 6 pixels across. Note that marks greater than about 6 pixels in size will not look very good since they will overlap axis labels and may not fit well in the legend. The clip argument, if true, states that the point should not be drawn if it is out of range. Note that this method is not synchronized, so the caller should be. Moreover this method should always be called from the event thread when being used to write to the screen.
      Parameters:
      graphics - The graphics context.
      dataset - The index of the data set.
      xpos - The X position.
      ypos - The Y position.
      clip - If true, do not draw if out of range.
    • _help

      protected void _help()
      Display basic information in its own window.
    • _setButtonsVisibility

      protected void _setButtonsVisibility(boolean vis)
      Deprecated.
      Set the visibility of the Fill button. This is deprecated. Use setButtons().
    • _setPadding

      protected void _setPadding(double padding)
      Set the padding multiple. The plot rectangle can be "padded" in each direction -x, +x, -y, and +y. If the padding is set to 0.05 (and the padding is used), then there is 10% more length on each axis than set by the setXRange() and setYRange() methods, 5% in each direction.
      Parameters:
      padding - The padding multiple.
    • _setYRange

      public void _setYRange(double min, double max)