sim.portrayal.simple
Class LabelledPortrayal2D

java.lang.Object
  extended by sim.portrayal.SimplePortrayal2D
      extended by sim.portrayal.simple.LabelledPortrayal2D
All Implemented Interfaces:
java.io.Serializable, Portrayal, Portrayal2D

public class LabelledPortrayal2D
extends SimplePortrayal2D

A wrapper for other Portrayal2Ds which also draws a textual label. When you create this LabelledPortrayal2D, you will pass in an underlying Portrayal2D which is supposed to draw the actual object; LabelledPortrayal2D will then add on an underlying label. If the object will draw itself (it's its own Portrayal2D), you can signify this by passing in null for the underlying 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 + ox * info.draw.width) + dx;
   y:     (int)(info.draw.y + oy * info.draw.height) + dy;
   

... that is, ox and oy are values which scale when you zoom in, and dx and dy are values which add additional fixed pixels. The default is ox = 0, oy = 0.5, dx = 0, dy = 20. This draws the label twenty 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:
Serialized Form

Field Summary
 int align
          One of ALIGN_CENTER, ALIGN_LEFT, or ALIGN_RIGHT
static int ALIGN_CENTER
           
static int ALIGN_LEFT
           
static int ALIGN_RIGHT
           
static int ALWAYS_SCALE
           
 SimplePortrayal2D child
           
static int DEFAULT_DX
           
static int DEFAULT_DY
           
static double DEFAULT_OX
           
static double DEFAULT_OY
           
 int dx
          The post-scaling offset from the object's origin.
 int dy
          The post-scaling offset from the object's origin.
 java.awt.Font font
          The font of the text.
 java.lang.String label
           
static int NEVER_SCALE
           
 boolean onlyLabelWhenSelected
           
 double ox
          The pre-scaling offset from the object's origin.
 double oy
          The pre-scaling offset from the object's origin.
 java.awt.Paint paint
          The Paint or Color of the text
static int SCALE_WHEN_SMALLER
           
 
Constructor Summary
LabelledPortrayal2D(SimplePortrayal2D child, int dx, int dy, double ox, double oy, java.awt.Font font, int align, java.lang.String label, java.awt.Paint paint, boolean onlyLabelWhenSelected)
          If child is null, then the underlying model object is presumed to be a Portrayal2D and will be used.
LabelledPortrayal2D(SimplePortrayal2D child, java.lang.String label)
          Draws 20 pixels down from the [0,0.5] prescaled position of the Portrayal2D, using the SansSerif 10pt font, blue, and left alignment.
LabelledPortrayal2D(SimplePortrayal2D child, java.lang.String label, java.awt.Paint paint, boolean onlyLabelWhenSelected)
          Draws 20 pixels down from the [0,1] prescaled position of the Portrayal2D, using the SansSerif 10pt font, and left alignment.
 
Method Summary
 void draw(java.lang.Object object, java.awt.Graphics2D graphics, DrawInfo2D info)
          Draw a portrayed object centered at the origin in info, and with the given scaling factors.
 SimplePortrayal2D getChild(java.lang.Object object)
           
 Inspector getInspector(LocationWrapper wrapper, GUIState state)
          Provide an inspector for an object.
 java.lang.String getLabel(java.lang.Object object, DrawInfo2D info)
          Returns a name appropriate for the object.
 int getLabelScaling()
           
 java.lang.String 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 hitObject(java.lang.Object object, DrawInfo2D range)
          If drawing area intersects selected area, return true.
 boolean isLabelShowing()
           
 void setLabelScaling(int val)
           
 void setLabelShowing(boolean val)
           
 boolean 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, move
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_OX

public static final double DEFAULT_OX
See Also:
Constant Field Values

DEFAULT_OY

public static final double DEFAULT_OY
See Also:
Constant Field Values

DEFAULT_DX

public static final int DEFAULT_DX
See Also:
Constant Field Values

DEFAULT_DY

public static final int DEFAULT_DY
See Also:
Constant Field Values

ALIGN_CENTER

public static final int ALIGN_CENTER
See Also:
Constant Field Values

ALIGN_LEFT

public static final int ALIGN_LEFT
See Also:
Constant Field Values

ALIGN_RIGHT

public static final int ALIGN_RIGHT
See Also:
Constant Field Values

ox

public double ox
The pre-scaling offset from the object's origin.


oy

public double oy
The pre-scaling offset from the object's origin.


dx

public int dx
The post-scaling offset from the object's origin.


dy

public int dy
The post-scaling offset from the object's origin.


align

public int align
One of ALIGN_CENTER, ALIGN_LEFT, or ALIGN_RIGHT


font

public java.awt.Font font
The font of the text.


paint

public java.awt.Paint paint
The Paint or Color of the text


label

public java.lang.String label

child

public SimplePortrayal2D child

onlyLabelWhenSelected

public boolean onlyLabelWhenSelected

NEVER_SCALE

public static final int NEVER_SCALE
See Also:
Constant Field Values

SCALE_WHEN_SMALLER

public static final int SCALE_WHEN_SMALLER
See Also:
Constant Field Values

ALWAYS_SCALE

public static final int ALWAYS_SCALE
See Also:
Constant Field Values
Constructor Detail

LabelledPortrayal2D

public LabelledPortrayal2D(SimplePortrayal2D child,
                           int dx,
                           int dy,
                           double ox,
                           double oy,
                           java.awt.Font font,
                           int align,
                           java.lang.String label,
                           java.awt.Paint paint,
                           boolean onlyLabelWhenSelected)
If child is null, then the underlying model object is presumed to be a Portrayal2D and will be used.


LabelledPortrayal2D

public LabelledPortrayal2D(SimplePortrayal2D child,
                           java.lang.String label)
Draws 20 pixels down from the [0,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,
                           java.lang.String label,
                           java.awt.Paint paint,
                           boolean onlyLabelWhenSelected)
Draws 20 pixels down from the [0,1] prescaled position of the Portrayal2D, using the SansSerif 10pt font, and left alignment. If label is null, then object.toString() is used. If child is null, then the underlying model object is presumed to be a Portrayal2D and will be used.

Method Detail

isLabelShowing

public boolean isLabelShowing()

setLabelShowing

public void setLabelShowing(boolean val)

getLabelScaling

public int getLabelScaling()

setLabelScaling

public void setLabelScaling(int val)

getChild

public SimplePortrayal2D getChild(java.lang.Object object)

draw

public void draw(java.lang.Object object,
                 java.awt.Graphics2D graphics,
                 DrawInfo2D info)
Description copied from interface: Portrayal2D
Draw a portrayed object centered at the origin in info, and with the given scaling factors. It is possible that object is null.

Specified by:
draw in interface Portrayal2D
Overrides:
draw in class SimplePortrayal2D

hitObject

public boolean hitObject(java.lang.Object object,
                         DrawInfo2D range)
Description copied from class: SimplePortrayal2D
If drawing area intersects selected area, return true. The default computes the intersection with the (-0.5,-0.5) to (0.5,0.5) rectangle.

Overrides:
hitObject in class SimplePortrayal2D

setSelected

public boolean setSelected(LocationWrapper wrapper,
                           boolean selected)
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 interface Portrayal
Overrides:
setSelected in class SimplePortrayal2D

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 SimplePortrayal2D

getLabel

public java.lang.String getLabel(java.lang.Object object,
                                 DrawInfo2D info)
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

public java.lang.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 SimplePortrayal2D