gpt4 book ai didi

java - "Run"方法不起作用

转载 作者:行者123 更新时间:2023-12-01 13:12:20 26 4
gpt4 key购买 nike

大家好,我是 Java 游戏编程新手,现在正在使用 Runnable 接口(interface)。由于某种原因,我的 run() 方法从未被调用,我不确定为什么。我尝试将许多 System.out.println 语句放在那里,但它们从未被打印。任何帮助将不胜感激!

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JPanel;


public class GamePanel extends JPanel implements Runnable
{
private final int WIDTH = 160;
private final int HEIGHT = WIDTH/12 *9;
private final int RATIO = 3;

private Thread animator;
private volatile boolean running;
private volatile boolean gameOver;

private double FPS = 60D;
private double period = 1000/FPS;

private Image dbImage;
private Graphics dbg;

public GamePanel()
{
setPreferredSize(new Dimension(WIDTH *3, HEIGHT*3));
setBackground(Color.WHITE);
setFocusable(true);
requestFocus();
terminate();
}

public void addNotify()
{
super.addNotify();
startGame();
}

public void startGame()
{
System.out.println("Thread started");
animator = new Thread();
animator.start();
}

public void stopGame()
{
System.out.println("Thread stopped");
running = false;
}

public void run()
{
long beforeTime, timeDiff, sleepTime;
beforeTime = System.currentTimeMillis();
System.out.println(beforeTime);

running = true;
while (running)
{
System.out.println("Running");
gameUpdate();
gameRender();
paintScreen();

timeDiff = System.currentTimeMillis() - beforeTime;
sleepTime = (long) period - timeDiff;

if(sleepTime <= 0)
sleepTime = 5;

try
{
Thread.sleep(sleepTime);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
beforeTime = System.currentTimeMillis();
}
System.exit(0);
}

public void gameRender()
{
if (dbImage == null)
{
dbImage = createImage(WIDTH, HEIGHT);
}
else
dbg = dbImage.getGraphics();

dbg.setColor(Color.WHITE);
dbg.fillRect(0, 0, WIDTH, HEIGHT);
}

public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(dbImage, 0, 0, null);
}

public void gameUpdate()
{

}

private void paintScreen()
{
Graphics g;
try
{
g = this.getGraphics();
if (g!= null && dbImage!= null)
g.drawImage(dbImage, 0, 0, null);
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
catch (Exception e)
{
System.out.println("Error: " + e.getMessage());
}
}

public void terminate()
{
addKeyListener (new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_ESCAPE)
{
stopGame();
}
}
});
}


}


import javax.swing.JFrame;


public class GameFrame extends JFrame
{
private final int WIDTH = 160;
private final int HEIGHT = WIDTH/12*9;
private final int RATIO = 3;

public GameFrame()
{
setTitle("User Input Game");
setSize(WIDTH*3,HEIGHT*3);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
GamePanel mainPanel = new GamePanel();
add(mainPanel);
}
}




public class Main
{

public static void main(String[] args)
{
new GameFrame().setVisible(true);
}

}

最佳答案

您需要更改 startGame() 方法:

    animator = new Thread(new GamePanel());

您需要将 Runnable(GamePanel 就是其中之一)传递到 Thread 构造函数中。然后,线程在启动时运行可运行对象。

关于java - "Run"方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22750300/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com