【发布时间】:2024-01-12 22:04:02
【问题描述】:
我已经用 java 编程有一段时间了,我刚刚开始 2D 图形(游戏开发)。在编写我的游戏时,我注意到我的游戏出现了渲染问题。当我的播放器在屏幕上运行时,像素会出现故障。这就像多人游戏的延迟,但几乎不引人注意。在研究了这个问题之后,我没有想出任何解决方案,所以我请求你的帮助。
我的问题:我的游戏渲染很差,我想知道我的渲染方式是否很差。如果是这样,你能指出我的任何资源吗?
我的渲染类
public class Render extends JPanel implements Runnable{
int SCREEN_WIDTH,SCREEN_HEIGHT;
Game game;
Thread t;
public Render(int w,int h,Game g){
this.SCREEN_WIDTH = w;
this.SCREEN_HEIGHT = h;
this.game = g;
this.setDoubleBuffered(true);
}
public void run(){
while(true){
repaint();
}
}
public void paint(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.translate(-this.game.getCamera().getX(), -game.getCamera().getY());
g2d.setColor(Color.black);
//fill the screen with black
g2d.fillRect(0, 0, game.getLevelHandler().getLevel().getWidth() * 32, game.getLevelHandler().getLevel().getHeight() * 32);
/**for(Entity E: game.getObjects()){
E.draw(g2d);
}*/
//send the graphics object to my player...IS THIS OKAY TO DO?
game.getPlayer().draw(g2d);
}
public void start(){
t = new Thread(this);
t.start();
}
}
public class Player extends Entity{
float dx,dy,moveSpeed;
boolean jumping, canJump;
float terminalVelocity,acceleration = 0;
int GravityCounter,jumps,maxJumps,jumpheight = 0;
public Player(float x, float y, float w, float h, ObjectID id, Game g) {
super(x, y, w, h, id, g);
//gravity
terminalVelocity += 7;
acceleration += 0.2;
moveSpeed += 2.5;
//jumping
jumpheight = 40;
maxJumps = 2;
jumps = maxJumps;
}
public void tick() {
dx = 0;
dy = 0;
collisions();
move();
x += dx;
y += dy;
}
public void collisions(){
}
public void move(){
}
//the drawing
public void draw(Graphics2D g) {
g.setColor(Color.green);
g.drawRect((int)x, (int)y, (int)w, (int)h);
}
}
编辑:可下载链接here , 如果需要任何更改,我会尝试更正它。另外,内存泄漏时是否会出现这种故障?
由于建议,我修改了我的代码,这就是我现在所拥有的。
渲染器:
public class Render extends JPanel implements Runnable{
int SCREEN_WIDTH,SCREEN_HEIGHT;
Game game;
Thread t;
public Render(int w,int h,Game g){
this.SCREEN_WIDTH = w;
this.SCREEN_HEIGHT = h;
this.game = g;
this.setDoubleBuffered(true);
}
public void run(){
while(true){
repaint();
try {
Thread.sleep(16);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public synchronized void paint(Graphics g){
Graphics2D g2d = (Graphics2D) g;
g2d.translate(-this.game.getCamera().getX(), -game.getCamera().getY());
g2d.setColor(Color.black);
g2d.fillRect(0, 0, game.getLevelHandler().getLevel().getWidth() * 32, game.getLevelHandler().getLevel().getHeight() * 32);
for(Entity E: game.getObjects()){
synchronized(E){E.draw(g2d);}
}
synchronized(game.getPlayer()){game.getPlayer().draw(g2d);}
}
public void start(){
t = new Thread(this);
t.start();
}
}
播放器类没有改变
源代码:here
【问题讨论】:
-
您已覆盖
paint,但正在调用super.paintComponent,开始出现问题。请记住,Swing 不是线程安全的,您需要采取措施将线程更新与屏幕更新同步,以确保在绘制游戏实体时不会修改它们的状态... -
while(true) { repaint(); }...我想你可能想给 EDT 一些喘息的空间,尝试在repaint之后添加Thread.sleep(16)... -
考虑提供一个runnable example 来证明您的问题。这将导致更少的混乱和更好的响应
-
@MadProgrammer 看看我上面的修改。
-
覆盖
paintComponent,调用super.paintComponent。除非您可以为演示您的问题的可运行示例提供源代码,否则我们将无法提供进一步帮助...