src/java.desktop/share/classes/java/awt/image/AbstractMultiResolutionImage.java
changeset 47216 71c04702a3d5
parent 45012 4e9dedef0231
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     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 {@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      * This method simply delegates to the same method on the base image and
       
    70      * it is equivalent to: {@code getBaseImage().getWidth(observer)}.
       
    71      *
       
    72      * @return the width of the base image, or -1 if the width is not yet known
       
    73      * @see #getBaseImage()
       
    74      *
       
    75      * @since 9
       
    76      */
       
    77     @Override
       
    78     public int getWidth(ImageObserver observer) {
       
    79         return getBaseImage().getWidth(observer);
       
    80     }
       
    81 
       
    82     /**
       
    83      * This method simply delegates to the same method on the base image and
       
    84      * it is equivalent to: {@code getBaseImage().getHeight(observer)}.
       
    85      *
       
    86      * @return the height of the base image, or -1 if the height is not yet known
       
    87      * @see #getBaseImage()
       
    88      *
       
    89      * @since 9
       
    90      */
       
    91     @Override
       
    92     public int getHeight(ImageObserver observer) {
       
    93         return getBaseImage().getHeight(observer);
       
    94     }
       
    95 
       
    96     /**
       
    97      * This method simply delegates to the same method on the base image and
       
    98      * it is equivalent to: {@code getBaseImage().getSource()}.
       
    99      *
       
   100      * @return the image producer that produces the pixels for the base image
       
   101      * @see #getBaseImage()
       
   102      *
       
   103      * @since 9
       
   104      */
       
   105     @Override
       
   106     public ImageProducer getSource() {
       
   107         return getBaseImage().getSource();
       
   108     }
       
   109 
       
   110     /**
       
   111      * As per the contract of the base {@code Image#getGraphics()} method,
       
   112      * this implementation will always throw {@code UnsupportedOperationException}
       
   113      * since only off-screen images can return a {@code Graphics} object.
       
   114      *
       
   115      * @return throws {@code UnsupportedOperationException}
       
   116      * @throws UnsupportedOperationException this method is not supported
       
   117      */
       
   118     @Override
       
   119     public Graphics getGraphics() {
       
   120         throw new UnsupportedOperationException("getGraphics() not supported"
       
   121                 + " on Multi-Resolution Images");
       
   122     }
       
   123 
       
   124     /**
       
   125      * This method simply delegates to the same method on the base image and
       
   126      * it is equivalent to: {@code getBaseImage().getProperty(name, observer)}.
       
   127      *
       
   128      * @return the value of the named property in the base image
       
   129      * @see #getBaseImage()
       
   130      *
       
   131      * @since 9
       
   132      */
       
   133     @Override
       
   134     public Object getProperty(String name, ImageObserver observer) {
       
   135         return getBaseImage().getProperty(name, observer);
       
   136     }
       
   137 
       
   138     /**
       
   139      * Return the base image representing the best version of the image for
       
   140      * rendering at the default width and height.
       
   141      *
       
   142      * @return the base image of the set of multi-resolution images
       
   143      *
       
   144      * @since 9
       
   145      */
       
   146     protected abstract Image getBaseImage();
       
   147 }