Java GUI旋转和Rectangle的翻译 [英] Java GUI Rotation and Translation of Rectangle

查看:161
本文介绍了Java GUI旋转和Rectangle的翻译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在JPanel中绘制一个矩形,该矩形将平移然后自行旋转以模仿汽车的运动。我已经能够使矩形平移和旋转,但它围绕(0,0)的原点旋转。我非常高兴我可以让矩形移动和旋转,因为我对Java GUI很新,但我似乎无法得到如何让矩形围绕它自己旋转,因为我尝试了更多它,当我初始化矩形并将其旋转45度它的位置已更改,我假设是从旋转方法追加的变换矩阵。

I am trying to draw a rectangle in JPanel that would translate and then rotate itself to mimic the movement of a car. I have been able to make the rectangle translate and rotate, however it rotates around the origin of (0,0). I'm very pleased that I was able to have the rectangle move and rotate as I am very new to Java GUI, but I can not seem to get how to have the rectangle rotate around itself, because I experimented more with it, and when I initialized the rectangle and rotate it 45 degrees it's position was changed, which I would assume is the transform matrix that is appended from the rotate method.

我检查了网站我怎么解决这个问题,但是我只发现了如何旋转矩形,而不是如何像模拟汽车的运动一样旋转和移动。我认为它关注它的变换矩阵,但我只是猜测。所以我的问题是如何让矩形能够旋转并在自身周围移动而不是JPanel中的一个点。

I checked through the site on how would I solve this, however I only found how to rotate a rectangle and not on how to rotate and move like the movement of a simulated car. I would presume it is concerning about its transform matrix, but I'm only speculating. So my question is how would I be able to have the rectangle be able to rotate and move around itself and not against a point in JPanel.

这是我到目前为止的代码:

Here's the code that I have come up so far:

public class Draw extends JPanel implements ActionListener {


private int x = 100;
private int y = 100;
private double theta = Math.PI;

Rectangle rec = new Rectangle(x,y,25,25);

Timer timer = new Timer(25,this);

Draw(){
    setBackground(Color.black);
    timer.start();
}

public void paintComponent(Graphics g){
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D)g;     
    g2d.setColor(Color.white);
    rec.x = 100;
    rec.y = 100;
    g2d.rotate(theta);
    g2d.draw(rec);
    g2d.fill(rec);

}

public void actionPerformed(ActionEvent e) {
    x = (int) (x + (Math.cos(theta))*1);
    y = (int) (y + (Math.sin(theta))*1);
    theta = theta - (5*Math.PI/180);
    repaint();
}


推荐答案

通常有两种方法之一使用:

One of two approaches are commonly used:


  • 围绕中心旋转图形上下文( x y Shape ,如此处所示。

rotate(double theta, double x, double y)


  • 转换为原点,旋转并转换回来,如此处

    g2d.translate(this.getWidth() / 2, this.getHeight() / 2);
    g2d.rotate(theta);
    g2d.translate(-image.getWidth(null) / 2, -image.getHeight(null) / 2);
    


  • 注意明显的第二个例子中连接的逆序。

    Note the apparent reverse order of concatenation in the second example.

    附录:仔细观察你的例子,以下更改将旋转 Rectangle 围绕小组的中心。

    Addendum: Looking more closely at your example, the following change rotates the Rectangle around the panel's center.

    g2d.rotate(theta, getWidth() / 2, getHeight() / 2);
    

    此外,使用 @Override 注释,并给你的小组一个合理的首选大小:

    Also, use the @Override annotation, and give your panel a reasonable preferred size:

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

    这篇关于Java GUI旋转和Rectangle的翻译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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