Class Avatar

java.lang.Object
com.codename1.ui.Component
com.codename1.components.Avatar
All Implemented Interfaces:
Animation, Editable, StyleListener

public class Avatar extends Component

A round picture of a person, or their initials on a coloured circle when there is no picture, with an optional presence dot.

Avatar a = new Avatar("Ada Lovelace", null);   // "AL" on a colour from the name
a.setImage(Image.createImage(200, 120, 0xff8899aa)); // a photo, cropped to a circle
a.setStatusColor(0x2e7d32);                      // a green "online" dot

The colour of the initials circle is derived from the name, so a person keeps the same colour everywhere. The size is setSize millimetres (8 by default). The UIID is Avatar; its font, when themed, is used for the initials.

  • Constructor Details

    • Avatar

      public Avatar()
      An avatar with neither a name nor a picture.
    • Avatar

      public Avatar(String name, Image image)

      An avatar.

      Parameters
      • name: the person's name, for the initials and accessibility text

      • image: the picture, or null for initials

  • Method Details

    • initials

      public static String initials(String name)

      The initials of a name: the first letters of its first and last words.

      Parameters
      • name: the name
      Returns

      one or two upper-case letters, or an empty string

    • setPersonName

      public void setPersonName(String name)

      Sets the person's name.

      Parameters
      • name: the person's name
    • getPersonName

      public String getPersonName()

      The person's name.

      Returns

      the name, or null

    • setInitials

      public void setInitials(String text)

      Replaces the initials with other short text, such as a count.

      Parameters
      • text: up to three characters, or null for the initials of the name
    • setImage

      public void setImage(Image image)

      Sets the picture.

      Parameters
      • image: the picture, or null for initials
    • getImage

      public Image getImage()

      The picture.

      Returns

      the picture, or null

    • setSize

      public void setSize(float millimeters)

      Sets the diameter.

      Parameters
      • millimeters: the size in millimetres
    • setStatusColor

      public void setStatusColor(int rgb)

      Shows a presence dot at the bottom right.

      Parameters
      • rgb: the dot colour, or -1 for none
    • setRingColor

      public void setRingColor(int rgb)

      Draws a ring around the avatar, as AvatarGroup does to separate overlapping ones.

      Parameters
      • rgb: the ring colour, or -1 for none
    • colorFor

      public static int colorFor(String name)

      The colour a name's initials are drawn on.

      Parameters
      • name: the name
      Returns

      an RGB colour, the same for the same name

    • calcPreferredSize

      protected Dimension calcPreferredSize()
      Description copied from class: Component

      Calculates the preferred size based on component content. This method is invoked lazily by getPreferred size.

      Returns

      the calculated preferred size based on component content

      Overrides:
      calcPreferredSize in class Component
    • paint

      public void paint(Graphics g)
      Description copied from class: Component

      This method paints the Component on the screen, it should be overriden by subclasses to perform custom drawing or invoke the UI API's to let the PLAF perform the rendering.

      Parameters
      • g: the component graphics
      Specified by:
      paint in interface Animation
      Overrides:
      paint in class Component
    • getAccessibilityText

      public String getAccessibilityText()
      Description copied from class: Component

      Returns the text that describes this component to assistive technologies. If no text was set explicitly, this method attempts to derive a description from the component's label or content.

      Returns

      accessibility description or null if none

      Overrides:
      getAccessibilityText in class Component