1 /*
   2  * Copyright (c) 2014, 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 for the
  32  * <code>MultiResolutionImage</code> interface. The developer needs only
  33  * to subclass this abstract class and define the <code>getResolutionVariant</code>,
  34  * <code>getResolutionVariants</code>, and <code>getBaseImage</code> methods.
  35  *
  36  *
  37  * For example,
  38  * {@code
  39  * public class CustomMultiResolutionImage extends AbstractMultiResolutionImage {
  40  *
  41  *     int baseImageIndex;
  42  *     Image[] resolutionVariants;
  43  *
  44  *     public CustomMultiResolutionImage(int baseImageIndex,
  45  *             Image... resolutionVariants) {
  46  *          this.baseImageIndex = baseImageIndex;
  47  *          this.resolutionVariants = resolutionVariants;
  48  *     }
  49  *
  50  *     @Override
  51  *     public Image getResolutionVariant(float logicalDPIX, float logicalDPIY,
  52  *             float baseImageWidth, float baseImageHeight,
  53  *             float destImageWidth, float destImageHeight) {
  54  *         // return a resolution variant based on the given logical DPI,
  55  *         // base image size, or destination image size
  56  *     }
  57  *
  58  *     @Override
  59  *     public List<Image> getResolutionVariants() {
  60  *         return Arrays.asList(resolutionVariants);
  61  *     }
  62  *
  63  *     protected Image getBaseImage() {
  64  *         return resolutionVariants[baseImageIndex];
  65  *     }
  66  * }
  67  * }
  68  *
  69  * @see java.awt.Image
  70  * @see java.awt.image.MultiResolutionImage
  71  *
  72  * @since 1.9
  73  */
  74 public abstract class AbstractMultiResolutionImage extends java.awt.Image
  75         implements MultiResolutionImage {
  76 
  77     /**
  78      * @inheritDoc
  79      */
  80     @Override
  81     public int getWidth(ImageObserver observer) {
  82         return getBaseImage().getWidth(null);
  83     }
  84 
  85     /**
  86      * @inheritDoc
  87      */
  88     @Override
  89     public int getHeight(ImageObserver observer) {
  90         return getBaseImage().getHeight(null);
  91     }
  92 
  93     /**
  94      * @inheritDoc
  95      */
  96     @Override
  97     public ImageProducer getSource() {
  98         return getBaseImage().getSource();
  99     }
 100 
 101     /**
 102      * @inheritDoc
 103      */
 104     @Override
 105     public Graphics getGraphics() {
 106         return getBaseImage().getGraphics();
 107 
 108     }
 109 
 110     /**
 111      * @inheritDoc
 112      */
 113     @Override
 114     public Object getProperty(String name, ImageObserver observer) {
 115         return getBaseImage().getProperty(name, observer);
 116     }
 117 
 118     /**
 119      * @return base image
 120      */
 121     protected abstract Image getBaseImage();
 122 }