java 实现图片的缩放、翻转

       Java实现的图片翻转加缩放功能。用applet,实现了简单的动画效果,不过只能执行一遍,因为我巧用了repaint,哈哈!仅供参考:

     import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.image.BufferedImage; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.JApplet; public class ZoomImageTest extends JApplet { private static final long serialVersionUID = 2045383843321336797L; private int degree = 0; private int sW = 0; private int sH = 0; public ZoomImageTest() { } @Override public void paint(Graphics g) { sW += 2; sH += 2; degree += 2; BufferedImage img = null; try { BufferedImage sourceImage = ImageIO.read(this.getClass() .getResource("/login.jpg"));// 源图片 int w = sourceImage.getWidth(); int h = sourceImage.getHeight(); Graphics2D g2 = (img = new BufferedImage( sourceImage.getWidth() / 10, sourceImage.getHeight() / 10, sourceImage.getColorModel().getTransparency())) .createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g2.drawImage(sourceImage, 0, 0, sourceImage.getWidth() / 10 - sW, sourceImage.getHeight() / 10 - sH, 0, 0, w, h, null);// 长和宽缩小10倍 g2.dispose(); BufferedImage trasImage = new BufferedImage(img.getWidth(), img .getHeight(), BufferedImage.TYPE_INT_RGB); Graphics2D gg2 = (trasImage = new BufferedImage( img.getWidth() + 100, img.getHeight() + 100, img .getColorModel().getTransparency())) .createGraphics(); gg2.rotate(Math.toRadians(degree), img.getWidth() >> 1, img .getHeight() >> 1); // 旋转30度,一中心为旋转原点 gg2.drawImage(img, 40, 20, null); gg2.dispose(); g.drawImage(trasImage, 0, 0, null); g.dispose(); repaint(); } catch (IOException e) { e.printStackTrace(); } } }

你可能感兴趣的:(java)