Java examples for 2D Graphics:BufferedImage Paint
Draws the given BufferedImage to the canvas, centered, wholly contained within the bounds defined by the destination rectangle, and with preserved aspect ratio.
/*//w ww .ja v a2 s . co m * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ //package com.book2s; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Rectangle; import java.awt.image.BufferedImage; public class Main { /** * Draws the given {@link BufferedImage} to the canvas, centered, wholly contained within the * bounds defined by the destination rectangle, and with preserved aspect ratio. * * @param g The destination canvas. * @param source The source image. * @param dstRect The destination rectangle in the destination canvas into which to draw the * image. */ public static void drawCenterInside(Graphics2D g, BufferedImage source, Rectangle dstRect) { final int srcWidth = source.getWidth(); final int srcHeight = source.getHeight(); if (srcWidth * 1.0 / srcHeight > dstRect.width * 1.0 / dstRect.height) { final int scaledWidth = Math.max(1, dstRect.width); final int scaledHeight = Math.max(1, dstRect.width * srcHeight / srcWidth); Image scaledImage = scaledImage(source, scaledWidth, scaledHeight); g.drawImage(scaledImage, dstRect.x, dstRect.y + (dstRect.height - scaledHeight) / 2, dstRect.x + dstRect.width, dstRect.y + (dstRect.height - scaledHeight) / 2 + scaledHeight, 0, 0, 0 + scaledWidth, 0 + scaledHeight, null); } else { final int scaledWidth = Math.max(1, dstRect.height * srcWidth / srcHeight); final int scaledHeight = Math.max(1, dstRect.height); Image scaledImage = scaledImage(source, scaledWidth, scaledHeight); g.drawImage(scaledImage, dstRect.x + (dstRect.width - scaledWidth) / 2, dstRect.y, dstRect.x + (dstRect.width - scaledWidth) / 2 + scaledWidth, dstRect.y + dstRect.height, 0, 0, 0 + scaledWidth, 0 + scaledHeight, null); } } /** * Smoothly scales the given {@link BufferedImage} to the given width and height using the * {@link Image#SCALE_SMOOTH} algorithm (generally bicubic resampling or bilinear filtering). * * @param source The source image. * @param width The destination width to scale to. * @param height The destination height to scale to. * @return A new, scaled image. */ public static BufferedImage scaledImage(BufferedImage source, int width, int height) { Image scaledImage = source.getScaledInstance(width, height, Image.SCALE_SMOOTH); BufferedImage scaledBufImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics g = scaledBufImage.createGraphics(); g.drawImage(scaledImage, 0, 0, null); g.dispose(); return scaledBufImage; } }