RenderedImageIcon implements Icon { public void paintIcon(Component c, Graphics g, int x, int y) { if (!(g instanceof Graphics2D)) { throw new IllegalStateException("paintIcon() only works on Graphics2D."); } Graphics2D g2d = (Graphics2D) g; AffineTransform scale = AffineTransform.getScaleInstance((double) (width) / renderedImage.getWidth(), (double) (height) / renderedImage.getHeight()); AffineTransform translate = AffineTransform.getTranslateInstance(x, y); translate.concatenate(scale); g2d.drawRenderedImage(renderedImage, translate); } RenderedImageIcon(RenderedImage renderedImage, int width, int height); void paintIcon(Component c, Graphics g, int x, int y); int getIconWidth(); int getIconHeight();  }
@Test public void testPaint() throws IOException { BufferedImage img = ImageIO.read(getClass().getResourceAsStream( "/org/geomajas/plugin/rasterizing/images/testIcon.png")); RenderedImageIcon icon = new RenderedImageIcon(img, 24, 24); BufferedImage copy = new BufferedImage(24, 24, BufferedImage.TYPE_4BYTE_ABGR); icon.paintIcon(null, copy.getGraphics(), 0, 0); copy.getGraphics().dispose(); Raster r1 = img.getData(); Raster r2 = copy.getData(); for (int i = 0; i < 24; i++) { for (int j = 0; j < 24; j++) { int[] p1 = r1.getPixel(i, j, (int[]) null); int[] p2 = r2.getPixel(i, j, (int[]) null); Assert.assertArrayEquals(p2, p1); } } }