Class Img<T extends Img<T>>

Direct Known Subclasses:
Image

public class Img<T extends Img<T>> extends StyledElement<T>
HTML <img> element for embedding images.

This class represents an image element that embeds images in web pages. Supports standard HTML image attributes as well as custom attributes for rollover effects (normal, over, and press states). The element is self-closing.

Features:

  • Image embedding with source URL
  • Alternative text for accessibility
  • Width and height specification
  • Cross-origin resource sharing support
  • Image map support (usemap, ismap)
  • Custom rollover states (normal, over, press)

Usage:

 Img image = new Img("/images/logo.png", "Company Logo");
 image.setIntrinsicWidth(200);
 image.setIntrinsicHeight(100);

 Img rollover = new Img();
 rollover.setNormalSource("/img/button.png");
 rollover.setOverSource("/img/button-hover.png");
 rollover.setPressSource("/img/button-pressed.png");
 
Since:
2007
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Image
  • invalid reference
    SmartImage
  • Constructor Details

    • Img

      public Img()
      Constructs a new image element.
    • Img

      public Img(String src)
      Constructs a new image element with the specified source URL.
      Parameters:
      src - The image source URL.
    • Img

      public Img(String src, String altText)
      Constructs a new image element with source URL and alt text.
      Parameters:
      src - The image source URL.
      altText - The alternative text for accessibility.
  • Method Details

    • setAlt

      public final T setAlt(String alt)
      Sets the alt attribute providing alternative text for the image.

      Alternative text is essential for accessibility and is displayed when the image cannot be loaded. Required for accessibility compliance on <img> elements.

      Parameters:
      alt - The alternative text.
      Returns:
      This element for method chaining.
    • setCrossOrigin

      public final T setCrossOrigin(CrossOrigin crossOrigin)
      Sets the cross-origin attribute using the CrossOrigin enum.
      Parameters:
      crossOrigin - The CORS mode.
      Returns:
      This element for method chaining.
    • setCrossOrigin

      public final T setCrossOrigin(String crossOrigin)
      Sets the crossorigin attribute.

      Specifies how the element handles cross-origin requests. The value "anonymous" sends requests without credentials; "use-credentials" includes cookies and authentication.

      Parameters:
      crossOrigin - The cross-origin value (anonymous, use-credentials).
      Returns:
      This element for method chaining.
    • setDecoding

      public final T setDecoding(String decoding)
      Sets the decoding hint for the image.

      Indicates the preferred method for decoding the image. Values include "sync" (decode synchronously), "async" (decode asynchronously), or "auto" (browser decides).

      Parameters:
      decoding - The decoding hint ("sync", "async", or "auto").
      Returns:
      This element for method chaining.
    • setIsMap

      public T setIsMap(boolean isMap)
      Sets whether the image is a server-side image map.

      When true, the click coordinates are sent to the server. The image must be inside an <a> element for this to work.

      Parameters:
      isMap - True to make this a server-side image map.
      Returns:
      This element for method chaining.
    • setLongDesc

      @Deprecated public T setLongDesc(String url)
      Deprecated.
      Use setAlt() with a more descriptive text instead.
      Sets the URL to a detailed description of the image.
      Parameters:
      url - The URL of the image description.
      Returns:
      This element for method chaining.
    • setUseMap

      public T setUseMap(String mapname)
      Sets the usemap attribute associating this image with a client-side image map.

      The value must be a hash reference to a <map> element (e.g., "#mymap").

      Parameters:
      mapname - The hash reference to a map element.
      Returns:
      This element for method chaining.
    • setSrc

      public final T setSrc(String src)
      Sets the image source URL.

      If the URL starts with "/" (absolute path within the application), the servlet context path will be automatically prepended.

      Parameters:
      src - The image source URL.
      Returns:
      This element for method chaining.
    • setNormalSource

      public final T setNormalSource(String src)
      Sets the normal state source for rollover effects.

      Also sets the main src attribute to this value.

      Parameters:
      src - The normal state image source URL.
      Returns:
      this element for method chaining
    • setOverSource

      public final T setOverSource(String src)
      Sets the hover state source for rollover effects.
      Parameters:
      src - The hover state image source URL.
      Returns:
      this element for method chaining
    • setPressSource

      public final T setPressSource(String src)
      Sets the pressed state source for rollover effects.
      Parameters:
      src - The pressed state image source URL.
      Returns:
      this element for method chaining
    • setIntrinsicWidth

      public T setIntrinsicWidth(int pixels)
      Sets the HTML width attribute to specify intrinsic width in pixels.

      This sets the HTML attribute (e.g., <img width="300">), not the CSS width property. To set CSS width, use the inherited StyledElement.setWidth(int) method.

      Parameters:
      pixels - The width in pixels.
      Returns:
      This element for method chaining.
    • setIntrinsicWidth

      public T setIntrinsicWidth(String width)
      Sets the HTML width attribute to specify intrinsic width.

      This sets the HTML attribute, not the CSS width property. To set CSS width, use the inherited StyledElement.setWidth(String) method.

      Parameters:
      width - The width value.
      Returns:
      This element for method chaining.
    • setIntrinsicHeight

      public T setIntrinsicHeight(int pixels)
      Sets the HTML height attribute to specify intrinsic height in pixels.

      This sets the HTML attribute (e.g., <img height="200">), not the CSS height property. To set CSS height, use the inherited StyledElement.setHeight(int) method.

      Parameters:
      pixels - The height in pixels.
      Returns:
      This element for method chaining.
    • setIntrinsicHeight

      public T setIntrinsicHeight(String height)
      Sets the HTML height attribute to specify intrinsic height.

      This sets the HTML attribute, not the CSS height property. To set CSS height, use the inherited StyledElement.setHeight(String) method.

      Parameters:
      height - The height value.
      Returns:
      This element for method chaining.
    • getIntrinsicWidth

      public String getIntrinsicWidth()
      Returns the HTML width attribute value.

      Returns the intrinsic width set via the HTML attribute, not the CSS width.

      Returns:
      The width value, or null if not set.
    • getIntrinsicHeight

      public String getIntrinsicHeight()
      Returns the HTML height attribute value.

      Returns the intrinsic height set via the HTML attribute, not the CSS height.

      Returns:
      The height value, or null if not set.