简体   繁体   English

Java GUI的旋转和Rectangle的翻译

[英]Java GUI Rotation and Translation of Rectangle

I am trying to draw a rectangle in JPanel that would translate and then rotate itself to mimic the movement of a car. 我试图在JPanel中绘制一个矩形,它将翻译然后旋转自己以模仿汽车的运动。 I have been able to make the rectangle translate and rotate, however it rotates around the origin of (0,0). 我已经能够使矩形平移和旋转,但它围绕(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. 我非常高兴能够让矩形移动和旋转,因为我是Java GUI的新手,但我似乎无法得到如何使矩形围绕自身旋转,因为我尝试了更多它,当我初始化矩形并将其旋转45度,它的位置发生了变化,我假设是旋转方法附加的变换矩阵。

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. 所以我的问题是如何让矩形能够旋转并在自身周围移动而不是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: 通常使用两种方法之一:

  • Rotate the graphics context around the center ( x , y ) of the Shape , as shown here . 绕的中心(X,Y)的图形上下文Shape ,如图所示在这里

     rotate(double theta, double x, double y) 
  • Translate to the origin, rotate and translate back, as shown here . 转换到原点,旋转和平移回,如图所示这里

     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. 请注意第二个示例中连接的明显相反顺序。

Addendum: Looking more closely at your example, the following change rotates the Rectangle around the panel's center. 附录:仔细观察您的示例,以下更改会围绕面板中心旋转Rectangle

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

Also, use the @Override annotation, and give your panel a reasonable preferred size: 此外,使用@Override注释,并为您的面板提供合理的首选大小:

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

Use affine transform to rotate the rectangle and convert it into the rotated polynomial. 使用仿射变换旋转矩形并将其转换为旋转多项式。 Check the code below: 检查以下代码:

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    g2d.setColor(Color.white);
    /* rotate rectnagle around rec.x and rec.y */
    AffineTransform at = AffineTransform.getRotateInstance(theta, 
        rec.x, rec.y);
    /* create the plunomial */
    Polygon p = new Polygon();
    /* path interator of the affine transformed polynomial */
    PathIterator i = rec.getPathIterator(at);
    while (!i.isDone()) {
        double[] points = new double[2];
        i.currentSegment(points);
        p.addPoint((int) points[0], (int) points[1]);

        i.next();
    }
    g2d.fill(p);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM