大家好,我想实现一个旋转的方法,比如
imageRotate(Image img1,double angle){
............................
}
传入一个Image对象,,,和要旋转的angle角度,,,然后就能返回一个旋转之后的Image对象,,,,谢谢了!!!!

解决方案 »

  1.   

    <html>  <head>  <meta http-equiv="Content-Type" content="text/html; charset=gb2312">  <style>  body img{border:3 gold ridge}  </style>  //给图片加上边框的CS代码  <title>用Javascript使网页图片产生旋转效果</title>  <script language="javascript">  var i=0;  function playImg()  {  image.style.filter="progid:DXImageTransform.Microsoft.BasicImage( Rotation="+i+")";  i++;  if (i>4)  {i=1};  mytimeout=setTimeout("playImg()",1500);  }  </script>  //以上是使图片实现四个方向转换的代码  </head>  <body bgcolor="#FFFFFF" text="#000000" onload="playImg()">  //修改body为加载就显示效果  <img src="/img/200406301.jpg" width="120" height="120" name="image">  </body>  </body>  </html>
      

  2.   


     private Image rotate90(Image original, boolean clockwise, ImageObserver observer) {  //加个参数double angle
            int width = original.getWidth(observer);
            int height = original.getHeight(observer);
     
            BufferedImage bufferedImage = new BufferedImage(height, 
                    width, BufferedImage.TYPE_INT_RGB);
            
            Graphics2D g2 = bufferedImage.createGraphics();
            if(clockwise) {
                AffineTransform aff = AffineTransform.getRotateInstance(Math.toRadians(90), 0, 0);  //90替换成参数double angle
                g2.setTransform(aff);
                g2.drawImage(original, 0, -height , observer);
            }
            else {
                AffineTransform aff = AffineTransform.getRotateInstance(Math.toRadians(-90), 0, 0);
                g2.setTransform(aff);
                g2.drawImage(original, -width, 0, observer);
            }
            
            return  bufferedImage;
        }
      

  3.   

    这个应该更好一点,呵呵,空白区域透明
        public Image imageRotate(Image image, double angle)
        {
            GraphicsConfiguration config = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
            ImageIcon tempImage = new ImageIcon(image);
            double radians = Math.toRadians(angle % 360);
            int oldWidth = tempImage.getIconWidth();
            int oldHeight = tempImage.getIconHeight();
            int newWidth = (int)(Math.abs(Math.cos(radians) * oldWidth) + Math.abs(Math.sin(radians) * oldHeight));
            int newHeight = (int)(Math.abs(Math.sin(radians) * oldWidth) + Math.abs(Math.cos(radians) * oldHeight));
            int centerX = newWidth / 2;
            int centerY = newHeight / 2;
            BufferedImage ret = config.createCompatibleImage(newWidth, newHeight, Transparency.BITMASK);
            Graphics2D g2d = (Graphics2D)ret.getGraphics();
            g2d.rotate(radians, centerX, centerY);
            g2d.drawImage(image, (newWidth - oldWidth) / 2, (newHeight - oldHeight) / 2, null);        return ret;
        }