如何在Java中旋转imageIcon [英] How to rotate an imageIcon in Java

查看:636
本文介绍了如何在Java中旋转imageIcon的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用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){
//根据旋转角度计算图像的新大小
double radians = Math.toRadians(度);
double sin = Math.abs(Math.sin(弧度));
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);

//创建一个新图像
BufferedImage rotate = new BufferedImage(newWidth,newHeight,BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = rotate.createGraphics();
//计算图像旋转的锚点
int x =(newWidth - image.getWidth())/ 2;
int y =(newHeight - image.getHeight())/ 2;
//在锚点周围转换原点
AffineTransform at = new AffineTransform();
at.setToRotation(弧度,x +(image.getWidth()/ 2),y +(image.getHeight()/ 2));
at.translate(x,y);
g2d.setTransform(at);
//绘制原始图像
g2d.drawImage(image,0,0,null);
g2d.dispose();
返回旋转;
}

当你只旋转90度时,这需要计算所需的为了能够以任何角度绘制旋转图像,需要调整新图像的大小。



然后只使用

 试试{
BufferedImage original = ImageIO.read(getClass()。getResource(domino.jpg));
BufferedImage rotate90 = rotate(original,90.0d);
BufferedImage rotateMinus90 = rotate(original,-90.0d);

JPanel panel = new JPanel();
panel.add(new JLabel(new ImageIcon(original)));
panel.add(new JLabel(new ImageIcon(rotating90)));
panel.add(new JLabel(new ImageIcon(rotatingMinus90)));

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

我更喜欢使用 ImageIO 加载图片,因为它会抛出 IOException 当出现问题时,而不是静默失败,如 ImageIcon



您还应该在应用程序的上下文中嵌入资源,这样可以更轻松地在运行时加载它们。根据IDE以及项目的设置方式,如何更改,但在大多数情况下,您应该能够将资源直接添加到源目录(最好是在子目录中),IDE将使其成为可能可用,并在导出项目时打包它


I'm creating a domino game in 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. 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.

解决方案

Rotating an image is non-trival, even just 90 degrees requires a certain amount of work.

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.

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.

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.

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

这篇关于如何在Java中旋转imageIcon的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆