旋转一个Image对象 [英] Rotating an Image object

查看:295
本文介绍了旋转一个Image对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要旋转 Image 的方法 getImage(),将它存储在一个新变量中,然后返回新的 Image 。这是我的尝试,图像看起来是空的或者其他的东西。它只是不显示在屏幕上:

  public Image getImage(){
buffImage.createGraphics()。旋转(方向);
返回buffImage;
}

当我取出 buffImage.createGraphics() .rotate(方向); 图像在屏幕上画得很好,没有问题,但当然不会旋转。

解决方案

因此,根据这个答案中的示例,你应该能够设计一个旋转方法,它可以旋转一个给定度数的源图像,例如...

pre $ //确保你实际加载了一些图像并将它分配给这个
//变量,否则你将有一个NullPointerException异常为
//处理
私有BufferedImage源;

public ImageByBy(double degrees){

//原始图像的大小
int w = source.getWidth();
int h = source.getHeight();
//以弧度表示的旋转角度
double rads = Math.toRadians(度);
//一些很好的数学表明我不知道我在说什么
//好吧,这会计算图像需要的空间量
//订单不会被剪裁当它旋转
double sin = Math.abs(Math.sin(rads));
double cos = Math.abs(Math.cos(rads));
int newWidth =(int)Math.floor(w * cos + h * sin);
int newHeight =(int)Math.floor(h * cos + w * sin);

//新的图像,原始图像可以被绘制
BufferedImage rotate = new BufferedImage(newWidth,newHeight,BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = rotated.createGraphics();
//将用于实际旋转图像的转换
//翻译,实际上确保图像位于
上//图像的可视区域
AffineTransform at = new AffineTransform(); ((newWidth - w)/ 2,(newHeight - h)/ 2);
at.translate

//我们围绕图像的中心旋转...
int x = w / 2;
int y = h / 2;
at.rotate(rads,x,y);
g2d.setTransform(at);
//我们将原始图像绘制到新图像上
g2d.drawImage(source,0,0,null);
g2d.dispose();

返回旋转;
}


I have a method getImage() that needs to rotate an Image, store it in a new variable, then return that new Image. Here is my attempt, the image appears to be empty or something. It just does not show up on the screen:

public Image getImage() {
    buffImage.createGraphics().rotate(direction);
    return buffImage;
}

When I take out the buffImage.createGraphics().rotate(direction); the image draws on the screen just fine with no problems but of course, not rotated.

解决方案

So, based on the example in this answer, you should be able to devise a rotation method which can rotate a source image by a given number of degrees, for example...

  // Make sure you actually load some image and assign it to this
  // variable, otherwise you will have a NullPointerException to 
  // deal with
  private BufferedImage source;

  public Image rotateBy(double degrees) {

    // The size of the original image
    int w = source.getWidth();
    int h = source.getHeight();
    // The angel of the rotation in radians
    double rads = Math.toRadians(degrees);
    // Some nice math which demonstrates I have no idea what I'm talking about
    // Okay, this calculates the amount of space the image will need in
    // order not be clipped when it's rotated
    double sin = Math.abs(Math.sin(rads));
    double cos = Math.abs(Math.cos(rads));
    int newWidth = (int) Math.floor(w * cos + h * sin);
    int newHeight = (int) Math.floor(h * cos + w * sin);

    // A new image, into which the original can be painted
    BufferedImage rotated = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = rotated.createGraphics();
    // The transformation which will be used to actually rotate the image
    // The translation, actually makes sure that the image is positioned onto
    // the viewable area of the image
    AffineTransform at = new AffineTransform();
    at.translate((newWidth - w) / 2, (newHeight - h) / 2);

    // And we rotate about the center of the image...
    int x = w / 2;
    int y = h / 2;
    at.rotate(rads, x, y);
    g2d.setTransform(at);
    // And we paint the original image onto the new image
    g2d.drawImage(source, 0, 0, null);
    g2d.dispose();

    return rotated;
  }

这篇关于旋转一个Image对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆