Display an image.

Method Signatures and Parameters

Jt.image(String url)

url (String)

A URL for a hosted image file.

Jt.image(byte[] data)

data (byte[])

Raw image data.

Jt.image(java.nio.file.Path filePath)

filePath (java.nio.file.Path)

A path to a local image file. The path can be absolute or relative to the working directory.

Jt.image(JtUploadedFile uploadedFile)

uploadedFile (io.javelit.core.JtUploadedFile)

An uploaded file.

Jt.imageFromSvg(String svg)

svg (String)

No description

Chainable builder methods

caption(String caption)

Image caption. If this is null (default), no caption is displayed. Markdown is supported, see io.javelit.core.Jt#markdown(String) for more details.

width(String width)

The width of the element. This can be one of the following:

  • content (default): The width of the element matches the width of its content, but doesn't exceed the width of the parent container.
  • stretch: The width of the element matches the width of the parent container.
  • An integer specifying the width in pixels: The element has a fixed width. If the specified width is greater than the width of the parent container, the width of the element matches the width of the parent container.

width(int widthPixels)

The width of the radio group in pixels. The element will have a fixed width. If the specified width is greater than the width of the parent container, the width of the element matches the width of the parent container.

key(String key)

A string to use as the unique key for the widget. If this is omitted, a key will be generated for the widget based on its content. No two widgets may have the same key.

Only applies to input widgets that have a provided .key(). Disable the persistence of this widget value across runs where the widget does not appear.

use()

Put the widget in the app, in the MAIN container.

use(JtContainer container)

Put the widget in the app, in the provided container.

Examples

Image from external URL

import io.javelit.core.Jt;

public class UrlImageApp {
  public static void main(String[] args) {
    Jt.image("https://raw.githubusercontent.com/javelit/public_assets/refs/heads/main/image/mountains2.jpg").use();
  }
}
Image from static resource
package staticImageApp;

import io.javelit.core.Jt;

public class StaticImageApp {
  public static void main(String[] args) {
    // assume static/mountains.jpg is present in the working directory
    Jt.image("app/static/mountains.jpg").use();
  }
}

Image from raw data

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;

import io.javelit.core.Jt;

public class ByteImageApp {
  public static void main(String[] args) throws IOException {
    byte[] imageBytes = generateHexagonImage();
    Jt.image(imageBytes).use();
  }

  private static byte[] generateHexagonImage() throws IOException {
    int width = 800;
    int height = 400;
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = image.createGraphics();
    // Enable anti-aliasing for smoother edges
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    // Light blue background
    g2d.setColor(new Color(240, 248, 255));
    g2d.fillRect(0, 0, width, height);
    // Draw hexagon
    int centerX = width / 2;
    int centerY = height / 2;
    int radius = 150;
    int[] xPoints = new int[6];
    int[] yPoints = new int[6];
    for (int i = 0; i < 6; i++) {
      double angle = Math.PI / 3 * i - Math.PI / 6;
      xPoints[i] = centerX + (int) (radius * Math.cos(angle));
      yPoints[i] = centerY + (int) (radius * Math.sin(angle));
    }
    // Fill hexagon with teal color
    g2d.setColor(new Color(32, 178, 170));
    g2d.fillPolygon(xPoints, yPoints, 6);
    // Draw hexagon border
    g2d.setColor(new Color(0, 128, 128));
    g2d.setStroke(new BasicStroke(3));
    g2d.drawPolygon(xPoints, yPoints, 6);
    g2d.dispose();
    // Convert to PNG bytes
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(image, "PNG", baos);
    return baos.toByteArray();
  }
}

Image from local file

import java.nio.file.Path;

import io.javelit.core.Jt;

public class FileImageApp {
  public static void main(String[] args) {
    // assumes mountains.jpg is present in the working directory
    Jt.image(Path.of("mountains.jpg")).use();
  }
}

From an SVG image

import io.javelit.core.Jt;

public class SvgImageApp {
  public static void main(String[] args) {
    String svg = """
        <svg width="200" height="200" xmlns="http://www.w3.org/2000/svg">
            <circle cx="100" cy="100" r="80" fill="#4CAF50" />
            <path d="M 60 100 L 90 130 L 140 80" stroke="white" stroke-width="8" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
        </svg>
        """;
    Jt.imageFromSvg(svg).use();
  }
}

forum

Still have questions?

Go to our discussions forum for helpful information and advice from Javelit experts.