sim.portrayal.grid
Class ValueGrid2DPortrayal

java.lang.Object
  extended bysim.portrayal.FieldPortrayal
      extended bysim.portrayal.FieldPortrayal2D
          extended bysim.portrayal.grid.ValueGrid2DPortrayal
All Implemented Interfaces:
Portrayal, Portrayal2D, java.io.Serializable
Direct Known Subclasses:
FastValueGrid2DPortrayal, HexaValueGrid2DPortrayal

public class ValueGrid2DPortrayal
extends FieldPortrayal2D

This class is capable of portraying the DoubleGrid2D and IntGrid2D fields (and only those two fields -- or subclasses). It is fairly customizable, and this flexibility comes at a cost in drawing speed. If you just need to draw your field as a grid of squares, you might look into the much simpler, and faster (and aptly named) FastValueGrid2DPortrayal instead.

Like other FieldPortrayal2Ds, this class uses an underlying SimplePortrayal2D to draw each separate element in the grid. A default SimplePortrayal2D is provided which draws squares. In the default, the color for the square is determined by looking up the value of the square in a user-provided color-table, or if there is none, by interpolating it between two user-provided colors. See the setColorTable() and setLevels() methods.

Here's a trick you might consider in specifying interpolations. Rather than draw from white to red (for example), you might consider setting the backdrop of the display to white, and then instead draw from FULLY TRANSPARENT white to FULLY OPAQUE red. That is, from Color(0,0,0,0) to Color(255,0,0,255). Fully transparent colors are not drawn; and not drawing at all is significantly faster than drawing for no reason! Plus you can stack multiple ValueGrid2DPortrayals on top of one another and let the transparency bleed through for a nice effect. The alpha channel is your friend.

By default the min Level and the max Level are the same (1.0), and the alpha values for both are 0 (totally transparent). Thus if you want a range, you must specify it. This is intentional, because this way if you want to use a color table instead (say, to specify three colors for the integers 0, 1, and 2), you can specify them, and ALL other grid values will be automatically transparent.

If you would like more control over the color of your values (perhaps to implement a nonlinear function of the colors), you can override the getColor() function to define your own custom color.

You can also provide your own custom SimplePortrayal2D (use setPortrayalForAll(...) ) to draw elements as you see fit rather than as rectangles. Your SimplePortrayal2D should expect objects passed to its draw method to be of type MutableDouble. Do not hold onto this array -- it will be reused.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes inherited from class sim.portrayal.FieldPortrayal
FieldPortrayal.CustomInspector
 
Field Summary
 java.awt.Color clearColor
           
static int COLOR_DISCRETIZATION
           
 java.awt.Color[] colors
          User-provided color table
 AbstractGrid2D field
           
 int maxAlpha
           
 int maxBlue
           
 int maxGreen
           
 double maxLevel
           
 int maxRed
           
 int minAlpha
           
 int minBlue
           
 java.awt.Color minColor
           
 int minGreen
           
 double minLevel
           
 int minRed
           
 java.lang.String valueName
           
 
Fields inherited from class sim.portrayal.FieldPortrayal
classPortrayals, portrayalForAll, portrayalForNull, portrayalForRemainder, portrayals
 
Constructor Summary
ValueGrid2DPortrayal()
           
ValueGrid2DPortrayal(java.lang.String valueName)
           
 
Method Summary
 java.awt.Color getColor(double level)
          Override this if you'd like to customize the color for values in the portrayal.
 Portrayal getDefaultPortrayal()
          Should return a portrayal which can portray any object regardless of whether it's valid or not
 java.lang.Object getField()
          Returns the current field.
 java.lang.String getValueName()
           
 LocationWrapper getWrapper(double val, int x, int y)
           
protected  void hitOrDraw(java.awt.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.
 double newValue(int x, int y, double value)
          This method is called by the default inspector to filter new values set by the user.
 java.awt.Color[] setColorTable(java.awt.Color[] colors)
          Specifies that if a value (cast into an int) in the IntGrid2D or DoubleGrid2D falls in the range 0 ...
 void setField(java.lang.Object field)
          Attaches the field portrayal to the given field.
 void setLevels(double minLevel, double maxLevel, java.awt.Color minColor, java.awt.Color maxColor)
          Sets the color levels for the ValueGrid2DPortrayal values for use by the default getColor(...) method.
 
Methods inherited from class sim.portrayal.FieldPortrayal2D
draw, hitObjects
 
Methods inherited from class sim.portrayal.FieldPortrayal
getDefaultNullPortrayal, getInspector, getName, getPortrayalForAll, getPortrayalForNull, getPortrayalForObject, getPortrayalForRemainder, setPortrayalForAll, setPortrayalForClass, setPortrayalForNull, setPortrayalForObject, setPortrayalForRemainder, setSelected
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface sim.portrayal.Portrayal
getInspector, getName, setSelected
 

Field Detail

field

public AbstractGrid2D field

minRed

public int minRed

minBlue

public int minBlue

minGreen

public int minGreen

minAlpha

public int minAlpha

maxRed

public int maxRed

maxBlue

public int maxBlue

maxGreen

public int maxGreen

maxAlpha

public int maxAlpha

maxLevel

public double maxLevel

minLevel

public double minLevel

clearColor

public final java.awt.Color clearColor

minColor

public java.awt.Color minColor

COLOR_DISCRETIZATION

public static final int COLOR_DISCRETIZATION
See Also:
Constant Field Values

colors

public java.awt.Color[] colors
User-provided color table


valueName

public java.lang.String valueName
Constructor Detail

ValueGrid2DPortrayal

public ValueGrid2DPortrayal()

ValueGrid2DPortrayal

public ValueGrid2DPortrayal(java.lang.String valueName)
Method Detail

setField

public void setField(java.lang.Object field)
Description copied from class: FieldPortrayal
Attaches the field portrayal to the given field. Should throw an exception if the object is invalid.

Specified by:
setField in class FieldPortrayal

getField

public java.lang.Object getField()
Description copied from class: FieldPortrayal
Returns the current field.

Specified by:
getField in class FieldPortrayal

getValueName

public java.lang.String getValueName()

newValue

public double newValue(int x,
                       int y,
                       double value)
This method is called by the default inspector to filter new values set by the user. You should return the "corrected" value if the given value is invalid. The default version of this method bases values on the values passed into the setLevels() and setColorTable() methods.


getColor

public java.awt.Color getColor(double level)
Override this if you'd like to customize the color for values in the portrayal. The default version looks up the value in the colors[] table, else computes the interpolated color and grabs it out of a predefined color cache (there can't be more than about 1024 or so interpolated colors, max).


setColorTable

public java.awt.Color[] setColorTable(java.awt.Color[] colors)
Specifies that if a value (cast into an int) in the IntGrid2D or DoubleGrid2D falls in the range 0 ... colors.length, then that index in the colors table should be used to represent that value. Otherwise, values in setLevels(...) are used. You can remove the color table by passing in null here. Returns the old color table.


setLevels

public void setLevels(double minLevel,
                      double maxLevel,
                      java.awt.Color minColor,
                      java.awt.Color maxColor)
Sets the color levels for the ValueGrid2DPortrayal values for use by the default getColor(...) method. These are overridden by any array provided in setColorTable(). If the value in the IntGrid2D or DoubleGrid2D is less than or equal to minLevel, then minColor is used. If the value is greater than or equal to maxColor, then maxColor is used. Otherwise a linear interpolation from minColor to maxColor is used.


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

hitOrDraw

protected void hitOrDraw(java.awt.Graphics2D graphics,
                         DrawInfo2D info,
                         Bag putInHere)
Description copied from class: FieldPortrayal2D
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).

Overrides:
hitOrDraw in class FieldPortrayal2D

getWrapper

public LocationWrapper getWrapper(double val,
                                  int x,
                                  int y)