Package sim.portrayal

Class FieldPortrayal2D

java.lang.Object
sim.portrayal.FieldPortrayal
sim.portrayal.FieldPortrayal2D
All Implemented Interfaces:
Portrayal, Portrayal2D
Direct Known Subclasses:
ContinuousPortrayal2D, NetworkPortrayal2D, ObjectGridPortrayal2D, SparseGridPortrayal2D, ValueGridPortrayal2D

public abstract class FieldPortrayal2D extends FieldPortrayal implements Portrayal2D
Superclass of all Field Portrayals in 2D. Implements default versions of the necessary methods, which you should feel free to override (especially portray, hit, and draw!).

The default version of getDefaultPortrayal() returns an empty SimplePortrayal: you'll might want to override that as well. These defaults enable you to use FieldPortrayal2D as an "empty field portrayal" to be attached to a Display2D and draw arbitrary things when its draw(...) method is called. In that case, all you need to do is override the draw(...) method and you're set.

The default versions of hitObjects and draw call a protected but empty method called hitOrDraw. It's very common that a field's hitObjects and draw methods will be identical save for a single line which calls hitObjects or draw on an underlying SimplePortrayal. So most fields unify both methods into this single method and unify the line as something like this:


    if (graphics == null)
    {
    if (portrayal.hitObject(portrayedObject, newinfo))
    putInHere.add(getWrapper(portrayedObject, portrayedObjectLocation));
    }
    else
    portrayal.draw(portrayedObject, graphics, newinfo);
    

...where getWrapper(...) returns a LocationWrapper appropriate to the Field.

  • Field Details

    • DEFAULT

      public static final int DEFAULT
      Default buffering: let the program decide on its own (typically in a platform-dependent fashion)
      See Also:
    • USE_BUFFER

      public static final int USE_BUFFER
      Use a buffer
      See Also:
    • DONT_USE_BUFFER

      public static final int DONT_USE_BUFFER
      Don't use a buffer
      See Also:
  • Constructor Details

    • FieldPortrayal2D

      public FieldPortrayal2D()
  • Method Details

    • getRelativeObjectPosition

      public Point2D.Double getRelativeObjectPosition(Object location, Object otherObjectLocation, DrawInfo2D otherObjectInfo)
      Returns the position on-screen of an object at a given location in the field, using another object's location and DrawInfo2D to perform the computation. Returns null if we can't compute it -- this happens by default if the two locations are neither Int2D nor Double2D. This method is largely used by TrailPortrayal2D.
    • draw

      public void draw(Object object, Graphics2D graphics, DrawInfo2D info)
      Draws the field with its origin at [info.draw.x,info.draw.y], relative to the scaled coordinate system defined by [info.draw.width,info.draw.height]. The only parts that need be drawn are those which fall within the [info.clip] rectangle. Since your draw and hitObjects methods are likely to be nearly identical, you can choose instead to override the hitOrDraw method to handle both of them. By default this method simply calls hitOrDraw. FieldPortrayals will receive null for the object; they should just draw their own fields.
      Specified by:
      draw in interface Portrayal2D
    • hitObjects

      public void hitObjects(DrawInfo2D range, Bag putInHere)
      Adds to the provided Bag LocationWrappers for any objects which overlap the provided hit range. The hit range will usually define a single point, but COULD be a range. The object should perceive itself as located at the (range.draw.x,range.draw.y) origin and drawn relative to the (range.draw.width,range.draw.height) scale.
    • hitOrDraw

      protected void hitOrDraw(Graphics2D graphics, DrawInfo2D info, Bag putInHere)
      Instead of overriding the draw and hitObjects methods, you can optionally override this method to provide both the draw(...) and hitObjects(...) functionality in a single method, as it's common that these two methods have nearly identical code. You should test which operation to do based on whether or not graphics is null (if it is, you're hitting, else you're drawing).
    • getDefaultPortrayal

      public Portrayal getDefaultPortrayal()
      Description copied from class: FieldPortrayal
      Should return a portrayal which can portray any object regardless of whether it's valid or not
      Specified by:
      getDefaultPortrayal in class FieldPortrayal
    • setObjectPosition

      public void setObjectPosition(Object object, Point2D.Double position, DrawInfo2D fieldPortrayalInfo)
      Moves (or tries to move) the object to an internal location equivalent to the given position on-screen of the provided object, assuming that the object exists within the underlying field and that this location is acceptable. Optionally overridable. The default implementation calls setObjectLocation to do its work.
    • getScale

      public Double2D getScale(DrawInfo2D fieldPortrayalInfo)
      Returns the width and height, in pixels, of 1.0 x 1.0 units in the underlying field. Optionally overridable. The default version thows an error if called.
    • getPositionLocation

      public Object getPositionLocation(Point2D.Double position, DrawInfo2D fieldPortrayalInfo)
      Returns the Location, in the parlance of the underlying Field, of the given position. If there is no such Location, then null is returned. Optionally overridable. By default null is returned.
    • getLocationPosition

      public Point2D.Double getLocationPosition(Object location, DrawInfo2D fieldPortrayalInfo)
      Returns the position on-screen of the provided location in the underlying field. Negative positions are acceptable. If null is returned, then the portrayal is unable to perform the requested action on the given location. Optionally overridable. The default implementation returns null.
    • getClipLocation

      public Object getClipLocation(DrawInfo2D info)
      Deprecated.
      use getPositionLocation
      Returns an object representing the location in the field of the origin of the clip of the DrawInfo2D. This method calls getPositionLocation, which may or may not be implemented by the FieldPortrayal2D.
    • getObjectPosition

      public Point2D.Double getObjectPosition(Object object, DrawInfo2D fieldPortrayalInfo)
      Returns the position-onscreen of the provided object, assuming that the object exists within the underlying field. Negative locations are acceptable. If null is returned, then the portrayal is unable to perform the requested action on the given object.
    • getBuffering

      public int getBuffering()
      Returns whether or not the FieldPortrayal2D will use a buffering "trick" to draw quickly. This optional property is in FieldPortrayal2D but is only taken advantage of by one or two subclasses. Some FieldPortrayal2Ds primarily draw lots of rectangles in a grid. Certain ones can draw this in one of two ways: either by drawing each rectangle separately, or by filling a buffer (an image) with individual points, then stretching the buffer over the area, causing the points to enlarge into rectangles. The second is often faster, especially if given lots of memory (and always faster on Macs regardless). The FieldPortrayal2D's behavior in this regard will depend on the value of the buffering property. The property can take on one of three values: DEFAULT (let the machine decide on its own in a platform-dependent fashion -- the default), USE_BUFFER, or DONT_USE_BUFFER.
    • setBuffering

      public void setBuffering(int val)
      Sets whether or not the FieldPortrayal2D will use a buffering "trick" to draw quickly. This optional property is in FieldPortrayal2D but is only taken advantage of by one or two subclasses. Some FieldPortrayal2Ds primarily draw lots of rectangles in a grid. Certain ones can draw this in one of two ways: either by drawing each rectangle separately, or by filling a buffer (an image) with individual points, then stretching the buffer over the area, causing the points to enlarge into rectangles. The second is often faster, especially if given lots of memory (and always faster on Macs regardless). The FieldPortrayal2D's behavior in this regard will depend on the value of the buffering property. The property can take on one of three values: DEFAULT (let the machine decide on its own in a platform-dependent fashion -- the default), USE_BUFFER, or DONT_USE_BUFFER.