本文介绍了Java从屏幕上的任何X/Y位置向点旋转图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个船对象,我希望能够朝着我在屏幕上单击的任何特定点旋转(参见船和正方形的图片作为点).我可以让船面向大方向,但是当它旋转船本身时它有这种奇怪的行为;图像围绕圆形轴移动,而不是从图像本身的中心点旋转.

I have a ship object that I want to be able to rotate towards any certain point I click on the screen (See pic of ship and sqaures as points). I can get the ship to face the general direction, but it has this strange behavior of as it rotates the ship itself; the image moves around an circular axis, instead of rotating from the center point of the image itself.

谁能给我一个公式来计算从图像的中心 xy 点到目标正方形的角度(鼠标点击)?

Can someone please, give me the formula to get the angle from the center xy point of image to the destination sqaure(mouse click)?

推荐答案

您需要围绕其中心点旋转图像,这意味着您需要在绘制图像之前将 Graphics 对象平移到图像的中心.

You need to rotate the image about its center point, which means you need to translate the Graphics object to the center of your image before you paint the image.

这个例子围绕一个固定点旋转:

This example rotates about a fixed point:

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.*;

public class Rotation2 extends JPanel
{
    BufferedImage image;
    int degrees;
    int point = 250;

    public Rotation2(BufferedImage image)
    {
        this.image = image;
        setDegrees( 0 );
        setPreferredSize( new Dimension(600, 600) );
    }

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        Graphics2D g2 = (Graphics2D)g.create();

        double radians = Math.toRadians( degrees );
        g2.translate(point, point);
        g2.rotate(radians);
        g2.translate(-image.getWidth(this) / 2, -image.getHeight(this) / 2);
        g2.drawImage(image, 0, 0, null);

        g2.dispose();

        g.setColor(Color.RED);
        g.fillOval(point - 5, point - 5, 10, 10);
    }

    public void setDegrees(int degrees)
    {
        this.degrees = degrees;
        repaint();
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                try
                {
                    String path = "mong.jpg";
//                  String path = "dukewavered.gif";
                    ClassLoader cl = Rotation2.class.getClassLoader();
                    BufferedImage bi = ImageIO.read(cl.getResourceAsStream(path));
                    final Rotation2 r = new Rotation2(bi);

                    final JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 360, 0);
                    slider.addChangeListener(new ChangeListener()
                    {
                        public void stateChanged(ChangeEvent e)
                        {
                            int value = slider.getValue();
                            r.setDegrees( value );
                        }
                    });

                    JFrame f = new JFrame();
                    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    f.add(new JScrollPane(r));
                    f.add(slider, BorderLayout.SOUTH);
                    f.pack();
                    f.setLocationRelativeTo(null);
                    f.setVisible(true);
                }
                catch(IOException e)
                {
                    System.out.println(e);
                }
            }
        });
    }
}

这篇关于Java从屏幕上的任何X/Y位置向点旋转图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 21:24
查看更多