java实现图片水平和垂直翻转效果
最近在做小型游戏,遇到了要翻转图片的苦恼,经过我一下午的研究,终于发现了一种好用的方法。
部分代码来源于别人,我在这个基础上修改了下,变得更好用了,之前的别人Image输入都是BufferedImage,我改成了Image,也就是加了一个转换。
大家如果看不懂代码没关系,会用就行了,我会展示一下怎么用的。
ImageRotate类
packagemypackage; importjavax.imageio.ImageIO; importjava.awt.*; importjava.awt.image.BufferedImage; importjava.io.File; /** *AuthorMythos_Q * *Time2011-07-20 * *Description图片旋转、翻转处理 */ publicclassImgRotate { staticintUp_Down_Reverse=0; staticintLeft_Right_Reverse=1; /** *旋转图片为指定角度 * *@parambufferedimage *目标图像 *@paramdegree *旋转角度 *@return */ publicstaticBufferedImagerotateImage(finalBufferedImagebufferedimage,finalintdegree){ intw=bufferedimage.getWidth(); inth=bufferedimage.getHeight(); inttype=bufferedimage.getColorModel().getTransparency(); BufferedImageimg; Graphics2Dgraphics2d; (graphics2d=(img=newBufferedImage(h,w,type)).createGraphics()).setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2d.rotate(Math.toRadians(degree),w/2,h/2+(w>h?(w-h)/2:(h-w)/2)); graphics2d.drawImage(bufferedimage,0,0,null); graphics2d.dispose(); returnimg; } /** *旋转图片为指定角度 * *@paramfile *目标图像 *@paramdegree *旋转角度(90,180,270) *@return */ publicstaticFilerotateImage(Filefile,intdegree)throwsException{ if(degree==90)returnrotateImage90(file); if(degree==180)returnrotateImage180(file); if(degree==270)returnrotateImage270(file); returnnull; } publicstaticImagerotateImage(ImageImage,intdegree) { if(degree==90) returnrotateImage90(Image); if(degree==180) returnrotateImage180(Image); if(degree==270) returnrotateImage270(Image); returnnull; } privatestaticImagerotateImage90(Imageimage) { BufferedImagebufferedimage=ImageToBufferedImage(image); intw=bufferedimage.getWidth(); inth=bufferedimage.getHeight(); inttype=bufferedimage.getColorModel().getTransparency(); BufferedImageimg; Graphics2Dgraphics2d; (graphics2d= (img=newBufferedImage(h,w,type)).createGraphics() ).setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2d.rotate(Math.toRadians(270),w/2,h/2+(w-h)/2); graphics2d.drawImage(bufferedimage,0,0,null); graphics2d.dispose(); returnBufferedImageToImage(img); } //左转90度 publicstaticFilerotateImage90(Filefile)throwsException { BufferedImagebufferedimage=ImageIO.read(file); intw=bufferedimage.getWidth(); inth=bufferedimage.getHeight(); inttype=bufferedimage.getColorModel().getTransparency(); BufferedImageimg; Graphics2Dgraphics2d; (graphics2d= (img=newBufferedImage(h,w,type)).createGraphics() ).setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2d.rotate(Math.toRadians(270),w/2,h/2+(w-h)/2); graphics2d.drawImage(bufferedimage,0,0,null); graphics2d.dispose(); ImageIO.write(img,"jpg",file); returnfile; } //右转90度 publicstaticFilerotateImage270(Filefile)throwsException { BufferedImagebufferedimage=ImageIO.read(file); intw=bufferedimage.getWidth(); inth=bufferedimage.getHeight(); inttype=bufferedimage.getColorModel().getTransparency(); BufferedImageimg; Graphics2Dgraphics2d; (graphics2d=(img=newBufferedImage(h,w,type)).createGraphics()).setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2d.rotate(Math.toRadians(90),w/2-(w-h)/2,h/2); graphics2d.drawImage(bufferedimage,0,0,null); graphics2d.dispose(); ImageIO.write(img,"jpg",file); returnfile; } publicstaticImagerotateImage270(Imageimage) { BufferedImagebufferedimage=ImageToBufferedImage(image); intw=bufferedimage.getWidth(); inth=bufferedimage.getHeight(); inttype=bufferedimage.getColorModel().getTransparency(); BufferedImageimg; Graphics2Dgraphics2d; (graphics2d=(img=newBufferedImage(h,w,type)).createGraphics()).setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2d.rotate(Math.toRadians(90),w/2-(w-h)/2,h/2); graphics2d.drawImage(bufferedimage,0,0,null); graphics2d.dispose(); returnBufferedImageToImage(img); } //对转 publicstaticFilerotateImage180(Filefile)throwsException { BufferedImagebufferedimage=ImageIO.read(file); intw=bufferedimage.getWidth(); inth=bufferedimage.getHeight(); inttype=bufferedimage.getColorModel().getTransparency(); BufferedImageimg; Graphics2Dgraphics2d; (graphics2d=(img=newBufferedImage(w,h,type)).createGraphics()).setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2d.rotate(Math.toRadians(180),w/2,h/2); graphics2d.drawImage(bufferedimage,0,0,null); graphics2d.dispose(); ImageIO.write(img,"jpg",file); returnfile; } publicstaticImagerotateImage180(Imageimage) { BufferedImagebufferedimage=ImageToBufferedImage(image); intw=bufferedimage.getWidth(); inth=bufferedimage.getHeight(); inttype=bufferedimage.getColorModel().getTransparency(); BufferedImageimg; Graphics2Dgraphics2d; (graphics2d=(img=newBufferedImage(w,h,type)).createGraphics()).setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2d.rotate(Math.toRadians(180),w/2,h/2); graphics2d.drawImage(bufferedimage,0,0,null); graphics2d.dispose(); returnBufferedImageToImage(img); } /*** *图片镜像处理 *@paramfile *@paramFX0为上下反转1为左右反转 *@return */ publicstaticvoidimageMisro(Filefile,intFX) { try { BufferedImagebufferedimage=ImageIO.read(file); intw=bufferedimage.getWidth(); inth=bufferedimage.getHeight(); int[][]datas=newint[w][h]; for(inti=0;i下面展示用法:
packagemypackage; importjava.awt.*; importjava.awt.image.*; importjava.net.URL; importjavax.swing.*; publicclassTestextendsJPanel { JFrameframe; Imageimage=newImageIcon("hand2.jpg").getImage(); publicvoidpaint(Graphicsg) { g.drawImage(image,0,0,null);//之前的图片 Imagem=ImgRotate.imageMisro(image,ImgRotate.Left_Right_Reverse); g.drawImage(m,200,200,null);//水平翻转的图片 Imagemm=ImgRotate.rotateImage(m,90);//这里只能填90,180,270 g.drawImage(mm,250,0,null); } publicTest() { frame=newJFrame(); frame.add(this); frame.setSize(500,500); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } publicstaticvoidmain(String[]args) { newTest(); } }以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。