import java.awt.geom.*; static void paintFitBackground(Component c, Graphics _g, BufferedImage image) { Graphics2D g = cast _g; RenderingHints hints = g.getRenderingHints(); antiAliasOn(g); int w = c.getWidth(), h = c.getHeight(); g.setColor(c.getBackground()); g.fillRect(0, 0, w, h); if (image != null) { int iw = image.getWidth(), ih = image.getHeight(); double imgScale = max(doubleRatio(h, ih), doubleRatio(w, iw)); AffineTransform before = g.getTransform(); double y1 = h-ih*imgScale; g.scale(imgScale, imgScale); g.translate(iround((w-iw*imgScale)/2), iround((h-ih*imgScale)/2)); g.drawImage(image, 0, 0, c); g.setTransform(before); } g.setRenderingHints(hints); }