Java:旋转图像

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



aluckdog
浏览 410回答 3
3回答

万千封印

这就是您可以做到的。这段代码假设存在一个名为“ image”的缓冲图像(如您的评论所说)// The required drawing locationint drawLocationX = 300;int drawLocationY = 300;// Rotation informationdouble 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 locationsg2d.drawImage(op.filter(image, null), drawLocationX, drawLocationY, null);

米琪卡哇伊

一种简单的方法,而无需使用如此复杂的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);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java