Java 9 Multi-Resolution Images


In this tutorial, we will introduce Java 9 Multi-Resolution Images, a new API that allows a set of images with different resolutions (width and height) to be encapsulated into only one single image.

I. Basic operations

The new API which is defined in the java.awt.image package can help us:
– Encapsulate many images with different resolutions into an image as its variants.
– Get all variants in the image.
– Get a resolution-specific image variant – the best variant to represent the logical image at the indicated size based on a given DPI metric.

Now take a look at MultiResolutionImage with 2 importants methods getResolutionVariant() that returns an Image and getResolutionVariants() that returns a list of Images:

Then, we have an abstract class that implements MultiResolutionImage:

This abstract AbstractMultiResolutionImage class provides default implementations of several Image methods. We can create our own custom class by extending it to implement the interface.

Java 9 also provides a very simple useful implementation – BaseMultiResolutionImage class:

In the tutorial, we will use this class to do some interesting things to test new API.

II. Example

– We have 3 images with different resolutions ([806,648] – [256,206] – [240,240]) which will be encapsulated into only one single MultiResolutionImage image.
– Next, we retrieve all variants (List of Images) in that MultiResolutionImage image by using getResolutionVariants() method.
– Then we get a resolution-specific image variant for each indicated size using getResolutionVariant() method.

Run code above, the result is:

By grokonez | March 27, 2017.

Last updated on September 11, 2018.

Related Posts

Got Something To Say:

Your email address will not be published. Required fields are marked *