Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. package lab4.gui;
  2.  
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.util.Observable;
  6. import java.util.Observer;
  7. import lab4.client.GomokuClient;
  8. import lab4.data.GameGrid;
  9. import lab4.data.GomokuGameState;
  10. import lab4.gui.*;
  11. import javax.swing.JButton;
  12. import javax.swing.JFrame;
  13. import javax.swing.JLabel;
  14. import javax.swing.JPanel;
  15. import javax.swing.SpringLayout;
  16.  
  17.  
  18.  
  19. /*
  20. * The GUI class
  21. */
  22.  
  23. public class GomokuGUI implements Observer {
  24.  
  25. private GomokuClient client;
  26. private GomokuGameState gamestate;
  27.  
  28. /**
  29. * The constructor
  30. *
  31. * @param g
  32. * The game state that the GUI will visualize
  33. * @param c
  34. * The client that is responsible for the communication
  35. */
  36. public GomokuGUI(GomokuGameState g, GomokuClient c){
  37. this.client = c;
  38. this.gamestate = g;
  39. //client.addObserver(this);
  40. //gamestate.addObserver(this);
  41.  
  42. GamePanel grid = new GamePanel( test);
  43.  
  44. JFrame square1 = new JFrame("Gomoku");
  45. square1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  46. square1.setLocation(500,500);
  47. square1.setSize(800,600);
  48. square1.setVisible(true);
  49. JPanel panel = new JPanel();
  50. square1.add(panel);
  51. panel.add(grid);
  52. JLabel messageLabel = new JLabel("Welcome to Gomoku!");
  53. panel.add(messageLabel);
  54. messageLabel.setVisible(true);
  55.  
  56. JButton connectButton=new JButton("Connect");
  57. connectButton.setVisible(true);
  58. panel.add(connectButton);
  59. connectButton.addActionListener(new ActionListener(){
  60. public void actionPerformed (ActionEvent g){
  61. // JA vad ska hända här?
  62.  
  63.  
  64.  
  65. }
  66. });
  67.  
  68. JButton newGameButton=new JButton("New Game");
  69. newGameButton.setVisible(true);
  70. messageLabel.setSize(120, 20);
  71. panel.add(newGameButton);
  72. newGameButton.addActionListener( new ActionListener()
  73. { public void actionPerformed (ActionEvent e){
  74. newGame();
  75.  
  76. }
  77.  
  78.  
  79. }
  80.  
  81. );
  82.  
  83. JButton disconnectButton= new JButton("Disconnect");
  84. disconnectButton.setVisible(true);
  85. messageLabel.setSize(120, 20);
  86. panel.add(disconnectButton);
  87. disconnectButton.addActionListener( new ActionListener(){
  88.  
  89. public void actionPerformed(ActionEvent f){
  90. disconnect();
  91. }
  92.  
  93. }
  94.  
  95. );
  96.  
  97.  
  98.  
  99. final SpringLayout layout = new SpringLayout();
  100. panel.setLayout(layout);
  101. layout.putConstraint(SpringLayout.SOUTH, connectButton,500, SpringLayout.NORTH, panel);
  102. layout.putConstraint(SpringLayout.SOUTH, newGameButton,500, SpringLayout.NORTH, panel);
  103. layout.putConstraint(SpringLayout.SOUTH, disconnectButton,500, SpringLayout.NORTH, panel);
  104. layout.putConstraint(SpringLayout.WEST, newGameButton,150, SpringLayout.WEST, panel);
  105. layout.putConstraint(SpringLayout.WEST, connectButton, 100, SpringLayout.EAST, newGameButton);
  106. layout.putConstraint(SpringLayout.WEST, disconnectButton, 100, SpringLayout.EAST, connectButton);
  107. layout.putConstraint(SpringLayout.SOUTH, messageLabel, 560, SpringLayout.NORTH, panel);
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115. public void update (Observable arg0, Object arg1) {
  116.  
  117. // Update the buttons if the connection status has changed
  118. if (arg0 == client) {
  119. if (client.getConnectionStatus() == GomokuClient.UNCONNECTED) {
  120. connectButton.setEnabled(true);
  121. newGameButton.setEnabled(false);
  122. disconnectButton.setEnabled(false);
  123. } else {
  124. connectButton.setEnabled(false);
  125. newGameButton.setEnabled(true);
  126. disconnectButton.setEnabled(true);
  127. }
  128. }
  129.  
  130. // Update the status text if the gamestate has changed
  131. if (arg0 == gamestate) {
  132. messageLabel.setText(gamestate.getMessageString());
  133. }
  134.  
  135. }
  136.  
  137. // private class MouseListener implements ActionListener {
  138. //
  139. // public void actionPerformed(ActionEvent e) {
  140. // System.out.println("test");
  141. // }
  142.  
  143.  
  144.  
  145.  
  146. }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement