DEV Community

loading...

Java 9 Multi-Resolution Images

loizenai
Software Engineer - Founder at https://loizenai.com
・2 min read

https://grokonez.com/java/java-9/java-9-multi-resolution-images
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:


package java.awt.image;

public interface MultiResolutionImage {

    Image getResolutionVariant(double destImageWidth, double destImageHeight);
    public List getResolutionVariants();
}

Then, we have an abstract class that implements MultiResolutionImage:


public abstract class AbstractMultiResolutionImage extends java.awt.Image
        implements MultiResolutionImage {

    @Override
    public int getWidth(ImageObserver observer) {
        return getBaseImage().getWidth(observer);
    }

    @Override
    public int getHeight(ImageObserver observer) {
        return getBaseImage().getHeight(observer);
    }

    @Override
    public ImageProducer getSource() {
        return getBaseImage().getSource();
    }

    @Override
    public Graphics getGraphics() {
        throw new UnsupportedOperationException("getGraphics() not supported"
                + " on Multi-Resolution Images");
    }

    @Override
    public Object getProperty(String name, ImageObserver observer) {
        return getBaseImage().getProperty(name, observer);
    }

    protected abstract Image getBaseImage();
}

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.

More at:

https://grokonez.com/java/java-9/java-9-multi-resolution-images
Java 9 Multi-Resolution Images

Discussion (0)