Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Graphics2D仅在旋转0/360度时绘制_Java_Graphics2d - Fatal编程技术网

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

Java Graphics2D仅在旋转0/360度时绘制,java,graphics2d,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

我试图制造一个旋转的敌人,只是向前移动。玩家表现得非常完美,但由于某种原因,只有当我将旋转设置为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);
    }

}

将渲染组件更改为

@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);
}

这是对
render
方法的递归调用-如果我没有忽略某些内容,那么这将导致
StackOverflowError
(或者至少是其他奇怪的行为)。