Class LabelledPortrayal2D
- All Implemented Interfaces:
Serializable
,Portrayal
,Portrayal2D
You provide a string label at construction time. The LabelledPortrayal2D will retrieve the label by calling getLabel(Object obj, DrawInfo2D info) at draw time to get the expected label. By default the getLabel function will return the label you had provided at construction; if your provided label was null, then getLabel will by default return the string name of the object. You are free to override this function to provide more specialized label information.
There are certain guidelines you can specify for when the label is to be drawn. At construction time you can state that the label should only be drawn when the object is selected. Additionally if you call the setLabelShowing(...) function, you can turn off or on label drawing entirely for this LabelledPortrayal2D.
You may specify a color or paint for the label (the default is blue) and an alignment (the default is align left).
The label is drawn at:
x: (int)(info.draw.x + scalex * info.draw.width + offsetx); y: (int)(info.draw.y + scaley * info.draw.height + offsety);
... that is, scalex and scaley are values which scale when you zoom in, and offsetx and offsety are values which add additional fixed pixels. The default is scalex = 0, scaley = 0.5, offsetx = 0, offsety = 10. This draws the label ten pixels below the outer rectangular edge of the bounds rect for the portrayal.
The label can be set to scale when zoomed in or out (by default it does not scale).
Note: One oddity of LabelledPortrayal2D is due to the fact that the label is only drawn if the object is being drawn. While most FieldPortrayals ask objects just off-screen to draw themselves just to be careful, if an object is significantly off-screen, it may not be asked to draw itself, and so the label will not be drawn -- even though part of the label could be on-screen at the time! C'est la vie.
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionint
One of ALIGN_CENTER, ALIGN_LEFT, or ALIGN_RIGHTstatic final int
static final int
static final int
static final int
static final double
static final double
static final double
static final double
The font of the text.static final int
double
The post-scaling offset from the object's origin.double
The post-scaling offset from the object's origin.boolean
The Paint or Color of the textstatic final int
double
The pre-scaling offset from the object's origin.double
The pre-scaling offset from the object's origin.Fields inherited from class sim.portrayal.SimplePortrayal2D
TYPE_HIT_OBJECT, TYPE_SELECTED_OBJECT
-
Constructor Summary
ConstructorDescriptionLabelledPortrayal2D
(SimplePortrayal2D child, double offsetx, double offsety, double scalex, double scaley, Font font, int align, String label, Paint paint, boolean onlyLabelWhenSelected) Draws [x=offsetx, y=offsety] pixels away from the [dx=scalex, dy=scaley] prescaled position of the Portrayal2D, using the SansSerif 10pt font, blue, and left alignment.LabelledPortrayal2D
(SimplePortrayal2D child, double scaley, String label, Paint paint, boolean onlyLabelWhenSelected) Draws 10 pixels down from the [dx=0, dy=scaley] prescaled position of the Portrayal2D, using the SansSerif 10pt font, blue, and left alignment.LabelledPortrayal2D
(SimplePortrayal2D child, String label) Draws 10 pixels down from the [dx=0, dy=0.5] prescaled position of the Portrayal2D, using the SansSerif 10pt font, blue, and left alignment.LabelledPortrayal2D
(SimplePortrayal2D child, String label, Paint paint, boolean onlyLabelWhenSelected) Draws 10 pixels down from the [dx=0, dy=0.5] prescaled position of the Portrayal2D, using the SansSerif 10pt font, and left alignment. -
Method Summary
Modifier and TypeMethodDescriptionvoid
draw
(Object object, Graphics2D graphics, DrawInfo2D info) 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.getInspector
(LocationWrapper wrapper, GUIState state) Provide an inspector for an object.getLabel
(Object object, DrawInfo2D info) Returns a name appropriate for the object.int
Returns the label scaling, one of NEVER_SCALE (the default), SCALE_WHEN_SMALLER, and ALWAYS_SCALE.getName
(LocationWrapper wrapper) Returns a static, one-line name for the given object that is useful for a human to distinguish it from other objects.boolean
Returns whether the label should only be shown when the object is selectedboolean
handleMouseEvent
(GUIState guistate, Manipulating2D manipulating, LocationWrapper wrapper, MouseEvent event, DrawInfo2D fieldPortrayalDrawInfo, int type) Optionally handles a mouse event.boolean
hitObject
(Object object, DrawInfo2D range) Return true if the given object, when drawn, intersects with a provided rectangle, for hit testing purposes.boolean
Sets whether the label should be shown regardless.void
setLabelScaling
(int val) Sets the label scaling, one of NEVER_SCALE (the default), SCALE_WHEN_SMALLER, and ALWAYS_SCALE.void
setLabelShowing
(boolean val) Returns whether the label should be shown regardless.void
setOnlyLabelWhenSelected
(boolean val) Sets whether the label should only be shown when the object is selectedboolean
setSelected
(LocationWrapper wrapper, boolean selected) Change the portrayal state to reflect the fact that you've been selected or not selected.Methods inherited from class sim.portrayal.SimplePortrayal2D
getStatus
-
Field Details
-
DEFAULT_SCALE_X
public static final double DEFAULT_SCALE_X- See Also:
-
DEFAULT_SCALE_Y
public static final double DEFAULT_SCALE_Y- See Also:
-
DEFAULT_OFFSET_X
public static final double DEFAULT_OFFSET_X- See Also:
-
DEFAULT_OFFSET_Y
public static final double DEFAULT_OFFSET_Y- See Also:
-
ALIGN_CENTER
public static final int ALIGN_CENTER- See Also:
-
ALIGN_LEFT
public static final int ALIGN_LEFT- See Also:
-
ALIGN_RIGHT
public static final int ALIGN_RIGHT- See Also:
-
scalex
public double scalexThe pre-scaling offset from the object's origin. -
scaley
public double scaleyThe pre-scaling offset from the object's origin. -
offsetx
public double offsetxThe post-scaling offset from the object's origin. -
offsety
public double offsetyThe post-scaling offset from the object's origin. -
align
public int alignOne of ALIGN_CENTER, ALIGN_LEFT, or ALIGN_RIGHT -
font
The font of the text. -
paint
The Paint or Color of the text -
label
-
child
-
onlyLabelWhenSelected
public boolean onlyLabelWhenSelected -
scaledFont
-
NEVER_SCALE
public static final int NEVER_SCALE- See Also:
-
SCALE_WHEN_SMALLER
public static final int SCALE_WHEN_SMALLER- See Also:
-
ALWAYS_SCALE
public static final int ALWAYS_SCALE- See Also:
-
-
Constructor Details
-
LabelledPortrayal2D
public LabelledPortrayal2D(SimplePortrayal2D child, double offsetx, double offsety, double scalex, double scaley, Font font, int align, String label, Paint paint, boolean onlyLabelWhenSelected) Draws [x=offsetx, y=offsety] pixels away from the [dx=scalex, dy=scaley] prescaled position of the Portrayal2D, using the SansSerif 10pt font, blue, and left alignment. If label is null, then object.toString() is used. Labelling occurs if onlyLabelWhenSelected is true. If child is null, then the underlying model object is presumed to be a Portrayal2D and will be used. -
LabelledPortrayal2D
Draws 10 pixels down from the [dx=0, dy=0.5] prescaled position of the Portrayal2D, using the SansSerif 10pt font, blue, and left alignment. If label is null, then object.toString() is used. Labelling will always occur. If child is null, then the underlying model object is presumed to be a Portrayal2D and will be used. -
LabelledPortrayal2D
public LabelledPortrayal2D(SimplePortrayal2D child, double scaley, String label, Paint paint, boolean onlyLabelWhenSelected) Draws 10 pixels down from the [dx=0, dy=scaley] prescaled position of the Portrayal2D, using the SansSerif 10pt font, blue, and left alignment. If label is null, then object.toString() is used. Labelling occurs if onlyLabelWhenSelected is true. If child is null, then the underlying model object is presumed to be a Portrayal2D and will be used. -
LabelledPortrayal2D
public LabelledPortrayal2D(SimplePortrayal2D child, String label, Paint paint, boolean onlyLabelWhenSelected) Draws 10 pixels down from the [dx=0, dy=0.5] prescaled position of the Portrayal2D, using the SansSerif 10pt font, and left alignment. If label is null, then object.toString() is used. Labelling occurs if onlyLabelWhenSelected is true. If child is null, then the underlying model object is presumed to be a Portrayal2D and will be used.
-
-
Method Details
-
setOnlyLabelWhenSelected
public void setOnlyLabelWhenSelected(boolean val) Sets whether the label should only be shown when the object is selected -
getOnlyLabelWhenSelected
public boolean getOnlyLabelWhenSelected()Returns whether the label should only be shown when the object is selected -
isLabelShowing
public boolean isLabelShowing()Sets whether the label should be shown regardless. -
setLabelShowing
public void setLabelShowing(boolean val) Returns whether the label should be shown regardless. -
getLabelScaling
public int getLabelScaling()Returns the label scaling, one of NEVER_SCALE (the default), SCALE_WHEN_SMALLER, and ALWAYS_SCALE. -
setLabelScaling
public void setLabelScaling(int val) Sets the label scaling, one of NEVER_SCALE (the default), SCALE_WHEN_SMALLER, and ALWAYS_SCALE. -
getChild
-
draw
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 interfacePortrayal2D
- Overrides:
draw
in classSimplePortrayal2D
-
hitObject
Description copied from class:SimplePortrayal2D
Return true if the given object, when drawn, intersects with a provided rectangle, for hit testing purposes. The object is drawn 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 region to do hit testing in; often this region is actually of 0 width or height, which might represent a single point. 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.- Overrides:
hitObject
in classSimplePortrayal2D
-
setSelected
Description copied from interface:Portrayal
Change the portrayal state to reflect the fact that you've been selected or not selected. Always return true, except if you've received a setSelected(true) and in fact do not wish to be selectable, in which case return false in that sole situation.- Specified by:
setSelected
in interfacePortrayal
- Overrides:
setSelected
in classSimplePortrayal2D
-
getInspector
Description copied from interface:Portrayal
Provide an inspector for an object.- Specified by:
getInspector
in interfacePortrayal
- Overrides:
getInspector
in classSimplePortrayal2D
-
getLabel
Returns a name appropriate for the object. By default, this returns the label, or if label is null, then returns ("" + object). Override this to make a more customized label to display for the object on-screen. -
getName
Description copied from interface:Portrayal
Returns a static, one-line name for the given object that is useful for a human to distinguish it from other objects. A simple default would be just to return "" + object.- Specified by:
getName
in interfacePortrayal
- Overrides:
getName
in classSimplePortrayal2D
-
handleMouseEvent
public boolean handleMouseEvent(GUIState guistate, Manipulating2D manipulating, LocationWrapper wrapper, MouseEvent event, DrawInfo2D fieldPortrayalDrawInfo, int type) Description copied from class:SimplePortrayal2D
Optionally handles a mouse event. At present, events are sent to SimplePortrayal2Ds representing objects which have been either selected or are presently hit by the event coordinates. The wrapper provides the field portrayal, object location, and object. Also provided are the display, event, the DrawInfo2D for the field portrayal, and the type of mouse event situation (either because the object was SELECTED or because it was HIT).To indicate that the event was handled, return true. The default blank implementation of this method simply returns false. Events are first sent to portrayals selected objects, until one of them handles the event. If none handled the event, then events are sent to portrayals of objects hit by the event, until one of *them* handles the event. If still no one has handled the event, then the Display2D will route the event to built-in mechanisms such selecting the object or inspecting it.
If you're modifying or querying the model as a result of this event, be sure to lock on guistate.state.schedule before you do so.
- Overrides:
handleMouseEvent
in classSimplePortrayal2D
-