I write a card game with server and client (multi client). And I need to check every second if game is started or game is shut down. So I write a java.swing.timer to check it. But it doesn't set visibility of JPanel when game is started. (Sorry for bad English)
That is code segment
public void checkGameState() {
ActionListener action;
timer = new Timer(delay, (ActionEvent e) -> {
try {
boolean isEnd = checkIfGameIsClose();
if (isEnd == true) {
System.out.println("---->"+isEnd);
mainGamePanel.setVisible(false);
waitRoomPanel.setVisible(true);
availability_button.setText("Ready");
availability_button.setBackground(Color.red);
availability_status = false;
}
} catch (IOException ex) {
Logger.getLogger(ClientFrame.class.getName()).log(Level.SEVERE, null, ex);
}
try {
boolean isStart = checkIfGameIsOpen();
if (isStart == true) {
System.out.println("---->"+isStart);
mainGamePanel.setVisible(true);
waitRoomPanel.setVisible(false);
clientConnectToServer.isEndTheGame();
}
} catch (IOException ex) {
Logger.getLogger(ClientFrame.class.getName()).log(Level.SEVERE, null, ex);
}
});
timer.start();
}
public boolean checkIfGameIsClose() throws IOException {
boolean isClosedGame = clientConnectToServer.checkifGameIsEnd();
System.out.println(isClosedGame + " ---> Close");
return isClosedGame;
}
public boolean checkIfGameIsOpen() throws IOException {
boolean gameIsStart = clientConnectToServer.startGame();
System.out.println(gameIsStart + "---> open");
return gameIsStart;
}
Then game is started method checkIfGameIsOpen() return true. Then game is shut down method checkIfGameIsClose() return true.
mainGamePanel is Panel what should appear when game is started.
Problems and possible solutions:
revalidate()
and repaint()
on this container after making all such changes. The first, revalidate, tells the container and its layout managers to re-layout all contained components, and the second, repaint, requests that the JVM re-draw the container and all its children. This will help rid the container of "dirty" pixels.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.