Advertisement
Guest User

Untitled

a guest
Feb 20th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. package lab4.gui;
  2. import java.util.Observable;
  3. import java.util.Observer;
  4.  
  5. import javax.swing.JButton;
  6. import javax.swing.JFrame;
  7. import javax.swing.JLabel;
  8.  
  9. import lab4.client.GomokuClient;
  10. import lab4.data.GameGrid;
  11. import lab4.data.GomokuGameState;
  12.  
  13. /*
  14. * The GUI class
  15. */
  16.  
  17. public class GomokuGUI implements Observer{
  18.  
  19. private GomokuClient client;
  20. private GomokuGameState gamestate;
  21.  
  22. JFrame frame;
  23. GameGrid grid;
  24. GamePanel panel;
  25. JLabel messageLabel;
  26. JButton connectButton, disconnectButton, newGameButton;
  27.  
  28. /**
  29. * The constructor
  30. *
  31. * @param g The game state that the GUI will visualize
  32. * @param c The client that is responsible for the communication
  33. */
  34. public GomokuGUI(GomokuGameState g, GomokuClient c){
  35. frame = new JFrame("Gomoku");
  36. grid = new GameGrid(15);
  37. panel = new GamePanel(grid);
  38. messageLabel = new JLabel();
  39. connectButton = new JButton();
  40. disconnectButton = new JButton();
  41. newGameButton = new JButton();
  42.  
  43.  
  44. this.client = c;
  45. this.gamestate = g;
  46. client.addObserver(this);
  47. gamestate.addObserver(this);
  48.  
  49.  
  50. }
  51.  
  52.  
  53. public void update(Observable arg0, Object arg1) {
  54.  
  55. // Update the buttons if the connection status has changed
  56. if(arg0 == client){
  57. if(client.getConnectionStatus() == GomokuClient.UNCONNECTED){
  58. connectButton.setEnabled(true);
  59. newGameButton.setEnabled(false);
  60. disconnectButton.setEnabled(false);
  61. }else{
  62. connectButton.setEnabled(false);
  63. newGameButton.setEnabled(true);
  64. disconnectButton.setEnabled(true);
  65. }
  66. }
  67.  
  68. // Update the status text if the gamestate has changed
  69. if(arg0 == gamestate){
  70. messageLabel.setText(gamestate.getMessageString());
  71. }
  72.  
  73. }
  74.  
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement