[英]Thread errors when running Java game
I am getting some strange errors when running my Java game. 运行Java游戏时出现一些奇怪的错误。 I've been working on this game for weeks as a project for my computer science class and it is due on 6/4/2015, so I appreciate any help.
作为计算机科学课程的一个项目,我一直在从事这款游戏的开发,预计将于2015年6月4日到期,因此我非常感谢。 Initially I wrote the game in a class called Game and ran it from a static method called run.
最初,我是在名为Game的类中编写游戏的,然后从名为run的静态方法运行它。 Later, I decided to add a GUI menu in a class called Control.
后来,我决定在名为Control的类中添加GUI菜单。 To run the game now, I call the main method in the Control class.
现在运行游戏,我在Control类中调用main方法。 This menu has a button with an action listener.
该菜单有一个带有动作监听器的按钮。 When the button is clicked, the run method in the Game class is called.
单击该按钮时,将调用Game类中的run方法。 If I click and run the run method directly the game works fine.
如果我单击并直接运行run方法,则游戏运行正常。 But if I click the button which calls the run method it draws a frame, but not the actual game.
但是,如果我单击调用run方法的按钮,它将绘制一个框架,而不是实际的游戏。
Here is my code for the GUI: 这是我的GUI代码:
public class Control extends JFrame implements ActionListener {
// JPanel
public JPanel pnlButton = new JPanel();
// Buttons
public JButton btnAddFlight = new JButton("Multiplayer");
public JButton single = new JButton("Singleplayer");
public Control() throws IOException,InterruptedException {
super("Bouncy Ball");
//Set button size
btnAddFlight.setBounds(150, 400, 220, 30);
single.setBounds(150,350,220,30);
// JPanel bounds
pnlButton.setBounds(0, 0, 500, 500);
pnlButton.setBackground(Color.WHITE);
// Adding the Bouncy Ball logo to JPanel
String path = "gg.jpg";
File file = new File(path);
BufferedImage image = ImageIO.read(file);
JLabel label = new JLabel(new ImageIcon(image));
label.setBounds(179,50,150,150);
//Action Listener setup
single.addActionListener(this);
//add buttons and title logo to JPanel
pnlButton.add(btnAddFlight);
pnlButton.add(label);
pnlButton.add(single);
//Set up and add the instructions to the JPanel
JLabel gg = new JLabel("Welcome to Bouncy Ball, a game where you have to manipulate");
gg.setFont(new Font("Serif", Font.PLAIN, 18));
gg.setBounds(0,10,500,500);
pnlButton.add(gg);
JLabel f = new JLabel("the window size with WASD to win! Click the buttons to start.");
f.setFont(new Font("Serif", Font.PLAIN, 18));
f.setBounds(0,28,500,500);
pnlButton.add(f);
pnlButton.setLayout(null);
//Add JPanel to JFrame
this.add(pnlButton);
// JFrame properties
setSize(500, 500);
setTitle("Bouncy Ball");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);;
}
@Override
public void actionPerformed(ActionEvent submitClicked) {
try{
isClicked = true;
Game.run();
}
catch(InterruptedException a){}
}
public static void main(String[] args) throws IOException,InterruptedException{
new Control();
}
}
and here is the run method in my game class: 这是我的游戏类中的run方法:
public static void run() throws InterruptedException {
//Setting up JFrame
frame = new KeyFrame("Bouncy Ball");
frame.setSize(1000, 1000);
//Setting up Scanner and get user level input
Scanner scan = new Scanner (System.in);
System.out.println("Level one, two, three, or four?(must be an int, four is multiplayer)");
f = scan.nextInt();
//Display JFrame
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.toFront();
//Add Game JPanel
game = new Game();
frame.add(game);
//Setting up game basics
b = new Ball(game,0,0,30,30);
setUpLevel();
objList.add(0,b);
//Main Game Loop
while (true) {
//Request Focus
game.requestFocusInWindow();
//Update current Time
lastStartTime = System.currentTimeMillis();
//Move the ball and the player
p.move();
b.move();
//Refresh JPanel
game.repaint();
Thread.sleep(10);
//Check if the player wins or loses
checkWin();
checkLoss();
}
}
If I call the run method directly it works, but not if I click the button in the GUI. 如果我直接调用run方法,它将起作用,但是如果我单击GUI中的按钮,则不会起作用。 I have a feeling that it is a thread issue, because the main thread ends once run is called.
我感觉这是一个线程问题,因为一旦运行被调用,主线程便结束了。 I'm not a 100% percent sure as to the cause though.
我不确定原因是100%。 I'd appreciate any responses, I've been working on this game for weeks as a project for my computer science class and it is due on 6/4/2015.
我会很感激您的回应,作为我的计算机科学课的一个项目,我已经在这个游戏上工作了数周,它将于2015年6月4日到期。
Swing is single threaded - all painting, events, etc...occur on this thread (named the EDT ). 摆动是单线程的-所有绘画,事件等都发生在该线程上(称为EDT )。 The
Game.run
method is called on the EDT, which in turn executing a long running task (eg while (true)
, Thread.sleep
) - this prevents the EDT from doing anything else. 在EDT上调用
Game.run
方法,该方法依次执行长时间运行的任务(例如while (true)
, Thread.sleep
)-这可以防止EDT做其他任何事情。 To perform animation, consider using a Thread , or better yet a Swing Timer . 要执行动画,请考虑使用Thread或更好的Swing Timer 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.