Images and Other Resources

Web applications can display various resources, such as images, other embedded content, or downloadable files, that the browser has to load from the server. Image resources are typically displayed with the Image component or as component icons. Embedded browser frames can be displayed with BrowserFrame, and other content with the Embedded component, as described in “Embedded Resources”. Downloadable files are usually provided by clicking a Link or using the FileDownloader extension.

There are several ways to how such resources can be provided by the web server. Static resources can be provided without having to ask for them from the application. For dynamic resources, the user application must be able to create them dynamically. The resource request interfaces in Vaadin allow applications to both refer to static resources as well as dynamically create them. The dynamic creation includes the StreamResource class and the RequestHandler described in “Request Handlers”.

Vaadin also provides low-level facilities for retrieving the URI and other parameters of a HTTP request. We will first look into how applications can provide various kinds of resources and then look into low-level interfaces for handling URIs and parameters to provide resources and functionalities.

Notice that using request handlers to create “pages” is not normally meaningful in Vaadin or in AJAX applications generally. Please see “AJAX” for a detailed explanation.

Resource Interfaces and Classes

The resource classes in Vaadin are grouped under two interfaces: a generic Resource interface and a more specific ConnectorResource interface for resources provided by the servlet.

resource classdiagram hi

Resource Interface and Class Diagram

File Resources

File resources are files stored anywhere in the file system. As such, they can not be retrieved by a regular URL from the server, but need to be requested through the Vaadin servlet. The use of file resources is typically necessary for persistent user data that is not packaged in the web application, which would not be persistent over redeployments.

A file object that can be accessed as a file resource is defined with the standard java.io.File class. You can create the file either with an absolute or relative path, but the base path of the relative path depends on the installation of the web server. For example, with Apache Tomcat, the default current directory would be the installation path of Tomcat.

In the following example, we provide an image resource from a file stored in the web application. Notice that the image is stored under the WEB-INF folder, which is a special folder that is never accessible using an URL, unlike the other folders of a web application. This is a security solution - another would be to store the resource elsewhere in the file system.

Java

  1. // Find the application directory
  2. String basepath = VaadinService.getCurrent()
  3. .getBaseDirectory().getAbsolutePath();
  4. // Image as a file resource
  5. FileResource resource = new FileResource(new File(basepath +
  6. "/WEB-INF/images/image.png"));
  7. // Show the image in the application
  8. Image image = new Image("Image from file", resource);
  9. // Let the user view the file in browser or download it
  10. Link link = new Link("Link to the image file", resource);

In a Maven based Vaadin project the image file should be located inside src/main/webapp/WEB-INF/images/image.png.

Class Loader Resources

The ClassResource allows resources to be loaded from the class path using Java Class Loader. Normally, the relevant class path entry is the WEB-INF/classes folder under the web application, where the Java compilation should compile the Java classes and copy other files from the source tree.

The one-line example below loads an image resource from the application package and displays it in an Image component.

Java

  1. layout.addComponent(new Image(null,
  2. new ClassResource("smiley.jpg")));

Theme Resources

Theme resources of ThemeResource class are files, typically images, included in a theme. A theme is located with the path VAADIN/themes/themename in a web application. The name of a theme resource is given as the parameter for the constructor, with a path relative to the theme folder.

Java

  1. // A theme resource in the current theme ("mytheme")
  2. // Located in: VAADIN/themes/mytheme/img/themeimage.png
  3. ThemeResource resource = new ThemeResource("img/themeimage.png");
  4. // Use the resource
  5. Image image = new Image("My Theme Image", resource);

To use theme resources, you must set the theme for the UI. See “Themes” for more information regarding themes.

Stream Resources

Stream resources allow creating dynamic resource content. Charts are typical examples of dynamic images. To define a stream resource, you need to implement the StreamResource.StreamSource interface and its getStream() method. The method needs to return an InputStream from which the stream can be read.

The following example demonstrates the creation of a simple image in PNG image format.

Java

  1. import java.awt.image.*;
  2. public class MyImageSource implements StreamSource {
  3. ByteArrayOutputStream imagebuffer = null;
  4. int reloads = 0;
  5. // This method generates the stream contents
  6. public InputStream getStream () {
  7. // Create an image
  8. BufferedImage image = new BufferedImage (400, 400,
  9. BufferedImage.TYPE_INT_RGB);
  10. Graphics2D drawable = image.createGraphics();
  11. // Draw something static
  12. drawable.setStroke(new BasicStroke(5));
  13. drawable.setColor(Color.WHITE);
  14. drawable.fillRect(0, 0, 400, 400);
  15. drawable.setColor(Color.BLACK);
  16. drawable.drawOval(50, 50, 300, 300);
  17. // Draw something dynamic
  18. drawable.setFont(new Font("Montserrat",
  19. Font.PLAIN, 48));
  20. drawable.drawString("Reloads=" + reloads, 75, 216);
  21. reloads++;
  22. drawable.setColor(new Color(0, 165, 235));
  23. int x= (int) (200-10 + 150*Math.sin(reloads * 0.3));
  24. int y= (int) (200-10 + 150*Math.cos(reloads * 0.3));
  25. drawable.fillOval(x, y, 20, 20);
  26. try {
  27. // Write the image to a buffer
  28. imagebuffer = new ByteArrayOutputStream();
  29. ImageIO.write(image, "png", imagebuffer);
  30. // Return a stream from the buffer
  31. return new ByteArrayInputStream(
  32. imagebuffer.toByteArray());
  33. } catch (IOException e) {
  34. return null;
  35. }
  36. }
  37. }

The content of the generated image is dynamic, as it updates the reloads counter with every call. The ImageIO. write() method writes the image to an output stream, while we had to return an input stream, so we stored the image contents to a temporary buffer.

Below we display the image with the Image component.

Java

  1. // Create an instance of our stream source.
  2. StreamSource imagesource = new MyImageSource();
  3. // Create a resource that uses the stream source and give it
  4. // a name. The constructor will automatically register the
  5. // resource in the application.
  6. StreamResource resource =
  7. new StreamResource(imagesource, "myimage.png");
  8. // Create an image component that gets its contents
  9. // from the resource.
  10. layout.addComponent(new Image("Image title", resource));

The resulting image is shown in A stream resource.

application streamresource

A stream resource

Another way to create dynamic content is a request handler, described in “Request Handlers”.