From Documentation

Stop.png This documentation is for an older version of ZK. For the latest one, please click here.

An image component is used to display a client side image. You can use the src attribute to specify a URI where the image is located. This approach is similar to HTML and is useful if you want to display a static image, or any image that can be identified by URL.

<image src="/some/my.jpg"/>

Locale Dependent Image

You can specify "*" for identifying a Locale dependent image. For example, if you have different image for different Locales, you could use the following code.

<image src="/my*.png"/>

Assuming one of your users is visiting your page with de_DE as the preferred Locale. Zk will try to locate the image file called /my_de_DE.png. If it is not found, it will try /my_de.png and finally /my.png.

Please refer to the Browser and Locale Dependent URI section in the Internationalization chapter for details.

Secondly, you could use the setContent method to set the content of an image to an image component directly. Once assigned, the image displayed at the browser is updated automatically. This approach is useful if an image is generated dynamically.

For example, you can generate a map for the location specified by a user as demonstrated below.

	Location: <textbox onChange="updateMap(self.value)"/>
	Map: <image id="image"/>
		void updateMap(String location) {
			if (location.length() > 0) {
				org.zkoss.image.AImage img = new org.zkoss.image.AImage(location);

In the above example, we assume you have a class named MapImage for generating a map of the specified location.

Notice that the image component accepts the content encapsulated by the Image format. If the image generated by your tool is not in this format, you can use the AImage class to wrap a binary array of data, a file or an input stream into the Image interface.

In traditional Web applications, caching a dynamically generated image is complicated, however with the image component, you don't need to worry about it. Once the content of an image is assigned, it belongs to the image component, and the memory it occupies will be released automatically when the image component is no long used.

Tip: If you want to display the content other than an image, say a PDF, you can use the iframe component. Please refer to the relevant section for details.

Image supports javax.awt.image.RenderedImage

Since version 3.0.7 ZK allows image, button and related components to support RenderedImage directly without a format conversion. Here is the example code,

<window title="Test of Live Image">
	<image id="img"/>
	import java.awt.*;
	import java.awt.image.*;
	import java.awt.geom.*;
	int x = 10, y = 10;

	void draw(int x1, int y1, int x2, int y2) {
		BufferedImage bi = new BufferedImage(400, 300, BufferedImage.TYPE_INT_RGB);
		Graphics2D g2d = bi.createGraphics();
		Line2D line = new Line2D.Double(x1, y1, x2, y2);
		g2d.setStroke(new BasicStroke(3));
	draw(x, y, x += 10, y += 10);
	<button label="change" onClick="draw(x, y, x += 10, y += 10)"/>

Last Update : 2022/01/19

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.