Я делаю эту игру-головоломку, которая получает письмо с клавиатуры с помощью Scannner (System.in) и изменяет состояние головоломки.Провоцировать прерывание в основном логическом цикле
Теперь, я делаю gui для игры. У меня есть JPanel с JLabels, который показывает головоломку и еще один JLabel с двумя JButtons, один из которых уже есть кнопка выхода, а другая должна быть кнопкой воспроизведения. Эта кнопка воспроизведения должна имитировать перезапуск всей игры. Мой вопрос: как я могу остановить mainloop сразу после нажатия кнопки Replay? Есть ли системный вызов, который я могу использовать, когда я нажимаю кнопку, чтобы он генерировал прерывание в основном цикле?
Вот главная логика цикла:
public static void main(String args[]){
Cliente i=new Cliente();
int labSize;
labSize=i.func1();
boolean b1;
b1=i.func2();
boolean b2;
b2=i.func3();
int nDarts;
nDarts=i.func4();
int nDrags;
nDrags=i.func5();
Game g=new Game(labSize,nDarts,nDrags);
g.boolInit(b1, b2);
g.initGame();
i.setTab(g.getLab(), g.getLabSize());
do{
g.updateGame();
i.setTab(g.getLab(), g.getLabSize());
i.showTab();
g.moveHeroObj(i);
g.firingDartsLogic(i);
g.weaponCaught();
g.shieldCaught();
g.dartLogic();
g.flamesLogic();
if(!g.didGameEnd()){
g.dragonKillLogic();
g.dragonLogic();
}
g.updateHeroSymbol();
g.updateDragonSymbol();
if(g.finishedGame())
g.changeEndBool();
}while(!g.didGameEnd());
g.updateGame();
i.setTab(g.getLab(), g.getLabSize());
i.showTab();
i.exitFunc();
return;
}
А вот GUI:
public GameGUI(int n) {
labSize=n;
mainFrame = new JFrame("Maze Game");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
mainFrame.setVisible(false);
choicesPanel=new JPanel();
choicesPanel.setLayout(new GridLayout(0, 1));
labPanel=new JPanel(new GridLayout(labSize,labSize));
replayButton=new JButton("Replay");
replayButton.setAlignmentY(Component.TOP_ALIGNMENT);
replayButton.setAlignmentX(Component.LEFT_ALIGNMENT);
replayButton.addActionListener(new ReplayHandler());
exitButton=new JButton("Exit");
exitButton.setAlignmentY(Component.TOP_ALIGNMENT);
exitButton.setAlignmentX(Component.LEFT_ALIGNMENT);
exitButton.addActionListener(new ExitHandler());
choicesPanel.add(replayButton);
choicesPanel.add(exitButton);
mainFrame.getContentPane().add(choicesPanel);
mainFrame.getContentPane().add(labPanel);
}
private class ExitHandler implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
String[] holder={"Yes","Cancel"};
if(JOptionPane.showOptionDialog(null, "Are you sure you want to exit?", "", 0, 0, null, holder, 0)==0){
System.exit(0);
}
}
}
private class ReplayHandler implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
String[] holder={"Yes","No"};
if(JOptionPane.showOptionDialog(null, "Start a new game?", "", 0, 0, null, holder, 0)==0){
}
}
}
Спасибо за помощь заранее
Простейшим способом было бы иметь ссылку на вашу игру в вашем «ReplayHandler» и конкретном методе, например. 'Game.restartGame()' –