Class MovablePortrayal2D
- All Implemented Interfaces:
Serializable
,Portrayal
,Portrayal2D
This portrayal is used simply by wrapping around another portrayal or around null (if the object portrays itself).
If you declare a MovablePortrayal2D for an object, you will be able to move it in any field portrayal which supports the basic functions for moving objects. These at present are the various Sparse portrayals and the Continuous2D portrayal.
Moving an object also selects it and deselects other objects.
It's possible that an object wants to control how it's moved. For example, you may have objects which maintain their own internal location and don't like to be moved without being informed. You can still use them with MovablePortrayal2D if they implement the Fixed2D or Constrained interfaces, implementing its sole method to move themselves in their field, and then returning false. Similarly objects can implement this interface to simply deny moving at all, for example at certain points of time in the simulation.
IMPORTANT NOTE: If using AdjustablePortrayal2D in conjunction with MovablePortrayal2D, always wrap the MovablePortrayal2D inside the AdjustablePortrayal2D, not the other way around.
- See Also:
-
Field Summary
Fields inherited from class sim.portrayal.SimplePortrayal2D
TYPE_HIT_OBJECT, TYPE_SELECTED_OBJECT
-
Constructor Summary
-
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.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 MovablePortrayal2D selects the object when it is being moved.boolean
handleMouseEvent
(GUIState guistate, Manipulating2D manipulating, LocationWrapper wrapper, MouseEvent event, DrawInfo2D range, 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
setSelected
(LocationWrapper wrapper, boolean selected) Change the portrayal state to reflect the fact that you've been selected or not selected.void
setSelectsWhenMoved
(boolean val) Sets whether the MovablePortrayal2D selects the object when it is being moved.Methods inherited from class sim.portrayal.SimplePortrayal2D
getStatus
-
Field Details
-
child
-
-
Constructor Details
-
MovablePortrayal2D
-
-
Method Details
-
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
-
getSelectsWhenMoved
public boolean getSelectsWhenMoved()Returns whether the MovablePortrayal2D selects the object when it is being moved. -
setSelectsWhenMoved
public void setSelectsWhenMoved(boolean val) Sets whether the MovablePortrayal2D selects the object when it is being moved. -
handleMouseEvent
public boolean handleMouseEvent(GUIState guistate, Manipulating2D manipulating, LocationWrapper wrapper, MouseEvent event, DrawInfo2D range, 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
-
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
-
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
-