java - 如何在 Java 中旋转 imageIcon

标签 java image swing java-2d affinetransform

我正在用java创建一个多米诺骨牌游戏。我有以下代码,用于加载、调整大小,然后在屏幕上显示多米诺骨牌图像:

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 度。我在网上搜索过,但发现的例子似乎很复杂。

知道如何旋转我的图像吗?

顺便说一句,如果您认为这不是在多米诺骨牌游戏中显示多米诺骨牌的正确方式,请告诉我。我是一个java新手。

最佳答案

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

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

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操纵绘画发生的原点 - 习惯这个,你会做很多。

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

Rotated

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 会将其添加到源目录中。可供您使用并在导出项目时打包

关于java - 如何在 Java 中旋转 imageIcon,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50883802/

相关文章:

java - 创建包后在 eclipse IDE 中创建包信息 java 的快捷方式。

javascript - Jquery 背景图像淡入淡出 - 有更好的方法吗?

python - OLED 显示屏调整图像大小困难

java - JFrame 背景图像顶部出现奇怪的白色条纹

java - 为什么我的照片不再显示了

Java Swing,看不到错误

javascript - Headless Chrome 和 document.location.href 更改

java - 错误: cannot find symbol

java - 尝试修改游戏,以便程序保留未接受的答案列表并在最后打印它们。 (java)

JavaScript/JQuery 在 HTML 中查找图像链接并转换为实际图像