Java:将图像旋转到鼠标位置? [英] Java: Rotate image towards mouse position?

查看:91
本文介绍了Java:将图像旋转到鼠标位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个人:

让我们称他为鲍勃。

我想让他转向我的鼠标位置。我已经想通过在Bob和鼠标之间画一条线并找到这条线的角度,我可以找出角度是什么,Bob需要面对以便指向鼠标。但是我仍然不知道如何完成这项任务。

I want to make him rotate towards my mouse position. I have already figured out that by drawing a line between Bob and the mouse and by finding the angle of this line, i can find out what the angle is, that Bob needs to face in order to 'point' towards the mouse. However I still don't know how to accomplish this task.

提前致谢!

推荐答案

您不应该将图像包含在 JLabel 中。它根本没有提供太大的灵活性。定位可能是奇数,您无法绘制旋转的图像。 (你只能创建一个旋转的图像并将其放入标签中,但这样效率会很低,而且尺寸和位置问题会让你发疯。)

You should not include the image in a JLabel. It simply does not offer much flexibility. The positioning may be odd, and you can't draw the image rotated. (You could only create a rotated image and put this into the label, but this would be inefficient, and the sizing and position issues would drive you mad).

相反,你应该在 JPanel 的重写 paintComponent 方法中手动绘制图像。还有一些细节需要考虑。例如,图像应该具有某个位置,而位置应该可以引用图像的 center - 即,指出它将被旋转。

Instead, you should paint your image manually, in the overridden paintComponent method of a JPanel. There are still some details to consider. For example, the image should probably have a certain position, and the position should probably refer to the center of the image - namely, to the point that it will be rotated about.

然而,当您计算出图像中心和鼠标位置之间的直线角度时,您可以简单地绘制图像。所需的方向,通过

However, when you have computed the angle of the line between the image center and the mouse position, you can simply paint the image with the desired orientation, by


  • 移动图像以使其中心位于原点

  • 旋转图像

  • 移动图像以使其中心位于所需位置

这是 MCVE

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.geom.AffineTransform;
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.SwingUtilities;

public class ImageFollowingMouseTest
{
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI()
    {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(new ImageFollowingMousePanel());
        f.setSize(400,400);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

}

class ImageFollowingMousePanel extends JPanel implements MouseMotionListener
{
    private final BufferedImage image;
    private Point imagePosition = new Point(150,150);
    private double imageAngleRad = 0;

    public ImageFollowingMousePanel()
    {
        BufferedImage i = null;
        try
        {
            i = ImageIO.read(new File("6Wu0b.png"));
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        image = i;
        addMouseMotionListener(this);
    }

    protected void paintComponent(Graphics gr) 
    {
        super.paintComponent(gr);
        Graphics2D g = (Graphics2D)gr;
        g.setRenderingHint(
            RenderingHints.KEY_RENDERING, 
            RenderingHints.VALUE_RENDER_QUALITY);

        int cx = image.getWidth() / 2;
        int cy = image.getHeight() / 2;
        AffineTransform oldAT = g.getTransform();
        g.translate(cx+imagePosition.x, cy+imagePosition.y);
        g.rotate(imageAngleRad);
        g.translate(-cx, -cy);
        g.drawImage(image, 0, 0, null);
        g.setTransform(oldAT);

    }

    @Override
    public void mouseDragged(MouseEvent e)
    {
    }

    @Override
    public void mouseMoved(MouseEvent e)
    {
        double dx = e.getX() - imagePosition.getX();
        double dy = e.getY() - imagePosition.getY();
        imageAngleRad = Math.atan2(dy, dx);
        repaint();
    }


}

这篇关于Java:将图像旋转到鼠标位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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