在Java Swing上旋转JLabel或ImageIcon

首先,这是我使用秋千的第一个星期,如果我的问题太明显,那就对不起。另外,我需要使用标准Java库的解决方案,因为这是用于家庭作业,不允许使用奇怪的库。

我将JLabel与ImageIcon一起使用以在JFrame上显示图像。现在,我想将屏幕上的图像旋转到任意角度。我发现了有关Graphics2D的一些信息,但我找不到实现此目的的方法。

由于我发现的解决方案不起作用或不理解,因此我对旋转ImageIcon或JLabel的任何解决方案都感兴趣。由于我将图像放置在JLabel上的setBounds上,因此旋转JLabel是我认为更好的解决方案(通过这种方式,我也不必强制保存ImageIcon对象)。

感谢您的关注,并为我的英语不好对不起。

编辑…要在屏幕上显示图像,请执行以下操作:

JFrame frame = new JFrame("Something");

frame.setLayout(new FlowLayout()); //for example

JPanel panel = new JPanel();

panel.setLayout(null);

ImageIcon playerSprite = new ImageIcon("rute/to/file.png");

JLabel player = new JLabel(playerSprite);

panel.add(player);

player.setBounds(10,10,36,52); //for example

frame.getContentPane().add(panel);

frame.setVisible(true);

继续,如何旋转此IconImage或JLabel。如果您认为效果更好,可以使用其他方法来显示图像。如果解决方案是使用Graphics2D(如我所见),我将欣赏一个解决方案,该解决方案可以到达此类的对象,稍后再将旋转的图像返回到ImageIcon,因为当我尝试这样做时…

ImageIcon imagePlayer = new ImageIcon("img/stand.png");

Image image = imagePlayer.getImage();

Graphics2D g = (Graphics2D)image.getGraphics();

在执行时,答案是这样的…

Exception in thread "main" java.lang.UnsupportedOperationException: getGraphics() not valid for images created with createImage(producer)

第2版​​…现在,我正在使用此代码。图像旋转,但旧的未旋转图像仍保留在新图像下方的屏幕上。将名为stand.png的png图像放在同一目录中,您将看到它。

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import javax.imageio.ImageIO;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import java.lang.Math;

public class Test {

public static void main(String args[]) throws Exception {

try {

JFrame frame = new JFrame("Rotation Test");

frame.setBounds(10,10,1008,756);

BufferedImage bi = ImageIO.read(new File("stand.png"));

Graphics2D g = (Graphics2D)bi.getGraphics();

g.rotate(Math.toRadians(45),26,26);

g.drawImage(bi, 0, 0, null);

JLabel player = new JLabel(new ImageIcon(bi));

frame.getContentPane().add(player);

player.setBounds(0,0,100,100);

frame.setVisible(true);

} catch (IOException ex) {

System.out.println("Exception");

}

}

}

回答:

与其旋转组件本身,不如旋转组件的 内容

附录:在该示例中,直接在内存中RotatableImage.getImage()创建一个图像BufferedImage,但是您可以用来ImageIO.read()从其他位置获取图像。BufferedImage#createGraphics()如果要修改图像,则支持,但是您可能只想在旋转的图形上下文中作为的一部分绘制未修改的图像paintComponent()

附录:您正在用旋转的副本在图像上绘画;而是将图像绘制到旋转的图形上下文中:

import java.awt.Dimension;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.image.BufferedImage;

import java.io.File;

import javax.imageio.ImageIO;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class Test {

public static void main(String args[]) throws Exception {

JFrame frame = new JFrame("Rotation Test");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final BufferedImage bi = ImageIO.read(new File("img/stand.jpg"));

frame.add(new JPanel() {

@Override

public Dimension getPreferredSize() {

return new Dimension(bi.getWidth(), bi.getHeight());

}

@Override

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2 = (Graphics2D) g;

g2.rotate(Math.PI / 4, bi.getWidth() / 2, bi.getHeight() / 2);

g2.drawImage(bi, 0, 0, null);

}

});

frame.pack();

frame.setVisible(true);

}

}

以上是 在Java Swing上旋转JLabel或ImageIcon 的全部内容, 来源链接: utcz.com/qa/415829.html

回到顶部