Class LabelledPortrayal3D

java.lang.Object
sim.portrayal3d.SimplePortrayal3D
sim.portrayal3d.simple.LabelledPortrayal3D
All Implemented Interfaces:
Portrayal, Portrayal3D

public class LabelledPortrayal3D extends SimplePortrayal3D
A wrapper for other Portrayal3Ds which also draws a textual label. When you create this LabelledPortrayal3D, you will pass in an underlying Portrayal3D which is supposed to represent the actual object; LabelledPortrayal3D will then add on an underlying label. If the object will draw itself (it's its own Portrayal3D), you can signify this by passing in null for the underlying Portrayal3D.

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. Beware that every time you change a label, the object has to generate a new label object and insert it into the system. This is expensive.

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 font for the label. The label is drawn at the (0.5,0.5,0.5) corner of the object by default: you can change this offset in the constructor if you like.

The label will always be drawn directly facing the viewer regardless of the rotation of the model.

  • Field Details

    • DEFAULT_LABEL_OFFSET

      public static final javax.media.j3d.Transform3D DEFAULT_LABEL_OFFSET
  • Constructor Details

    • LabelledPortrayal3D

      public LabelledPortrayal3D(SimplePortrayal3D child)
    • LabelledPortrayal3D

      public LabelledPortrayal3D(SimplePortrayal3D child, String label)
    • LabelledPortrayal3D

      public LabelledPortrayal3D(SimplePortrayal3D child, String label, Color color, boolean onlyLabelWhenSelected)
    • LabelledPortrayal3D

      public LabelledPortrayal3D(SimplePortrayal3D child, double offset, Font font, String label, Color color, boolean onlyLabelWhenSelected)
    • LabelledPortrayal3D

      public LabelledPortrayal3D(SimplePortrayal3D child, double offsetx, double offsety, double offsetz, Font font, String label, Color color, boolean onlyLabelWhenSelected)
    • LabelledPortrayal3D

      public LabelledPortrayal3D(SimplePortrayal3D child, javax.media.j3d.Transform3D offset, Font font, String label, Color color, boolean onlyLabelWhenSelected)
  • Method Details

    • getLabelScale

      public double getLabelScale()
    • setLabelScale

      public void setLabelScale(double s)
    • polygonAttributes

      public javax.media.j3d.PolygonAttributes polygonAttributes()
      Description copied from interface: Portrayal3D
      Provides a PolygonAttributes which can be modified to change the underlying model's attributes (culling, vertex versus point versus fill). This is an optional function: you are free to return null
      Specified by:
      polygonAttributes in interface Portrayal3D
      Overrides:
      polygonAttributes in class SimplePortrayal3D
    • getInspector

      public Inspector getInspector(LocationWrapper wrapper, GUIState state)
      Description copied from interface: Portrayal
      Provide an inspector for an object.
      Specified by:
      getInspector in interface Portrayal
      Overrides:
      getInspector in class SimplePortrayal3D
    • getName

      public String getName(LocationWrapper wrapper)
      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 interface Portrayal
      Overrides:
      getName in class SimplePortrayal3D
    • setCurrentDisplay

      public void setCurrentDisplay(Display3D display)
      Sets the current display both here and in the child.
      Specified by:
      setCurrentDisplay in interface Portrayal3D
      Overrides:
      setCurrentDisplay in class SimplePortrayal3D
    • setCurrentFieldPortrayal

      public void setCurrentFieldPortrayal(FieldPortrayal3D p)
      Sets the current field portrayal both here and in the child.
      Overrides:
      setCurrentFieldPortrayal in class SimplePortrayal3D
    • setSelected

      public boolean setSelected(LocationWrapper wrapper, boolean selected)
      Description copied from class: SimplePortrayal3D
      If the object is selected, adds it to a hash table of selected objects for which this SimplePortrayal3D's isSelected() method will return TRUE. If the object is deselected, removes it from the hash table. Always returns TRUE. The hash table doesn't exist until this method is first called.

      There are two implications to this approach. First, it means that after you've selected an object, there's a hash table attached to its portrayal. If you're using the same portrayal for lots of stuff, that's no big deal. But if you've got per-object portrayals and a lot of objects (or if the objects are themselves SimplePortrayal3D subclasses) then that's a fair number of hash tables. This is a minor memory issue but if you don't care about testing for whether you've been selected or not, you could just override this method to always return TRUE (and don't call super.selected(...) ) and the hash table will never be created. Note that isSelected will always return FALSE for your portrayal in this situation.

      Second, though you can test for selection with the isSelected() method, if you want to, say, change the look of your portrayal based on whether or not it's selected, you will need to test isSelected() each time getModel() is called and modify the model accordingly. This could be a bit expensive. We're working on an approach for you to be able to test if the object was RECENTLY selected or deselected so you could only test then, but it's nontrivial to do without using up a lot of memory.

      Specified by:
      setSelected in interface Portrayal
      Overrides:
      setSelected in class SimplePortrayal3D
    • getLabel

      public String getLabel(Object object, javax.media.j3d.TransformGroup j3dModel)
      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. The TransformGroup is the one for the underlying portrayal3d.
    • setOnlyLabelWhenSelected

      public void setOnlyLabelWhenSelected(boolean val)
    • getOnlyLabelWhenSelected

      public boolean getOnlyLabelWhenSelected()
    • isLabelShowing

      public boolean isLabelShowing()
    • setLabelShowing

      public void setLabelShowing(boolean val)
    • getChild

      public SimplePortrayal3D getChild(Object object)
    • getModel

      public javax.media.j3d.TransformGroup getModel(Object obj, javax.media.j3d.TransformGroup j3dModel)
      Description copied from interface: Portrayal3D
      Provides a TransformGroup which defines the node(s) to place in the scenegraph. This is the Portrayal3D equivalent of Portrayal2D's draw(object, graphics, drawinfo) method.

      You should hang your model off of the TransformGroup provided. You should not transform that TransformGroup in any way -- it is used elsewhere. Instead if you wish to transform your model (rotate it etc.) you should add your own additional TransformGroup as necessary.

      The provided TransformGroup can be null; in this case you need to create and return the outer TransformGroup for the object. If the provided TransformGroup is non-null, you should modify it and return the same.

      SimplePortrayals should assume the following contract: at the point that getModel(...) is called, the field portrayal and display will have already been set if it exists, else it will be null.

      Specified by:
      getModel in interface Portrayal3D
      Overrides:
      getModel in class SimplePortrayal3D