简体   繁体   English

如何在Java中旋转imageIcon

[英]How to rotate an imageIcon in Java

I'm creating a domino game in java. 我正在用Java创建一个多米诺骨牌游戏。 I have the following code that loads, resizes and then display the domino image on the screen: 我有以下代码加载,调整大小,然后在屏幕上显示多米诺骨牌图像:

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);

What I want to do is rotate the image either 90 or -90 degrees. 我想做的是将图像旋转90度或-90度。 I've searched the internet but the examples I've found seems very complicated. 我已经搜索了互联网,但是发现的示例似乎非常复杂。

Any idea how I can rotate my image? 知道如何旋转图像吗?

Btw, if you think that this is not the correct way to display dominoes in a domino game then please let me know. 顺便说一句,如果您认为这不是在多米诺骨牌游戏中显示多米诺骨牌的正确方法,请告诉我。 I'me a java newbie. 我是Java新手。

Rotating an image is non-trival, even just 90 degrees requires a certain amount of work. 旋转图像不是很重要的事情,即使仅旋转90度也需要一定的工作量。

So, based on pretty much every other question about rotating images, I'd start with something like... 因此,基于关于旋转图像的几乎所有其他问题,我将从类似以下内容开始:

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;
}

While you're only rotate 90 degrees, this takes care of calculating the required size the new image needs in order to be able to paint the rotated image, at any angle. 当您仅旋转90度时,这会计算新图像所需的尺寸,以便能够以任何角度绘制旋转的图像。

It then simply makes use of AffineTransform to manipulate the origin point from which painting occurs - get use to this, you will do it a lot. 然后,它只是利用AffineTransform来操纵绘画的起点-充分利用这一点,您会做很多事情。

Then, I load the images, rotate them and display them... 然后,我加载图像,旋转它们并显示它们...

旋转的

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();
}

I prefer to use ImageIO to load images, because it throws an IOException when something goes wrong, rather then failing silently like ImageIcon . 我更喜欢使用ImageIO加载图像,因为当出现问题时它会抛出IOException ,而不是像ImageIcon那样静默失败。

You should also be embedding your resources within your application's context, this makes it easier to load them at runtime. 您还应该将资源嵌入到应用程序的上下文中,这样可以更轻松地在运行时加载资源。 Depending on IDE and how your project is set up, how you do this will change, but in "most" cases, you should be able to add the resource directly to your source directory (preferably in sub directory) and the IDE will make it available for you and package it when you export the project 取决于IDE和项目的设置,更改方式将有所变化,但是在“大多数”情况下,您应该能够将资源直接添加到源目录(最好在子目录中),并且IDE会使其可供您使用,并在导出项目时打包

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

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM