Java Graphics2D仅在旋转0/360度时绘制

时间:2017-02-26 23:23:21

标签: java graphics2d

我试图制造一个只是向前移动的旋转敌人。玩家显示完美,但由于某种原因,敌人只会显示我是否将旋转设置为360或0,即使x和y位置按原样移动。

package Game;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;

public class Enemy extends Rectangle implements GameObject {

    private int speed;
    private double angle;

    public Enemy(int x, int y, int w, int h, int speed, int angle) {
        this.x = x;
        this.y = y;
        this.width = w;
        this.height = h;
        this.speed = speed;
        this.angle = Math.toRadians(angle);
    }

    @Override
    public void update(Engine g) {
        this.x += (speed * (float) Math.cos(angle));
        this.y += (speed * (float) Math.sin(angle));

        System.out.println(this.x + " " + this.y);
    }

    @Override
    public void render(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(Color.red);
        g2d.rotate(angle);
        g2d.fill(this);
    }

}

1 个答案:

答案 0 :(得分:1)

将渲染组件更改为此

@Override
public void render(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.setColor(Color.red);
    g2d.rotate(this.angle);
    g2d.fill(this);
    g2d.draw(this);
    this.render(g);
}