Class Image

All Implemented Interfaces:
Accessible, Buildable, ConstraintTarget, Proxy

@Generated("org.javagi.JavaGI") public class Image extends Widget implements Accessible, Buildable, ConstraintTarget

Displays an image.

An example GtkImage

Various kinds of object can be displayed as an image; most typically, you would load a GdkTexture from a file, using the convenience function fromFile(String), for instance:

GtkWidget *image = gtk_image_new_from_file ("myfile.png");

If the file isn’t loaded successfully, the image will contain a “broken image” icon similar to that used in many web browsers.

If you want to handle errors in loading the file yourself, for example by displaying an error message, then load the image with an image loading framework such as libglycin, then create the GtkImage with fromPaintable(Paintable).

Sometimes an application will want to avoid depending on external data files, such as image files. See the documentation of GResource inside GIO, for details. In this case, Gtk.Image:resource, fromResource(String), and setFromResource(String) should be used.

GtkImage displays its image as an icon, with a size that is determined by the application. See Picture if you want to show an image at is actual size.

CSS nodes

GtkImage has a single CSS node with the name image. The style classes .normal-icons or .large-icons may appear, depending on the Gtk.Image:icon-size property.

Accessibility

GtkImage uses the Gtk.AccessibleRole.img role.

  • Constructor Details

    • Image

      public Image(MemorySegment address)
      Create a Image instance for the provided memory address.
      Parameters:
      address - the memory address of the native object
    • Image

      public Image()
      Create a new Image.
  • Method Details

    • getType

      public static @Nullable Type getType()
      Get the GType of the Image class.
      Returns:
      the GType
    • getMemoryLayout

      public static MemoryLayout getMemoryLayout()
      The memory layout of the native struct.
      Returns:
      the memory layout
    • asParent

      protected Image asParent()
      Return this instance as if it were its parent type. Comparable to the Java super keyword, but ensures the parent typeclass is also used in native code.
      Overrides:
      asParent in class Widget
      Returns:
      the instance as if it were its parent type
    • fromFile

      public static Image fromFile(String filename)

      Creates a new GtkImage displaying the file filename.

      If the file isn’t found or can’t be loaded, the resulting GtkImage will display a “broken image” icon. This function never returns null, it always returns a valid GtkImage widget.

      If you need to detect failures to load the file, use an image loading framework such as libglycin to load the file yourself, then create the GtkImage from the texture.

      The storage type (see getStorageType()) of the returned image is not defined, it will be whatever is appropriate for displaying the file.

      Parameters:
      filename - a filename
      Returns:
      a new GtkImage
    • fromGicon

      public static Image fromGicon(Icon icon)

      Creates a GtkImage displaying an icon from the current icon theme.

      If the icon name isn’t known, a “broken image” icon will be displayed instead. If the current icon theme is changed, the icon will be updated appropriately.

      Parameters:
      icon - an icon
      Returns:
      a new GtkImage displaying the themed icon
    • fromIconName

      public static Image fromIconName(@Nullable String iconName)

      Creates a GtkImage displaying an icon from the current icon theme.

      If the icon name isn’t known, a “broken image” icon will be displayed instead. If the current icon theme is changed, the icon will be updated appropriately.

      Parameters:
      iconName - an icon name
      Returns:
      a new GtkImage displaying the themed icon
    • fromPaintable

      public static Image fromPaintable(@Nullable Paintable paintable)

      Creates a new GtkImage displaying paintable.

      The GtkImage does not assume a reference to the paintable; you still need to unref it if you own references. GtkImage will add its own reference rather than adopting yours.

      The GtkImage will track changes to the paintable and update its size and contents in response to it.

      Note that paintables are still subject to the icon size that is set on the image. If you want to display a paintable at its intrinsic size, use Picture instead.

      If paintable is a SymbolicPaintable, then it will be recolored with the symbolic palette from the theme.

      Parameters:
      paintable - a GdkPaintable
      Returns:
      a new GtkImage
    • fromPixbuf

      @Deprecated public static Image fromPixbuf(@Nullable Pixbuf pixbuf)
      Deprecated.

      Creates a new GtkImage displaying pixbuf.

      The GtkImage does not assume a reference to the pixbuf; you still need to unref it if you own references. GtkImage will add its own reference rather than adopting yours.

      This is a helper for fromPaintable(Paintable), and you can't get back the exact pixbuf once this is called, only a texture.

      Note that this function just creates an GtkImage from the pixbuf. The GtkImage created will not react to state changes. Should you want that, you should use fromIconName(String).

      Parameters:
      pixbuf - a GdkPixbuf
      Returns:
      a new GtkImage
    • fromResource

      public static Image fromResource(String resourcePath)

      Creates a new GtkImage displaying the resource file resourcePath.

      If the file isn’t found or can’t be loaded, the resulting GtkImage will display a “broken image” icon. This function never returns null, it always returns a valid GtkImage widget.

      If you need to detect failures to load the file, use an image loading framework such as libglycin to load the file yourself, then create the GtkImage from the texture.

      The storage type (see getStorageType()) of the returned image is not defined, it will be whatever is appropriate for displaying the file.

      Parameters:
      resourcePath - a resource path
      Returns:
      a new GtkImage
    • clear

      public void clear()
      Resets the image to be empty.
    • getGicon

      public @Nullable Icon getGicon()

      Gets the GIcon being displayed by the GtkImage.

      The storage type of the image must be ImageType.EMPTY or ImageType.GICON (see getStorageType()). The caller of this function does not own a reference to the returned GIcon.

      Returns:
      a GIcon
    • getIconName

      public @Nullable String getIconName()

      Gets the icon name and size being displayed by the GtkImage.

      The storage type of the image must be ImageType.EMPTY or ImageType.ICON_NAME (see getStorageType()). The returned string is owned by the GtkImage and should not be freed.

      Returns:
      the icon name
    • getIconSize

      public IconSize getIconSize()
      Gets the icon size used by the this Image when rendering icons.
      Returns:
      the image size used by icons
    • getPaintable

      public @Nullable Paintable getPaintable()

      Gets the image GdkPaintable being displayed by the GtkImage.

      The storage type of the image must be ImageType.EMPTY or ImageType.PAINTABLE (see getStorageType()). The caller of this function does not own a reference to the returned paintable.

      Returns:
      the displayed paintable
    • getPixelSize

      public int getPixelSize()
      Gets the pixel size used for named icons.
      Returns:
      the pixel size used for named icons.
    • getStorageType

      public ImageType getStorageType()

      Gets the type of representation being used by the GtkImage to store image data.

      If the GtkImage has no image data, the return value will be ImageType.EMPTY.

      Returns:
      image representation being used
    • setFromFile

      public void setFromFile(@Nullable String filename)

      Sets a GtkImage to show a file.

      See fromFile(String) for details.

      ::: warning Note that this function should not be used with untrusted data. Use a proper image loading framework such as libglycin, which can load many image formats into a GdkTexture, and then use setFromPaintable(Paintable).

      Parameters:
      filename - a filename
    • setFromGicon

      public void setFromGicon(Icon icon)

      Sets a GtkImage to show a GIcon.

      See fromGicon(Icon) for details.

      Parameters:
      icon - an icon
    • setFromIconName

      public void setFromIconName(@Nullable String iconName)

      Sets a GtkImage to show a named icon.

      See fromIconName(String) for details.

      Parameters:
      iconName - an icon name
    • setFromPaintable

      public void setFromPaintable(@Nullable Paintable paintable)

      Sets a GtkImage to show a GdkPaintable.

      See fromPaintable(Paintable) for details.

      Parameters:
      paintable - a GdkPaintable
    • setFromPixbuf

      @Deprecated public void setFromPixbuf(@Nullable Pixbuf pixbuf)
      Deprecated.

      Sets a GtkImage to show a GdkPixbuf.

      See fromPixbuf(Pixbuf) for details.

      Note: This is a helper for setFromPaintable(Paintable), and you can't get back the exact pixbuf once this is called, only a paintable.

      Parameters:
      pixbuf - a GdkPixbuf or NULL
    • setFromResource

      public void setFromResource(@Nullable String resourcePath)

      Sets a GtkImage to show a resource.

      See fromResource(String) for details.

      Parameters:
      resourcePath - a resource path
    • setIconSize

      public void setIconSize(IconSize iconSize)
      Suggests an icon size to the theme for named icons.
      Parameters:
      iconSize - the new icon size
    • setPixelSize

      public void setPixelSize(int pixelSize)

      Sets the pixel size to use for named icons.

      If the pixel size is set to a value != -1, it is used instead of the icon size set by setIconSize(IconSize).

      Parameters:
      pixelSize - the new pixel size
    • builder

      public static Image.Builder<? extends Image.Builder> builder()
      A Image.Builder object constructs a Image with the specified properties. Use the various set...() methods to set properties, and finish construction with Image.Builder.build().
      Returns:
      the builder object