Class VectorField

  • All Implemented Interfaces:
    Computable, java.io.Serializable

    public class VectorField
    extends Drawable
    implements Computable
    A VectorField displays lines or arrows on a grid of points where the direction and/or lengths are given by two functions (f1(x,y),f2(x,y)). This is probably more useful as a "direction field" than as a "vector field."
    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int ARROWS
      One of the possible styles for displaying a VectorField: as a direction field shown as arrows of equal length.
      static int CLAMPED_VECTORS
      One of the possible styles for displaying a VectorField: as a vector field where a vector is shown as an arrow from (x,y) to (x+xFunc(x,y),y+xFunc(x,y)), except that a maximum length is imposed.
      static int LINES
      One of the possible styles for displaying a VectorField: as a direction field shown as tangent lines.
      static int SCALED_LINES
      One of the possible styles for displaying a VectorField: as a field of tangent lines where the length of the line is proportional to the length of the vector.
      static int SCALED_VECTORS
      One of the possible styles for displaying a VectorField: as a vector field where a vector is shown as an arrow with length proportional to the length of the vector.
    • Constructor Summary

      Constructors 
      Constructor Description
      VectorField()
      Create a VectorField object with nothing to graph.
      VectorField​(Function xFunc, Function yFunc)
      Create a VectorField that will be displayed using the default style, as a direction field of equal-length arrows.
      VectorField​(Function xFunc, Function yFunc, int style)
      Create a VectorField with the specified functions and style.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void compute()
      Recompute data for the vector field and make sure that the area of the display canvas that shows the vector field is redrawn.
      void draw​(java.awt.Graphics g, boolean coordsChanged)
      Draw the vector field (possibly recomputing the data if the CoordinateRect has changed).
      java.awt.Color getColor()
      Get the color that is used to draw the vector field.
      int getPixelSpacing()
      Get the value of the pixelSpacing property, which determines the grid spacing for the vector field.
      int getStyle()
      Get the style in which the vector field is displayed.
      Function getXFunction()
      Get the (possibly null) function that gives the x-component of the vector field.
      Function getYFunction()
      Get the (possibly null) function that gives the y-component of the vector field.
      void setColor​(java.awt.Color c)
      Set the color to be used for drawing the vector field.
      void setFunctions​(Function dx, Function dy)
      Sets the functions that give the components of the vector field.
      void setPixelSpacing​(int spacing)
      Set the value of the pixelSpacing property, which determines the grid spacing for the vector field.
      void setStyle​(int style)
      Set the style in which the vector field is displayed.
      void setXFunction​(Function dx)
      Set the function that gives the x-component of the vector field.
      void setYFunction​(Function dy)
      Set the function that gives the y-component of the vector field.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • ARROWS

        public static final int ARROWS
        One of the possible styles for displaying a VectorField: as a direction field shown as arrows of equal length. The point where the vector is computed is the tail of the arrow.
        See Also:
        Constant Field Values
      • LINES

        public static final int LINES
        One of the possible styles for displaying a VectorField: as a direction field shown as tangent lines. The point where the vector is computed is the center of the line.
        See Also:
        Constant Field Values
      • CLAMPED_VECTORS

        public static final int CLAMPED_VECTORS
        One of the possible styles for displaying a VectorField: as a vector field where a vector is shown as an arrow from (x,y) to (x+xFunc(x,y),y+xFunc(x,y)), except that a maximum length is imposed.
        See Also:
        Constant Field Values
      • SCALED_LINES

        public static final int SCALED_LINES
        One of the possible styles for displaying a VectorField: as a field of tangent lines where the length of the line is proportional to the length of the vector.
        See Also:
        Constant Field Values
      • SCALED_VECTORS

        public static final int SCALED_VECTORS
        One of the possible styles for displaying a VectorField: as a vector field where a vector is shown as an arrow with length proportional to the length of the vector. The lengths are scaled so that the longest arrow has length equal to the grid spacing.
        See Also:
        Constant Field Values
    • Constructor Detail

      • VectorField

        public VectorField()
        Create a VectorField object with nothing to graph. The functions and other values can be set later. The default display style is as a direction field of equal-length arrows.
      • VectorField

        public VectorField​(Function xFunc,
                           Function yFunc)
        Create a VectorField that will be displayed using the default style, as a direction field of equal-length arrows. If either of the functions is null, nothing will be displayed. If non-null, the functions must be functions of two variables.
      • VectorField

        public VectorField​(Function xFunc,
                           Function yFunc,
                           int style)
        Create a VectorField with the specified functions and style.
        Parameters:
        xFunc - A Function of two variables giving the x-component of the vector field. If this is null, then nothing will be drawn.
        yFunc - A Function of two variables giving the y-component of the vector field. If this is null, then nothing will be drawn.
        style - The style in which the direction field is drawn. This can be one of the constants ARROWS (a direction field of equal-lenth arrows), LINES (equal length lines), CLAMPED_VECTORS (vectors drawn at actual length, unless too long), SCALED_VECTORS (vectors scaled so longest has length equal to the grid spacing), or SCALED_LINES (lines scaled so longest has length equal to the grid spacing).
    • Method Detail

      • setColor

        public void setColor​(java.awt.Color c)
        Set the color to be used for drawing the vector field. The default color is light gray.
      • getColor

        public java.awt.Color getColor()
        Get the color that is used to draw the vector field.
      • setFunctions

        public void setFunctions​(Function dx,
                                 Function dy)
        Sets the functions that give the components of the vector field. If either function is null, then nothing is drawn. If non-null, each function must be a function of two variables.
      • setXFunction

        public void setXFunction​(Function dx)
        Set the function that gives the x-component of the vector field. If this is null, then nothing is drawn. If non-null, it must be a function of two variables.
      • setYFunction

        public void setYFunction​(Function dy)
        Set the function that gives the y-component of the vector field. If this is null, then nothing is drawn. If non-null, it must be a function of two variables.
      • getXFunction

        public Function getXFunction()
        Get the (possibly null) function that gives the x-component of the vector field.
      • getYFunction

        public Function getYFunction()
        Get the (possibly null) function that gives the y-component of the vector field.
      • getStyle

        public int getStyle()
        Get the style in which the vector field is displayed.
      • setStyle

        public void setStyle​(int style)
        Set the style in which the vector field is displayed. This should be one of the constants ARROWS, LINES, CLAMPED_VECTORS, SCALED_LINES, or SCALED_VECTORS.
      • getPixelSpacing

        public int getPixelSpacing()
        Get the value of the pixelSpacing property, which determines the grid spacing for the vector field.
      • setPixelSpacing

        public void setPixelSpacing​(int spacing)
        Set the value of the pixelSpacing property, which determines the grid spacing for the vector field. The value will be clamped to the range from 5 to 200. The default value is 30.
      • compute

        public void compute()
        Recompute data for the vector field and make sure that the area of the display canvas that shows the vector field is redrawn. This method is ordinarily called by a Controller.
        Specified by:
        compute in interface Computable
      • draw

        public void draw​(java.awt.Graphics g,
                         boolean coordsChanged)
        Draw the vector field (possibly recomputing the data if the CoordinateRect has changed).
        Specified by:
        draw in class Drawable
        Parameters:
        g - The graphics context in which the Drawble is to be drawn. (The drawing can change the color in g, but should not permanently change font, painting mode, etc. Thus, every drawable is responsible for setting the color it wants to use.)
        coordsChanged - Indicates whether the CoordinateRect has changed.