DEV Community

loizenai
loizenai

Posted on

Java 9 Multi-Resolution Images

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

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay