如何在Java中旋转图像图标

如何在Java中旋转图像图标,java,image,swing,java-2d,affinetransform,Java,Image,Swing,Java 2d,Affinetransform,我正在用java创建一个domino游戏。我有以下代码,用于加载、调整大小,然后在屏幕上显示domino图像: 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 Imag

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

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度,但这需要计算新图像所需的大小,以便能够以任何角度绘制旋转图像

然后它简单地利用来操纵绘画发生的原点-利用这个,你会做很多

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

我更喜欢使用来加载图像,因为当出现问题时,它会抛出一个
IOException
,而不是像
ImageIcon
那样默默失败


您还应该将资源嵌入到应用程序的上下文中,这样可以更容易地在运行时加载它们。根据IDE和项目的设置方式,您的操作方式会有所改变,但在“大多数”情况下,您应该能够将资源直接添加到源目录(最好是在子目录中),IDE将使您可以使用该资源并在导出项目时对其进行打包,即使只有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度,但这需要计算新图像所需的大小,以便能够以任何角度绘制旋转图像

然后它简单地利用来操纵绘画发生的原点-利用这个,你会做很多

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

我更喜欢使用来加载图像,因为当出现问题时,它会抛出一个
IOException
,而不是像
ImageIcon
那样默默失败

您还应该将资源嵌入到应用程序的上下文中,这样可以更容易地在运行时加载它们。根据IDE和项目的设置方式,您执行此操作的方式将发生变化,但在“大多数”情况下,您应该能够将资源直接添加到源目录(最好是在子目录中),IDE将使您可以使用该资源,并在您从以下位置导出项目时对其进行打包:

解决方案来自:


“。但是我发现的例子似乎很复杂。”链接到你发现的三个最简单的例子。任何人试图回答这个问题都是毫无意义的,除非我们能够衡量出你认为“复杂”的东西。“如果你认为这不是在多米诺游戏中显示多米诺骨牌的正确方式,那么请让我知道”Unicode字符集包括多米诺骨牌。如果不使用它们,我倾向于在它们进入应用程序之前缩放和旋转瓷砖。“我搜索过互联网,但我发现的示例似乎非常复杂。”-为什么你认为我们提供的任何解决方案都不会“复杂”?旋转图像(并放大图像以适应)需要一些触发,这(对我来说)总是很复杂。
我在互联网上搜索过,但我找到的示例似乎非常复杂。
-如何使用。这是一个可重用的类,您只需在一行代码中选择旋转角度。如果原点处的图像数量已知,为什么不要求资源中的所有旋转图像?旋转图像是一项繁重/漫长的操作……”……但我发现的示例似乎非常复杂。”链接到您找到的三个最简单的示例。任何人试图回答这个问题都是毫无意义的,除非我们能够衡量出你认为“复杂”的东西。“如果你认为这不是在多米诺游戏中显示多米诺骨牌的正确方式,那么请让我知道”Unicode字符集包括多米诺骨牌。如果不使用它们,我倾向于在它们进入应用程序之前缩放和旋转瓷砖。“我搜索过互联网,但我发现的示例似乎非常复杂。”-为什么你认为我们提供的任何解决方案都不会“复杂”?旋转图像(并放大图像以适应)需要一些触发,这(对我来说)总是很复杂。
我在互联网上搜索过,但我找到的示例似乎非常复杂。
-如何使用。这是一个可重用的类,您只需在一行代码中选择旋转角度。如果原点处的图像数量已知,为什么不要求资源中的所有旋转图像?旋转图像是一项繁重(长)的操作…谢谢。我更改了一些小细节,但您的解决方案有效
AffineTransform
有一种旋转方法,因此您不必摆弄三角函数。Java有一个
AffineTransformOp
,所以你不必自己摆弄图像和图形,尽管有报告说你对质量失去了控制(使用
Graphics2D
你可以自己控制所有质量参数)。@MarkJeronimus Yes,我用了所有的方法——三角用来计算新图像的结果大小。当然,你可以在绘制过程中做这类事情,但是复合变换会变得很混乱。此外,我读到的问题是,如何拍摄现有图像并旋转它,创建一个新的图像one@MarkJeronimus所以,我在玩弄你的解决方案(因为找到更好的方法做某事总是很好的)我有一个
600x315
的图像,我正在旋转90度,这是在使用你的解决方案游戏时
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);