7

首先,这是我使用swing的第一周,如果我的问题太明显,请见谅。另外,我需要使用标准 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");
        }
    }
}
4

2 回答 2

11

考虑旋转组件的内容,而不是旋转组件本身。此示例JPanel.

附录:在示例 中直接在内存中RotatableImage.getImage()创建了一个,但您可以使用从其他地方获取图像。如果您想修改图像,则支持,但您可能只想在旋转的图形上下文中绘制未修改的图像作为.BufferedImageImageIO.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);
    }
}
于 2010-11-26T21:44:11.223 回答
3

由于我找到的解决方案不起作用或我不理解它们

好吧,如果你不理解它们,那么你应该发布你的 SSCCE (http://sscce.org) 来展示你的测试代码。然后也许有人能够解释代码是如何工作的。

我怀疑您是否能够理解我们可能发布的任何新代码,因为旋转图像或图标的概念都是相同的。

因为我在 JLabel 上定位图像做 setBounds

为什么要使用 setBounds()。您应该使用布局管理器,这样您就不必担心这一点。

编辑:

也许您可以使用Rotated Icon

于 2010-11-26T18:48:41.987 回答