首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Java中旋转imageIcon

如何在Java中旋转imageIcon
EN

Stack Overflow用户
提问于 2018-06-16 07:32:25
回答 2查看 2.4K关注 0票数 1

我正在用java创建一个domino游戏。我使用以下代码加载、调整大小,然后在屏幕上显示domino图像:

代码语言:javascript
复制
ImageIcon imageIcon = new ImageIcon("images\\4-4.png");
Image image = imageIcon.getImage(); 
Image newimg = image.getScaledInstance(60, 120,  java.awt.Image.SCALE_SMOOTH);  
imageIcon = new ImageIcon(newimg);

JLabel img = new JLabel(imageIcon);
img.setBounds(100, 100, 60, 120);
getContentPane().add(img);

我想要做的是将图像旋转90度或-90度。我在互联网上搜索过,但我发现的例子似乎非常复杂。

你知道我该怎么旋转我的图像吗?

顺便说一句,如果你认为这不是在骨牌游戏中显示骨牌的正确方式,请让我知道。我是个爪哇新手。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-16 08:46:22

旋转图像不是一件简单的事情,即使是90度也需要一定的工作量。

因此,基于几乎所有其他关于旋转图像的问题,我会从以下内容开始:

代码语言:javascript
复制
public BufferedImage rotate(BufferedImage image, Double degrees) {
    // Calculate the new size of the image based on the angle of rotaion
    double radians = Math.toRadians(degrees);
    double sin = Math.abs(Math.sin(radians));
    double cos = Math.abs(Math.cos(radians));
    int newWidth = (int) Math.round(image.getWidth() * cos + image.getHeight() * sin);
    int newHeight = (int) Math.round(image.getWidth() * sin + image.getHeight() * cos);

    // Create a new image
    BufferedImage rotate = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = rotate.createGraphics();
    // Calculate the "anchor" point around which the image will be rotated
    int x = (newWidth - image.getWidth()) / 2;
    int y = (newHeight - image.getHeight()) / 2;
    // Transform the origin point around the anchor point
    AffineTransform at = new AffineTransform();
    at.setToRotation(radians, x + (image.getWidth() / 2), y + (image.getHeight() / 2));
    at.translate(x, y);
    g2d.setTransform(at);
    // Paint the originl image
    g2d.drawImage(image, 0, 0, null);
    g2d.dispose();
    return rotate;
}

虽然您只需要旋转90度,但这需要计算新图像所需的大小,以便能够以任何角度绘制旋转后的图像。

然后,它简单地利用AffineTransform来操纵绘画发生的原点-使用它,你会做很多事情。

然后,我加载图像,旋转它们并显示它们...

代码语言:javascript
复制
try {
    BufferedImage original = ImageIO.read(getClass().getResource("domino.jpg"));
    BufferedImage rotated90 = rotate(original, 90.0d);
    BufferedImage rotatedMinus90 = rotate(original, -90.0d);

    JPanel panel = new JPanel();
    panel.add(new JLabel(new ImageIcon(original)));
    panel.add(new JLabel(new ImageIcon(rotated90)));
    panel.add(new JLabel(new ImageIcon(rotatedMinus90)));

    JOptionPane.showMessageDialog(null, panel, null, JOptionPane.PLAIN_MESSAGE, null);
} catch (IOException ex) {
    ex.printStackTrace();
}

我更喜欢使用ImageIO来加载图像,因为它会在出错时抛出IOException,而不是像ImageIcon那样静默地失败。

您还应该将资源嵌入到应用程序的上下文中,这使得在运行时加载它们变得更容易。根据IDE和项目的设置方式,执行此操作的方式会有所不同,但在大多数情况下,您应该能够将资源直接添加到源目录(最好在子目录中),并且IDE将使其可用,并在导出项目时将其打包

票数 5
EN

Stack Overflow用户

发布于 2018-06-18 23:26:19

解决方案来自:http://www.java2s.com/Code/Java/Advanced-Graphics/RotatingaBufferedImage.htm

代码语言:javascript
复制
AffineTransform tx = new AffineTransform();
tx.rotate(0.5, bufferedImage.getWidth() / 2, bufferedImage.getHeight() / 2);

AffineTransformOp op = new AffineTransformOp(tx,
    AffineTransformOp.TYPE_BILINEAR);
bufferedImage = op.filter(bufferedImage, null);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50883802

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档