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 *
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 @Override
68 public int getWidth(ImageObserver observer) {
69 return getBaseImage().getWidth(observer);
70 }
71
72 @Override
73 public int getHeight(ImageObserver observer) {
74 return getBaseImage().getHeight(observer);
75 }
76
77 @Override
78 public ImageProducer getSource() {
79 return getBaseImage().getSource();
80 }
81
82 @Override
83 public Graphics getGraphics() {
84 throw new UnsupportedOperationException("getGraphics() not supported"
85 + " on Multi-Resolution Images");
86 }
87
88 @Override
89 public Object getProperty(String name, ImageObserver observer) {
90 return getBaseImage().getProperty(name, observer);
91 }
92
93 /**
94 * Return the base image representing the best version of the image for
95 * rendering at the default width and height.
96 *
97 * @return the base image of the set of multi-resolution images
98 *
99 * @since 9
|
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 import java.util.stream.Collectors;
30 import sun.awt.image.MultiResolutionToolkitImage;
31
32 /**
33 * This class provides default implementations of several {@code Image} methods
34 * for classes that want to implement the {@MultiResolutionImage} interface.
35 *
36 * For example,
37 * <pre> {@code
38 * public class CustomMultiResolutionImage extends AbstractMultiResolutionImage {
39 *
40 * final Image[] resolutionVariants;
41 *
42 * public CustomMultiResolutionImage(Image... resolutionVariants) {
43 * this.resolutionVariants = resolutionVariants;
44 * }
45 *
46 * public Image getResolutionVariant(
47 * double destImageWidth, double destImageHeight) {
48 * // return a resolution variant based on the given destination image size
49 * }
50 *
61 * @see java.awt.Image
62 * @see java.awt.image.MultiResolutionImage
63 *
64 * @since 9
65 */
66 public abstract class AbstractMultiResolutionImage extends java.awt.Image
67 implements MultiResolutionImage {
68
69 @Override
70 public int getWidth(ImageObserver observer) {
71 return getBaseImage().getWidth(observer);
72 }
73
74 @Override
75 public int getHeight(ImageObserver observer) {
76 return getBaseImage().getHeight(observer);
77 }
78
79 @Override
80 public ImageProducer getSource() {
81 Image baseImage = getBaseImage();
82 int baseWidth = baseImage.getWidth(null);
83 int baseHeight = baseImage.getHeight(null);
84
85 return new MultiResolutionToolkitImage.MultiResolutionImageProducer(
86 getResolutionVariants().stream()
87 .map(rvImage -> new ResolutionVariantItem<>(
88 rvImage.getSource(),
89 rvImage.getWidth(null) / baseWidth,
90 rvImage.getHeight(null) / baseHeight))
91 .collect(Collectors.toList()));
92 }
93
94 @Override
95 public Graphics getGraphics() {
96 throw new UnsupportedOperationException("getGraphics() not supported"
97 + " on Multi-Resolution Images");
98 }
99
100 @Override
101 public Object getProperty(String name, ImageObserver observer) {
102 return getBaseImage().getProperty(name, observer);
103 }
104
105 /**
106 * Return the base image representing the best version of the image for
107 * rendering at the default width and height.
108 *
109 * @return the base image of the set of multi-resolution images
110 *
111 * @since 9
|