Class ValueGridPortrayal2D
- All Implemented Interfaces:
Portrayal
,Portrayal2D
- Direct Known Subclasses:
FastValueGridPortrayal2D
,HexaValueGridPortrayal2D
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 ValueGridPortrayal2Ds 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. The 'location' passed into the DrawInfo2D handed to the SimplePortryal2D is a MutableInt2D.
-
Field Summary
Fields inherited from class sim.portrayal.FieldPortrayal2D
DEFAULT, DONT_USE_BUFFER, USE_BUFFER
Fields inherited from class sim.portrayal.FieldPortrayal
classPortrayals, field, immutableField, portrayalForAll, portrayalForNonNull, portrayalForNull, portrayalForRemainder, portrayals
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionShould return a portrayal which can portray any object regardless of whether it's valid or notgetLocationPosition
(Object location, DrawInfo2D info) Returns the position on-screen of the provided location in the underlying field.getMap()
getPositionLocation
(Point2D.Double position, DrawInfo2D info) Returns the Location, in the parlance of the underlying Field, of the given position.getScale
(DrawInfo2D info) Returns the width and height, in pixels, of 1.0 x 1.0 units in the underlying field.getWrapper
(double val, Int2D loc) 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.double
newValue
(int x, int y, double value) This method is called by the default inspector to filter new values set by the user.void
setBorder
(boolean on) Turns border lines on or off.void
setBorderColor
(Color val) Sets the border color.void
setBorderLineFraction
(double val) Sets the border line fraction.void
setBorderLineMinMaxWidth
(double min, double max) Sets the minimum and maximum width of a border line in pixels.void
Sets the field, and sets the dirtyField flag to true.void
setGridColor
(Color val) Sets the grid color.void
setGridLineFraction
(double val) Sets the grid line fraction.void
setGridLineMinMaxWidth
(double min, double max) Sets the minimum and maximum width of a grid line in pixels.void
setGridLines
(boolean on) Turns grid lines on or off.void
setGridMinSpacing
(double val) Sets the grid min spacing.void
setGridModulus
(int val) Sets the grid modulus.void
void
setValueName
(String name) Methods inherited from class sim.portrayal.FieldPortrayal2D
draw, getBuffering, getClipLocation, getObjectPosition, getRelativeObjectPosition, hitObjects, setBuffering, setObjectPosition
Methods inherited from class sim.portrayal.FieldPortrayal
getAlternatePortrayalOrdering, getDefaultNullPortrayal, getField, getInspector, getName, getObjectLocation, getPortrayalForAll, getPortrayalForNonNull, getPortrayalForNull, getPortrayalForObject, getPortrayalForRemainder, getStatus, isDirtyField, isImmutableField, reset, setAlternatePortrayalOrdering, setDirtyField, setImmutableField, setObjectLocation, setPortrayalForAll, setPortrayalForClass, setPortrayalForNonNull, setPortrayalForNull, setPortrayalForObject, setPortrayalForRemainder, setSelected, 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, getStatus, setSelected
-
Field Details
-
valueToPass
-
locationToPass
-
-
Constructor Details
-
ValueGridPortrayal2D
public ValueGridPortrayal2D() -
ValueGridPortrayal2D
-
-
Method Details
-
getMap
-
setMap
-
setField
Description copied from class:FieldPortrayal
Sets the field, and sets the dirtyField flag to true. May throw an exception if the field is inappropriate. The default version just sets the field and sets the dirtyField flag.- Overrides:
setField
in classFieldPortrayal
-
getValueName
-
setValueName
-
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. -
getScale
Description copied from class:FieldPortrayal2D
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.- Overrides:
getScale
in classFieldPortrayal2D
-
getPositionLocation
Description copied from class:FieldPortrayal2D
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.- Overrides:
getPositionLocation
in classFieldPortrayal2D
-
getLocationPosition
Description copied from class:FieldPortrayal2D
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.- Overrides:
getLocationPosition
in classFieldPortrayal2D
-
getDefaultPortrayal
Description copied from class:FieldPortrayal
Should return a portrayal which can portray any object regardless of whether it's valid or not- Overrides:
getDefaultPortrayal
in classFieldPortrayal2D
-
hitOrDraw
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 classFieldPortrayal2D
-
getWrapper
-
setGridLines
public void setGridLines(boolean on) Turns grid lines on or off. By default the grid is off. -
setGridColor
Sets the grid color. By default the grid is blue. -
setGridModulus
public void setGridModulus(int val) Sets the grid modulus. This is the minimum number of grid cells skipped before another grid line is drawn. By default the modulus is 10. -
setGridMinSpacing
public void setGridMinSpacing(double val) Sets the grid min spacing. This is the minimum number of pixels skipped before another grid line is drawn. The grid modulus is doubled until the grid spacing equals or exceeds the minimum spacing. By default the min spacing is 2.0. -
setGridLineFraction
public void setGridLineFraction(double val) Sets the grid line fraction. This is the width of a stroked line as a fraction of the width (or height) of a grid cell. Grid lines are drawn centered on the borders between cells. By default the fraction is 1/8.0. -
setGridLineMinMaxWidth
public void setGridLineMinMaxWidth(double min, double max) Sets the minimum and maximum width of a grid line in pixels. By default, the minimum is 1.0 and the maximum is positive infinity. -
setBorder
public void setBorder(boolean on) Turns border lines on or off. By default the border is off. -
setBorderColor
Sets the border color. By default the border is red. -
setBorderLineFraction
public void setBorderLineFraction(double val) Sets the border line fraction. This is the width of a stroked line as a fraction of the width (or height) of a grid cell. Grid lines are drawn centered on the borders around the grid. Note that if the grid is being drawn clipped (see Display2D.setClipping(...)), then only HALF of the width of this line will be visible (the half that lies within the grid region). By default the fraction is 1/8.0.. -
setBorderLineMinMaxWidth
public void setBorderLineMinMaxWidth(double min, double max) Sets the minimum and maximum width of a border line in pixels. By default, the minimum is 1.0 and the maximum is positive infinity.
-