Java Graphics2D 仅在旋转 0/360 度时绘制
Java Graphics2D only drawing if rotated 0/360 degrees
我试图制作一个旋转的敌人,它只会向前移动。播放器显示完美,但由于某些原因敌人仅在我将旋转设置为 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);
}
我试图制作一个旋转的敌人,它只会向前移动。播放器显示完美,但由于某些原因敌人仅在我将旋转设置为 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);
}