1 /*
   2  * Copyright (c) 2015, 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 {@MultiResolutionImage} interface.
  33  *
  34  * For example,
  35  * <pre> {@code
  36  * public class CustomMultiResolutionImage extends AbstractMultiResolutionImage {
  37  *
  38  *     final Image[] resolutionVariants;
  39  *
  40  *     public CustomMultiResolutionImage(Image... resolutionVariants) {
  41  *          this.resolutionVariants = resolutionVariants;
  42  *     }
  43  *
  44  *     public Image getResolutionVariant(
  45  *             double destImageWidth, double destImageHeight) {
  46  *         // return a resolution variant based on the given destination image size
  47  *     }
  48  *
  49  *     public List<Image> getResolutionVariants() {
  50  *         return Collections.unmodifiableList(Arrays.asList(resolutionVariants));
  51  *     }
  52  *
  53  *     protected Image getBaseImage() {
  54  *         return resolutionVariants[0];
  55  *     }
  56  * }
  57  * } </pre>
  58  *
  59  * @see java.awt.Image
  60  * @see java.awt.image.MultiResolutionImage
  61  *
  62  * @since 9
  63  */
  64 public abstract class AbstractMultiResolutionImage extends java.awt.Image
  65         implements MultiResolutionImage {
  66 
  67     /**
  68      * This method simply delegates to the same method on the base image and
  69      * it is equivalent to: {@code getBaseImage().getWidth(observer)}.
  70      *
  71      * @return the width of the base image, or -1 if the width is not yet known
  72      * @see #getBaseImage()
  73      *
  74      * @since 9
  75      */
  76     @Override
  77     public int getWidth(ImageObserver observer) {
  78         return getBaseImage().getWidth(observer);
  79     }
  80 
  81     /**
  82      * This method simply delegates to the same method on the base image and
  83      * it is equivalent to: {@code getBaseImage().getHeight(observer)}.
  84      *
  85      * @return the height of the base image, or -1 if the height is not yet known
  86      * @see #getBaseImage()
  87      *
  88      * @since 9
  89      */
  90     @Override
  91     public int getHeight(ImageObserver observer) {
  92         return getBaseImage().getHeight(observer);
  93     }
  94 
  95     /**
  96      * This method simply delegates to the same method on the base image and
  97      * it is equivalent to: {@code getBaseImage().getSource()}.
  98      *
  99      * @return the image producer that produces the pixels for the base image
 100      * @see #getBaseImage()
 101      *
 102      * @since 9
 103      */
 104     @Override
 105     public ImageProducer getSource() {
 106         return getBaseImage().getSource();
 107     }
 108 
 109     /**
 110      * As per the contract of the base {@code Image#getGraphics()} method,
 111      * this implementation will always throw {@code UnsupportedOperationException}
 112      * since only off-screen images can return a {@code Graphics} object.
 113      *
 114      * @return throws {@code UnsupportedOperationException}
 115      * @throws UnsupportedOperationException this method is not supported
 116      */
 117     @Override
 118     public Graphics getGraphics() {
 119         throw new UnsupportedOperationException("getGraphics() not supported"
 120                 + " on Multi-Resolution Images");
 121     }
 122 
 123     /**
 124      * This method simply delegates to the same method on the base image and
 125      * it is equivalent to: {@code getBaseImage().getProperty(name, observer)}.
 126      *
 127      * @return the value of the named property in the base image
 128      * @see #getBaseImage()
 129      *
 130      * @since 9
 131      */
 132     @Override
 133     public Object getProperty(String name, ImageObserver observer) {
 134         return getBaseImage().getProperty(name, observer);
 135     }
 136 
 137     /**
 138      * Return the base image representing the best version of the image for
 139      * rendering at the default width and height.
 140      *
 141      * @return the base image of the set of multi-resolution images
 142      *
 143      * @since 9
 144      */
 145     protected abstract Image getBaseImage();
 146 }