1 /*
   2  * Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package java.awt.image;
  26 
  27 import java.awt.Graphics;
  28 import java.awt.Image;
  29 
  30 /**
  31  * This class provides default implementations of several {@code Image} methods
  32  * for classes that want to implement the {@code MultiResolutionImage}
  33  * interface.
  34  *
  35  * For example,
  36  * <pre> {@code
  37  * public class CustomMultiResolutionImage extends AbstractMultiResolutionImage {
  38  *
  39  *     final Image[] resolutionVariants;
  40  *
  41  *     public CustomMultiResolutionImage(Image... resolutionVariants) {
  42  *          this.resolutionVariants = resolutionVariants;
  43  *     }
  44  *
  45  *     public Image getResolutionVariant(
  46  *             double destImageWidth, double destImageHeight) {
  47  *         // return a resolution variant based on the given destination image size
  48  *     }
  49  *
  50  *     public List<Image> getResolutionVariants() {
  51  *         return Collections.unmodifiableList(Arrays.asList(resolutionVariants));
  52  *     }
  53  *
  54  *     protected Image getBaseImage() {
  55  *         return resolutionVariants[0];
  56  *     }
  57  * }
  58  * } </pre>
  59  *
  60  * @see java.awt.Image
  61  * @see java.awt.image.MultiResolutionImage
  62  *
  63  * @since 9
  64  */
  65 public abstract class AbstractMultiResolutionImage extends java.awt.Image
  66         implements MultiResolutionImage {
  67 
  68     /**
  69      * Constructor for subclasses to call.
  70      */
  71     public AbstractMultiResolutionImage() {}
  72 
  73     /**
  74      * This method simply delegates to the same method on the base image and
  75      * it is equivalent to: {@code getBaseImage().getWidth(observer)}.
  76      *
  77      * @return the width of the base image, or -1 if the width is not yet known
  78      * @see #getBaseImage()
  79      *
  80      * @since 9
  81      */
  82     @Override
  83     public int getWidth(ImageObserver observer) {
  84         return getBaseImage().getWidth(observer);
  85     }
  86 
  87     /**
  88      * This method simply delegates to the same method on the base image and
  89      * it is equivalent to: {@code getBaseImage().getHeight(observer)}.
  90      *
  91      * @return the height of the base image, or -1 if the height is not yet known
  92      * @see #getBaseImage()
  93      *
  94      * @since 9
  95      */
  96     @Override
  97     public int getHeight(ImageObserver observer) {
  98         return getBaseImage().getHeight(observer);
  99     }
 100 
 101     /**
 102      * This method simply delegates to the same method on the base image and
 103      * it is equivalent to: {@code getBaseImage().getSource()}.
 104      *
 105      * @return the image producer that produces the pixels for the base image
 106      * @see #getBaseImage()
 107      *
 108      * @since 9
 109      */
 110     @Override
 111     public ImageProducer getSource() {
 112         return getBaseImage().getSource();
 113     }
 114 
 115     /**
 116      * As per the contract of the base {@code Image#getGraphics()} method,
 117      * this implementation will always throw {@code UnsupportedOperationException}
 118      * since only off-screen images can return a {@code Graphics} object.
 119      *
 120      * @return throws {@code UnsupportedOperationException}
 121      * @throws UnsupportedOperationException this method is not supported
 122      */
 123     @Override
 124     public Graphics getGraphics() {
 125         throw new UnsupportedOperationException("getGraphics() not supported"
 126                 + " on Multi-Resolution Images");
 127     }
 128 
 129     /**
 130      * This method simply delegates to the same method on the base image and
 131      * it is equivalent to: {@code getBaseImage().getProperty(name, observer)}.
 132      *
 133      * @return the value of the named property in the base image
 134      * @see #getBaseImage()
 135      *
 136      * @since 9
 137      */
 138     @Override
 139     public Object getProperty(String name, ImageObserver observer) {
 140         return getBaseImage().getProperty(name, observer);
 141     }
 142 
 143     /**
 144      * Return the base image representing the best version of the image for
 145      * rendering at the default width and height.
 146      *
 147      * @return the base image of the set of multi-resolution images
 148      *
 149      * @since 9
 150      */
 151     protected abstract Image getBaseImage();
 152 }