Class VectorPortrayal2D

All Implemented Interfaces:
Serializable, Portrayal, Portrayal2D

public class VectorPortrayal2D extends OrientedPortrayal2D
VectorPortrayal2D draws vectors in grids. It is designed to work with either DoubleGrid2D or ObjectGrid2D, though it will work with DenseGrid2D and SparseGrid2D as well.

VectorPortrayal2D works in one of two modes:

  • Number mode. Here, VectorPortrayal2D takes up to three DoubleGrid2D fields. One field specifies the orientation (in radians) of the vector at a given location, one field specifies its scale (size), and one field specifies its color. Ideally, the three fields would be the same dimension. You can omit one or two of these fields (setting them to null) but not all three. If you chose to include the color field, you must also include a ColorMap which maps the field's value to a color.

    You specify these grids when constructing the VectorPortrayal2D. Additionally of course you will assign the VectorPortrayal as a portrayal for some ValueGridPortrayal2D attached to a grid of some sort. This grid will be ignored as far as drawing goes, but will be the grid used for all other purposes (such as hit-testing) and thus will be the grid that will determine the inspector that pops up when you double-click on a location. You could naturally use one of your three grids (perhaps the orientation grid) as the attached grid as well.

  • Object mode. Here, no DoubleGrid2D fields are provided at all, nor a ColorMap. In this case, VectorPortrayal2D is intended to be registered with an ObjectGridPortrayal2D (or in theory a DenseGridPortrayal2D or a SparseGridPortrayal2D). The VectorPortrayal2D will simply portray object from their underlying grids which is given to it. This is done by querying the object to see if it implements the Oriented2D interface (to get the vector orientation), the Scaled2D interface (to get the vector scale), and the Valuable interface (to get the color). Instead of the Valuable interface, an object may also subclass from Number. If the color is to be queried, a ColorMap must also be provided, else it may be null.

    Object mode also supports Double2D, Double3D, Int2D, and Int3D objects. Here, the x and y values define the vector proper and are converted into appropriate orientation and scale values (which isn't fast, as it requires atan2!). For Double3D and Int3D, the z value is converted into the color.

You can set the scale of the VectorPortrayal2D manually, in which case this value is multiplied by the value provided by the underlying object to get the final scale of the vector. You can also specify a vector shape, one of the OrientedPortrayal2D shapes (since VectorPortrayal2D is its subclass).

VectorPortrayal2D is a subclass of OrientedPortrayal2D mostly because it's convenient for it to use OrientedPortrayal's shapes and auto-rotation. OrentedPortrayal2D has other random features which don't make a whole lot of sense to use in this context, so we suggest you not play with them unless you absolutely must. This inludes mucking with the "offset" instance variable, calling setOrientationShowing(), or changing the "child" instance variable.

See Also:
  • Field Details

  • Constructor Details

    • VectorPortrayal2D

      public VectorPortrayal2D()
      Creates a VectorPortrayal2D which responds to objects which are Oriented2D and/or Scaled2D. These define the orientation and scaling of the vector respectively. The vector color is white, and its shape is a line arrow.
    • VectorPortrayal2D

      public VectorPortrayal2D(int shape)
      Creates a VectorPortrayal2D which responds to objects which are Oriented2D and/or Scaled2D. These define the orientation and scaling of the vector respectively. The vector color is white, and is of the shape specified.
    • VectorPortrayal2D

      public VectorPortrayal2D(ColorMap map)
      Creates a VectorPortrayal2D which responds to objects which are Valuable (or a Number), Oriented2D, and/or Scaled2D. These define the color, orientation and scaling of the vector respectively. The vector color is defined by the map provided, mapping the Valuable interface (or Number.doubleValue()). The vector shape is a line arrow. If the map is null, this constructor operates like new VectorPortrayal2D().
    • VectorPortrayal2D

      public VectorPortrayal2D(int shape, ColorMap map)
      Creates a VectorPortrayal2D which responds to objects which are Valuable (or a Number), Oriented2D, and/or Scaled2D. These define the color, orientation and scaling of the vector respectively. The vector color is defined by the map provided, mapping the Valuable interface (or Number.doubleValue()). The vector shape the shape provided. If the map is null, this constructor operates like new VectorPortrayal2D(shape).
    • VectorPortrayal2D

      public VectorPortrayal2D(DoubleGrid2D orientationGrid)
      Creates a VectorPortrayal with an orientation grid. If this grid is null, this constructor operates the same as new VectorPortrayal2D(). The shape will be a white line arrow.
    • VectorPortrayal2D

      public VectorPortrayal2D(DoubleGrid2D orientationGrid, DoubleGrid2D sizeGrid)
      Creates a VectorPortrayal with an orientation grid and size grid. Either of these grids can be null, and so won't be used. If both grids are null, this constructor operates the same as new VectorPortrayal2D(). The shape will be a white line arrow.
    • VectorPortrayal2D

      public VectorPortrayal2D(int shape, DoubleGrid2D orientationGrid, DoubleGrid2D sizeGrid, DoubleGrid2D colorGrid, ColorMap map)
      Create a VectorPortrayal with a provided shape (see OrientedPortrayal2D), and with an orientation, size, and color grid, plus a ColorMap for the color grid. Any of these grids can be null, and so won't be used. However, if the color grid is null, the ColorMap must be also null (and vice versa). If all three grids are null, this constructor operates the same as new VectorPortrayal(shape).
  • Method Details

    • getOrientation

      public double getOrientation(Object object, DrawInfo2D info)
      Description copied from class: OrientedPortrayal2D
      Returns the orientation of the underlying object, or NaN if there is no such orientation. The default implementation assumes that the object is non-null and is an instance of Oriented2D, and calls orientation2D() on it; else it returns NaN.
      Overrides:
      getOrientation in class OrientedPortrayal2D
    • filterScale

      public double filterScale(double objectScale)
      Override this method to change the scale of a value to a new one. The default version of this method multiples the value by the predefined object scale (this.scale).
    • setShape

      public void setShape(int val)
      Overrides:
      setShape in class OrientedPortrayal2D
    • getUsesExactOrientation

      public boolean getUsesExactOrientation()
    • setUsesExactOrientation

      public void setUsesExactOrientation(boolean val)
    • draw

      public void draw(Object object, Graphics2D graphics, DrawInfo2D info)
      Description copied from interface: Portrayal2D
      Draw a the given object with an origin at (info.draw.x, info.draw.y), and with the coordinate system scaled by so that 1 unit is in the x and y directions are equal to info.draw.width and info.draw.height respectively in pixels. The rectangle given by info.clip specifies the only region in which it is necessary to draw. If info.precise is true, try to draw using real-valued high-resolution drawing rather than faster integer drawing. It is possible that object is null. The location of the object in the field may (and may not) be stored in info.location. The form of that location varies depending on the kind of field used.
      Specified by:
      draw in interface Portrayal2D
      Overrides:
      draw in class OrientedPortrayal2D
    • hitObject

      public boolean hitObject(Object object, DrawInfo2D range)
      Returns true if any part of the 1x1 square surrounding the VectorPortrayal is hit (as opposed to the shape itself.
      Overrides:
      hitObject in class OrientedPortrayal2D
    • getInspector

      public Inspector getInspector(LocationWrapper wrapper, GUIState state)
      Description copied from interface: Portrayal
      Provide an inspector for an object.
      Specified by:
      getInspector in interface Portrayal
      Overrides:
      getInspector in class OrientedPortrayal2D