为了账号安全,请及时绑定邮箱和手机立即绑定

Java:旋转图像

Java:旋转图像

aluckdog 2019-11-04 09:30:59
我需要能够单独旋转图像(在Java中)。到目前为止,我发现的唯一东西是g2d.drawImage(image,affinetransform,ImageObserver)。不幸的是,我需要在特定点绘制图像,并且没有一种方法带有参数1.分别旋转图像和2.允许我设置x和y。任何帮助表示赞赏
查看完整描述

3 回答

?
万千封印

TA贡献1891条经验 获得超3个赞

这就是您可以做到的。这段代码假设存在一个名为“ image”的缓冲图像(如您的评论所说)


// The required drawing location

int drawLocationX = 300;

int drawLocationY = 300;


// Rotation information


double rotationRequired = Math.toRadians (45);

double locationX = image.getWidth() / 2;

double locationY = image.getHeight() / 2;

AffineTransform tx = AffineTransform.getRotateInstance(rotationRequired, locationX, locationY);

AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);


// Drawing the rotated image at the required drawing locations

g2d.drawImage(op.filter(image, null), drawLocationX, drawLocationY, null);


查看完整回答
反对 回复 2019-11-04
?
米琪卡哇伊

TA贡献1998条经验 获得超6个赞

一种简单的方法,而无需使用如此复杂的draw语句:


    //Make a backup so that we can reset our graphics object after using it.

    AffineTransform backup = g2d.getTransform();

    //rx is the x coordinate for rotation, ry is the y coordinate for rotation, and angle

    //is the angle to rotate the image. If you want to rotate around the center of an image,

    //use the image's center x and y coordinates for rx and ry.

    AffineTransform a = AffineTransform.getRotateInstance(angle, rx, ry);

    //Set our Graphics2D object to the transform

    g2d.setTransform(a);

    //Draw our image like normal

    g2d.drawImage(image, x, y, null);

    //Reset our graphics object so we can draw with it again.

    g2d.setTransform(backup);


查看完整回答
反对 回复 2019-11-04
  • 3 回答
  • 0 关注
  • 409 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信