java - 获取旋转矩形的角

标签 java rotation awt shapes rectangles

我有一个围绕它的中间旋转的矩形,我有另一个矩形我想连接到旋转矩形的右上角。问题是我不知道如何得到角,以便第二个矩形始终粘在那个角上。

这是我的示例代码。现在第二个矩形将一直在同一个地方,这不是我想要的结果。

package Test;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;

class Test{

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

    public Test(){
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new Graphic());
                frame.setSize(1000,700);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

class Graphic extends JPanel{
    private int x, y, windowW, windowH;
    private double angle;
    private Rectangle rect1, rect2;
    private Path2D path;
    private Timer timer;
    private AffineTransform rotation;

    public Graphic(){
        windowW = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth();
        windowH = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight();
        path = new Path2D.Double();
        rotation = new AffineTransform();
        angle = 0;
        x = windowW / 2;
        y = windowH / 2;
        timer = new Timer(100, new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                angle += .1;
                if(angle > 360) angle -= 360;
                repaint();
            }
        });
        timer.start();
    }

    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        rotation.setToTranslation(500, 200);
        rotation.rotate(angle, 32, 32);
        rect1 = new Rectangle(0, 0, 64, 64);
        path = new Path2D.Double(rect1, rotation);
        rect2 = new Rectangle(path.getBounds().x, path.getBounds().y, 10, 50);
        g2d.fill(path);
        g2d.fill(rect2);
    }
}

最佳答案

数学解:)

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    rotation.setToTranslation(500, 200);
    rotation.rotate(angle, 32, 32);
    rect1 = new Rectangle(0, 0, 64, 64);
    path = new Path2D.Double(rect1, rotation);
    double r = 32.0 * Math.sqrt(2);
    // (532, 232) - coordinates of rectangle center        |
    // you can change position of second rectangle by this V substraction (all you need to know is that the full circle corresponds to 2Pi)
    int x2 = (int) Math.round(532 + r * Math.cos(angle - Math.PI / 4));
    int y2 = (int) Math.round(232 + r * Math.sin(angle - Math.PI / 4));
    rect2 = new Rectangle(x2, y2, 10, 50);
    g2d.fill(path);
    g2d.fill(rect2);
}

当然,有些常量应该是类字段,而不是方法变量。

关于java - 获取旋转矩形的角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16777742/

相关文章:

Android 将位图旋转 90 度会导致图像被压扁。需要在纵向和横向之间进行真正的旋转

java - 可以将 swt.graphics.image 转换为 awt.image 吗?

Java - 如何使图像消失

java - 在Service中初始化网络连接

java - 如何从netbeans向数据库插入数据?

css - 在 IE 中使用过滤器取消旋转元素

ios - 在 3d 空间中沿 x 轴旋转平面时,如何计算底边的高度?

java - 如何在另一个 JPanel 上显示 JPanel 然后隐藏它

java - 使用 JPA 2 获取插入时自动生成的值

java - 使用 CDI 机制替换基于工厂的对象创建