获取正确的图像观察器以旋转图像 [英] getting the right image observer for rotating an image

查看:90
本文介绍了获取正确的图像观察器以旋转图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在画一个BufferedImage'bird',但是我想根据它掉落的角度旋转它.我有一个鸟对象,其中包含BufferedImage和一个将其旋转绘制的render()方法.

So Im drawing a BufferedImage 'bird' but I want to rotate it according to the angle that it is falling. I have a bird object which contains the BufferedImage and a render() method which draw it rotated.

public void render(Graphics2D g, ImageObserver io) {

    double theta = Math.tan((height - pastHeight) / .875);
    System.out.println(theta);
    Graphics2D g2 = (Graphics2D) bird.getGraphics();

    g2.drawImage(bird, 100, (int) height, null);

    g2.rotate(theta);

    g2.drawImage(bird, 100, (int) height, io);
}

我这样称呼

bird.render(g2, ???);

在我的jcomponent的paintcomponent方法中.

in my paintcomponent method in my jcomponent.

唯一的问题是我不知道该用什么作为我的ImageObserver ...我尝试传递我的JFrame和JComponent,但是当我这样做时该图像不再显示...我将为该图像传递什么出现在我的窗口中和/或我将如何实现这种旋转?

only problem is I dont know what to use as my ImageObserver... I've tried passing in my JFrame and my JComponent but the image no longer appears when I do that... what would I pass in for the image to appear in my window and/or how else would I achieve this rotation?

推荐答案

假设您正在扩展JComponent的对象中执行此操作,则应使用

Assuming that you are doing this in something that extends JComponent, you should use

bird.render(g2, this);

JComponent实现ImageObserver

图像消失的问题不是ImageObserver的问题,而是旋转发生的点,我认为这是Graphics上下文的左上角.

The problem with the image disappearing isn't an issue with the ImageObserver but the point around which the rotation is occurring, which I believe is the top/left corner of the Graphics context.

尝试使用Graphics2D#rotate(double, int, int),它将允许您指定旋转的原点(枢轴点).

Try using Graphics2D#rotate(double, int, int) which will allow you to specify the origin points of the rotation (pivot point).

别忘了重置翻译,因为翻译会影响您提供后的所有涂漆,并可能在以后的涂漆周期中重新使用.

Don't forget to reset your translations, as they will effect everything that is painted after your supply them and may be re-used in subsequent paint cycles.

使用简单示例进行了更新

这是一个基本示例,演示了rotate的不同用法.

This is a basic example that demonstrates the different uses of rotate.

首先,我只是使用Graphics#rotate(double)

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class RotateImage {

    public static void main(String[] args) {
        new RotateImage();
    }

    public RotateImage() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private BufferedImage img;
        private double angel = 0d;

        public TestPane() {
            try {
                img = ImageIO.read(...);
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            Timer timer = new Timer(40, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    angel += 5;
                    repaint();
                }
            });
            timer.start();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (img != null) {
                Graphics2D g2d = (Graphics2D) g.create();
                g2d.rotate(Math.toRadians(angel));
                int x = (getWidth() - img.getWidth()) / 2;
                int y = (getHeight() - img.getHeight()) / 2;
                g2d.drawImage(img, x, y, this);
                g2d.dispose();
            }
        }
    }

}

然后我将g2d.rotate(Math.toRadians(angel));替换为g2d.rotate(Math.toRadians(angel), getWidth() / 2, getHeight() / 2);,它使用组件的中心位置(或Graphics上下文)作为围绕其旋转的锚点...

Then I replaced g2d.rotate(Math.toRadians(angel)); with g2d.rotate(Math.toRadians(angel), getWidth() / 2, getHeight() / 2);, which used the center position of the component (or the Graphics context) as the anchor point around which the rotation would occur...

现在,由于您只想旋转图像,因此需要计算图像中心位置当前位置周围的锚点(假设您希望图像围绕中间位置旋转)

Now, because you only want to rotate your image, you're going to need to calculate the anchor point around the current position of the image's center position (assuming you want it to rotate around the middle)

这篇关于获取正确的图像观察器以旋转图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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