test/java/awt/image/MultiResolutionImageTest.java

Print this page


   1 /*
   2  * Copyright (c) 2013, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.awt.Color;
  25 import java.awt.Graphics;
  26 import java.awt.Graphics2D;
  27 import java.awt.Image;
  28 import java.awt.Toolkit;
  29 import java.awt.image.BufferedImage;
  30 import java.io.File;
  31 import java.lang.reflect.Method;
  32 import java.net.URL;
  33 import javax.imageio.ImageIO;
  34 import sun.awt.OSInfo;
  35 import sun.awt.SunHints;
  36 import java.awt.MediaTracker;

  37 import java.awt.geom.AffineTransform;
  38 import java.awt.image.ImageObserver;
  39 import java.util.Arrays;
  40 import java.util.List;
  41 import javax.swing.JPanel;
  42 import sun.awt.SunToolkit;
  43 import sun.awt.image.MultiResolutionImage;
  44 
  45 /**
  46  * @test
  47  * @bug 8011059
  48  * @author Alexander Scherbatiy
  49  * @summary [macosx] Make JDK demos look perfect on retina displays
  50  * @run main MultiResolutionImageTest CUSTOM
  51  * @run main MultiResolutionImageTest TOOLKIT_PREPARE
  52  * @run main MultiResolutionImageTest TOOLKIT_LOAD
  53  * @run main MultiResolutionImageTest TOOLKIT
  54  */
  55 public class MultiResolutionImageTest {
  56 
  57     private static final int IMAGE_WIDTH = 300;
  58     private static final int IMAGE_HEIGHT = 200;
  59     private static final Color COLOR_1X = Color.GREEN;
  60     private static final Color COLOR_2X = Color.BLUE;
  61     private static final String IMAGE_NAME_1X = "image.png";
  62     private static final String IMAGE_NAME_2X = "image@2x.png";


  63 
  64     public static void main(String[] args) throws Exception {
  65 
  66         System.out.println("args: " + args.length);
  67 
  68         if (args.length == 0) {
  69             throw new RuntimeException("Not found a test");
  70         }
  71 
  72         String test = args[0];
  73 
  74         System.out.println("TEST: " + test);
  75         System.out.println("CHECK OS: " + checkOS());
  76 
  77         if ("CUSTOM".equals(test)) {
  78             testCustomMultiResolutionImage();
  79         } else if (checkOS()) {
  80             switch (test) {
  81                 case "CUSTOM":
  82                     break;


 185             implements MultiResolutionImage {
 186 
 187         Image highResolutionImage;
 188 
 189         public MultiResolutionBufferedImage() {
 190             super(IMAGE_WIDTH, IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB);
 191             highResolutionImage = new BufferedImage(
 192                     2 * IMAGE_WIDTH, 2 * IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB);
 193             draw(getGraphics(), 1);
 194             draw(highResolutionImage.getGraphics(), 2);
 195         }
 196 
 197         void draw(Graphics graphics, float resolution) {
 198             Graphics2D g2 = (Graphics2D) graphics;
 199             g2.scale(resolution, resolution);
 200             g2.setColor((resolution == 1) ? COLOR_1X : COLOR_2X);
 201             g2.fillRect(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);
 202         }
 203 
 204         @Override
 205         public Image getResolutionVariant(int width, int height) {





 206             return ((width <= getWidth() && height <= getHeight()))
 207                     ? this : highResolutionImage;
 208         }
 209 
 210         @Override
 211         public List<Image> getResolutionVariants() {
 212             return Arrays.asList(this, highResolutionImage);
 213         }








 214     }
 215 
 216     static void testToolkitMultiResolutionImagePrepare() throws Exception {
 217 
 218         generateImages();
 219 
 220         File imageFile = new File(IMAGE_NAME_1X);
 221         String fileName = imageFile.getAbsolutePath();
 222 
 223         Image image = Toolkit.getDefaultToolkit().getImage(fileName);
 224 
 225         SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
 226         toolkit.prepareImage(image, IMAGE_WIDTH, IMAGE_HEIGHT, new LoadImageObserver(image));
 227 
 228         testToolkitMultiResolutionImageLoad(image);
 229     }
 230 
 231     static void testToolkitMultiResolutionImageLoad() throws Exception {
 232 
 233         generateImages();


 236         String fileName = imageFile.getAbsolutePath();
 237         Image image = Toolkit.getDefaultToolkit().getImage(fileName);
 238         testToolkitMultiResolutionImageLoad(image);
 239     }
 240 
 241     static void testToolkitMultiResolutionImageLoad(Image image) throws Exception {
 242 
 243         MediaTracker tracker = new MediaTracker(new JPanel());
 244         tracker.addImage(image, 0);
 245         tracker.waitForID(0);
 246         if (tracker.isErrorAny()) {
 247             throw new RuntimeException("Error during image loading");
 248         }
 249         tracker.removeImage(image, 0);
 250 
 251         testImageLoaded(image);
 252 
 253         int w = image.getWidth(null);
 254         int h = image.getHeight(null);
 255 
 256         Image resolutionVariant = ((MultiResolutionImage) image)
 257                 .getResolutionVariant(2 * w, 2 * h);
 258 
 259         if (image == resolutionVariant) {
 260             throw new RuntimeException("Resolution variant is not loaded");
 261         }
 262 
 263         testImageLoaded(resolutionVariant);
 264     }
 265 








 266     static void testImageLoaded(Image image) {
 267 
 268         SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
 269 
 270         int flags = toolkit.checkImage(image, IMAGE_WIDTH, IMAGE_WIDTH, new SilentImageObserver());
 271         if ((flags & (ImageObserver.FRAMEBITS | ImageObserver.ALLBITS)) == 0) {
 272             throw new RuntimeException("Image is not loaded!");
 273         }
 274     }
 275 
 276     static class SilentImageObserver implements ImageObserver {
 277 
 278         @Override
 279         public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
 280             throw new RuntimeException("Observer should not be called!");
 281         }
 282     }
 283 
 284     static class LoadImageObserver implements ImageObserver {
 285 


 356         }
 357     }
 358 
 359     static void testToolkitMultiResolutionImage(Image image, boolean enableImageScaling)
 360             throws Exception {
 361 
 362         MediaTracker tracker = new MediaTracker(new JPanel());
 363         tracker.addImage(image, 0);
 364         tracker.waitForID(0);
 365         if (tracker.isErrorAny()) {
 366             throw new RuntimeException("Error during image loading");
 367         }
 368 
 369         final BufferedImage bufferedImage1x = new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT,
 370                 BufferedImage.TYPE_INT_RGB);
 371         Graphics2D g1x = (Graphics2D) bufferedImage1x.getGraphics();
 372         setImageScalingHint(g1x, false);
 373         g1x.drawImage(image, 0, 0, null);
 374         checkColor(bufferedImage1x.getRGB(3 * IMAGE_WIDTH / 4, 3 * IMAGE_HEIGHT / 4), false);
 375 
 376         Image resolutionVariant = ((MultiResolutionImage) image).
 377                 getResolutionVariant(2 * IMAGE_WIDTH, 2 * IMAGE_HEIGHT);

 378 
 379         if (resolutionVariant == null) {
 380             throw new RuntimeException("Resolution variant is null");
 381         }
 382 
 383         MediaTracker tracker2x = new MediaTracker(new JPanel());
 384         tracker2x.addImage(resolutionVariant, 0);
 385         tracker2x.waitForID(0);
 386         if (tracker2x.isErrorAny()) {
 387             throw new RuntimeException("Error during scalable image loading");
 388         }
 389 
 390         final BufferedImage bufferedImage2x = new BufferedImage(2 * IMAGE_WIDTH,
 391                 2 * IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB);
 392         Graphics2D g2x = (Graphics2D) bufferedImage2x.getGraphics();
 393         setImageScalingHint(g2x, enableImageScaling);
 394         g2x.drawImage(image, 0, 0, 2 * IMAGE_WIDTH, 2 * IMAGE_HEIGHT, 0, 0, IMAGE_WIDTH, IMAGE_HEIGHT, null);
 395         checkColor(bufferedImage2x.getRGB(3 * IMAGE_WIDTH / 2, 3 * IMAGE_HEIGHT / 2), enableImageScaling);
 396 
 397         if (!(image instanceof MultiResolutionImage)) {
 398             throw new RuntimeException("Not a MultiResolutionImage");
 399         }
 400 
 401         MultiResolutionImage multiResolutionImage = (MultiResolutionImage) image;
 402 
 403         Image image1x = multiResolutionImage.getResolutionVariant(IMAGE_WIDTH, IMAGE_HEIGHT);
 404         Image image2x = multiResolutionImage.getResolutionVariant(2 * IMAGE_WIDTH, 2 * IMAGE_HEIGHT);



 405 
 406         if (image1x.getWidth(null) * 2 != image2x.getWidth(null)
 407                 || image1x.getHeight(null) * 2 != image2x.getHeight(null)) {
 408             throw new RuntimeException("Wrong resolution variant size");
 409         }
 410     }
 411 
 412     static void testToolkitImageObserver(final Image image) {
 413 
 414         ImageObserver observer = new ImageObserver() {
 415 
 416             @Override
 417             public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
 418 
 419                 if (img != image) {
 420                     throw new RuntimeException("Wrong image in observer");
 421                 }
 422 
 423                 if ((infoflags & (ImageObserver.ERROR | ImageObserver.ABORT)) != 0) {
 424                     throw new RuntimeException("Error during image loading");
 425                 }
 426 
 427                 return (infoflags & ImageObserver.ALLBITS) == 0;
 428 
 429             }
 430         };
 431 
 432         final BufferedImage bufferedImage2x = new BufferedImage(2 * IMAGE_WIDTH,
 433                 2 * IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB);
 434         Graphics2D g2x = (Graphics2D) bufferedImage2x.getGraphics();
 435         setImageScalingHint(g2x, true);
 436 
 437         g2x.drawImage(image, 0, 0, 2 * IMAGE_WIDTH, 2 * IMAGE_HEIGHT, 0, 0, IMAGE_WIDTH, IMAGE_HEIGHT, observer);
 438 
 439     }
 440 
 441     static void setImageScalingHint(Graphics2D g2d, boolean enableImageScaling) {
 442         g2d.setRenderingHint(SunHints.KEY_RESOLUTION_VARIANT, enableImageScaling
 443                 ? SunHints.VALUE_RESOLUTION_VARIANT_ON
 444                 : SunHints.VALUE_RESOLUTION_VARIANT_OFF);
 445     }
 446 
 447     static void checkColor(int rgb, boolean isImageScaled) {
 448 
 449         if (!isImageScaled && COLOR_1X.getRGB() != rgb) {
 450             throw new RuntimeException("Wrong 1x color: " + new Color(rgb));
 451         }
 452 
 453         if (isImageScaled && COLOR_2X.getRGB() != rgb) {
 454             throw new RuntimeException("Wrong 2x color" + new Color(rgb));
 455         }
 456     }
 457 
 458     static void generateImages() throws Exception {
 459         if (!new File(IMAGE_NAME_1X).exists()) {
 460             generateImage(1);
 461         }
 462 
 463         if (!new File(IMAGE_NAME_2X).exists()) {
 464             generateImage(2);


   1 /*
   2  * Copyright (c) 2013, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.awt.Color;
  25 import java.awt.Graphics;
  26 import java.awt.Graphics2D;
  27 import java.awt.Image;
  28 import java.awt.Toolkit;
  29 import java.awt.image.BufferedImage;
  30 import java.io.File;
  31 import java.lang.reflect.Method;
  32 import java.net.URL;
  33 import javax.imageio.ImageIO;
  34 import sun.awt.OSInfo;

  35 import java.awt.MediaTracker;
  36 import java.awt.RenderingHints;
  37 import java.awt.geom.AffineTransform;
  38 import java.awt.image.ImageObserver;
  39 import java.util.Arrays;
  40 import java.util.List;
  41 import javax.swing.JPanel;
  42 import sun.awt.SunToolkit;
  43 import java.awt.image.MultiResolutionImage;
  44 
  45 /**
  46  * @test
  47  * @bug 8011059 8029339
  48  * @author Alexander Scherbatiy
  49  * @summary [macosx] Make JDK demos look perfect on retina displays
  50  * @run main MultiResolutionImageTest CUSTOM
  51  * @run main MultiResolutionImageTest TOOLKIT_PREPARE
  52  * @run main MultiResolutionImageTest TOOLKIT_LOAD
  53  * @run main MultiResolutionImageTest TOOLKIT
  54  */
  55 public class MultiResolutionImageTest {
  56 
  57     private static final int IMAGE_WIDTH = 300;
  58     private static final int IMAGE_HEIGHT = 200;
  59     private static final Color COLOR_1X = Color.GREEN;
  60     private static final Color COLOR_2X = Color.BLUE;
  61     private static final String IMAGE_NAME_1X = "image.png";
  62     private static final String IMAGE_NAME_2X = "image@2x.png";
  63     private static final AffineTransform IDENTITY
  64             = AffineTransform.getScaleInstance(1, 1);
  65 
  66     public static void main(String[] args) throws Exception {
  67 
  68         System.out.println("args: " + args.length);
  69 
  70         if (args.length == 0) {
  71             throw new RuntimeException("Not found a test");
  72         }
  73 
  74         String test = args[0];
  75 
  76         System.out.println("TEST: " + test);
  77         System.out.println("CHECK OS: " + checkOS());
  78 
  79         if ("CUSTOM".equals(test)) {
  80             testCustomMultiResolutionImage();
  81         } else if (checkOS()) {
  82             switch (test) {
  83                 case "CUSTOM":
  84                     break;


 187             implements MultiResolutionImage {
 188 
 189         Image highResolutionImage;
 190 
 191         public MultiResolutionBufferedImage() {
 192             super(IMAGE_WIDTH, IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB);
 193             highResolutionImage = new BufferedImage(
 194                     2 * IMAGE_WIDTH, 2 * IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB);
 195             draw(getGraphics(), 1);
 196             draw(highResolutionImage.getGraphics(), 2);
 197         }
 198 
 199         void draw(Graphics graphics, float resolution) {
 200             Graphics2D g2 = (Graphics2D) graphics;
 201             g2.scale(resolution, resolution);
 202             g2.setColor((resolution == 1) ? COLOR_1X : COLOR_2X);
 203             g2.fillRect(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);
 204         }
 205 
 206         @Override
 207         public Image getResolutionVariant(float logicalDPIX, float logicalDPIY,
 208                 float baseWidth, float baseHeight, float destWidth, float destHeight) {
 209             return getResolutionVariant(destWidth, destHeight);
 210         }
 211 
 212         public Image getResolutionVariant(float width, float height) {
 213             return ((width <= getWidth() && height <= getHeight()))
 214                     ? this : highResolutionImage;
 215         }
 216 
 217         @Override
 218         public List<Image> getResolutionVariants() {
 219             return Arrays.asList(this, highResolutionImage);
 220         }
 221 
 222         static float scaleWidth(double size, AffineTransform t) {
 223             return (float) (size * Math.hypot(t.getScaleX(), t.getShearY()));
 224         }
 225 
 226         static float scaleHeight(double size, AffineTransform t) {
 227             return (float) (size * Math.hypot(t.getShearX(), t.getScaleY()));
 228         }
 229     }
 230 
 231     static void testToolkitMultiResolutionImagePrepare() throws Exception {
 232 
 233         generateImages();
 234 
 235         File imageFile = new File(IMAGE_NAME_1X);
 236         String fileName = imageFile.getAbsolutePath();
 237 
 238         Image image = Toolkit.getDefaultToolkit().getImage(fileName);
 239 
 240         SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
 241         toolkit.prepareImage(image, IMAGE_WIDTH, IMAGE_HEIGHT, new LoadImageObserver(image));
 242 
 243         testToolkitMultiResolutionImageLoad(image);
 244     }
 245 
 246     static void testToolkitMultiResolutionImageLoad() throws Exception {
 247 
 248         generateImages();


 251         String fileName = imageFile.getAbsolutePath();
 252         Image image = Toolkit.getDefaultToolkit().getImage(fileName);
 253         testToolkitMultiResolutionImageLoad(image);
 254     }
 255 
 256     static void testToolkitMultiResolutionImageLoad(Image image) throws Exception {
 257 
 258         MediaTracker tracker = new MediaTracker(new JPanel());
 259         tracker.addImage(image, 0);
 260         tracker.waitForID(0);
 261         if (tracker.isErrorAny()) {
 262             throw new RuntimeException("Error during image loading");
 263         }
 264         tracker.removeImage(image, 0);
 265 
 266         testImageLoaded(image);
 267 
 268         int w = image.getWidth(null);
 269         int h = image.getHeight(null);
 270 
 271         Image resolutionVariant = getOSDependentToolkitResolutionVariant(
 272                 (MultiResolutionImage) image, w, h);
 273 
 274         if (image == resolutionVariant) {
 275             throw new RuntimeException("Resolution variant is not loaded");
 276         }
 277 
 278         testImageLoaded(resolutionVariant);
 279     }
 280 
 281     static Image getOSDependentToolkitResolutionVariant(
 282             MultiResolutionImage mrImage, int w, int h) {
 283         if (OSInfo.OSType.WINDOWS.equals(OSInfo.getOSType())) {
 284             return mrImage.getResolutionVariant(2, 2, w, h, 2 * w, 2 * h);
 285         }
 286         return mrImage.getResolutionVariant(1, 1, w, h, 2 * w, 2 * h);
 287     }
 288 
 289     static void testImageLoaded(Image image) {
 290 
 291         SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
 292 
 293         int flags = toolkit.checkImage(image, IMAGE_WIDTH, IMAGE_WIDTH, new SilentImageObserver());
 294         if ((flags & (ImageObserver.FRAMEBITS | ImageObserver.ALLBITS)) == 0) {
 295             throw new RuntimeException("Image is not loaded!");
 296         }
 297     }
 298 
 299     static class SilentImageObserver implements ImageObserver {
 300 
 301         @Override
 302         public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
 303             throw new RuntimeException("Observer should not be called!");
 304         }
 305     }
 306 
 307     static class LoadImageObserver implements ImageObserver {
 308 


 379         }
 380     }
 381 
 382     static void testToolkitMultiResolutionImage(Image image, boolean enableImageScaling)
 383             throws Exception {
 384 
 385         MediaTracker tracker = new MediaTracker(new JPanel());
 386         tracker.addImage(image, 0);
 387         tracker.waitForID(0);
 388         if (tracker.isErrorAny()) {
 389             throw new RuntimeException("Error during image loading");
 390         }
 391 
 392         final BufferedImage bufferedImage1x = new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT,
 393                 BufferedImage.TYPE_INT_RGB);
 394         Graphics2D g1x = (Graphics2D) bufferedImage1x.getGraphics();
 395         setImageScalingHint(g1x, false);
 396         g1x.drawImage(image, 0, 0, null);
 397         checkColor(bufferedImage1x.getRGB(3 * IMAGE_WIDTH / 4, 3 * IMAGE_HEIGHT / 4), false);
 398 
 399         Image resolutionVariant = ((MultiResolutionImage) image)
 400                 .getResolutionVariant(2, 2, IMAGE_WIDTH, IMAGE_HEIGHT,
 401                         2 * IMAGE_WIDTH, 2 * IMAGE_HEIGHT);
 402 
 403         if (resolutionVariant == null) {
 404             throw new RuntimeException("Resolution variant is null");
 405         }
 406 
 407         MediaTracker tracker2x = new MediaTracker(new JPanel());
 408         tracker2x.addImage(resolutionVariant, 0);
 409         tracker2x.waitForID(0);
 410         if (tracker2x.isErrorAny()) {
 411             throw new RuntimeException("Error during scalable image loading");
 412         }
 413 
 414         final BufferedImage bufferedImage2x = new BufferedImage(2 * IMAGE_WIDTH,
 415                 2 * IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB);
 416         Graphics2D g2x = (Graphics2D) bufferedImage2x.getGraphics();
 417         setImageScalingHint(g2x, enableImageScaling);
 418         g2x.drawImage(image, 0, 0, 2 * IMAGE_WIDTH, 2 * IMAGE_HEIGHT, 0, 0, IMAGE_WIDTH, IMAGE_HEIGHT, null);
 419         checkColor(bufferedImage2x.getRGB(3 * IMAGE_WIDTH / 2, 3 * IMAGE_HEIGHT / 2), enableImageScaling);
 420 
 421         if (!(image instanceof MultiResolutionImage)) {
 422             throw new RuntimeException("Not a MultiResolutionImage");
 423         }
 424 
 425         MultiResolutionImage multiResolutionImage = (MultiResolutionImage) image;
 426 
 427         Image image1x = multiResolutionImage
 428                 .getResolutionVariant(1, 1, IMAGE_WIDTH, IMAGE_HEIGHT,
 429                         IMAGE_WIDTH, IMAGE_HEIGHT);
 430         Image image2x = getOSDependentToolkitResolutionVariant(
 431                 multiResolutionImage, IMAGE_WIDTH, IMAGE_WIDTH);
 432 
 433         if (image1x.getWidth(null) * 2 != image2x.getWidth(null)
 434                 || image1x.getHeight(null) * 2 != image2x.getHeight(null)) {
 435             throw new RuntimeException("Wrong resolution variant size");
 436         }
 437     }
 438 
 439     static void testToolkitImageObserver(final Image image) {
 440 
 441         ImageObserver observer = new ImageObserver() {
 442 
 443             @Override
 444             public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
 445 
 446                 if (img != image) {
 447                     throw new RuntimeException("Wrong image in observer");
 448                 }
 449 
 450                 if ((infoflags & (ImageObserver.ERROR | ImageObserver.ABORT)) != 0) {
 451                     throw new RuntimeException("Error during image loading");
 452                 }
 453 
 454                 return (infoflags & ImageObserver.ALLBITS) == 0;
 455 
 456             }
 457         };
 458 
 459         final BufferedImage bufferedImage2x = new BufferedImage(2 * IMAGE_WIDTH,
 460                 2 * IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB);
 461         Graphics2D g2x = (Graphics2D) bufferedImage2x.getGraphics();
 462         setImageScalingHint(g2x, true);
 463 
 464         g2x.drawImage(image, 0, 0, 2 * IMAGE_WIDTH, 2 * IMAGE_HEIGHT, 0, 0, IMAGE_WIDTH, IMAGE_HEIGHT, observer);
 465 
 466     }
 467 
 468     static void setImageScalingHint(Graphics2D g2d, boolean enableImageScaling) {
 469         g2d.setRenderingHint(RenderingHints.KEY_RESOLUTION_VARIANT, enableImageScaling
 470                 ? RenderingHints.VALUE_RESOLUTION_VARIANT_ON
 471                 : RenderingHints.VALUE_RESOLUTION_VARIANT_OFF);
 472     }
 473 
 474     static void checkColor(int rgb, boolean isImageScaled) {
 475 
 476         if (!isImageScaled && COLOR_1X.getRGB() != rgb) {
 477             throw new RuntimeException("Wrong 1x color: " + new Color(rgb));
 478         }
 479 
 480         if (isImageScaled && COLOR_2X.getRGB() != rgb) {
 481             throw new RuntimeException("Wrong 2x color" + new Color(rgb));
 482         }
 483     }
 484 
 485     static void generateImages() throws Exception {
 486         if (!new File(IMAGE_NAME_1X).exists()) {
 487             generateImage(1);
 488         }
 489 
 490         if (!new File(IMAGE_NAME_2X).exists()) {
 491             generateImage(2);