Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 5.32 KB | None | 0 0
  1. package lab4.gui;
  2.  
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.awt.event.MouseEvent;
  6. import java.awt.event.MouseListener;
  7. import java.util.Observable;
  8. import java.util.Observer;
  9. import lab4.client.GomokuClient;
  10. import javax.swing.JButton;
  11. import javax.swing.JFrame;
  12. import javax.swing.JLabel;
  13. import javax.swing.JPanel;
  14. import javax.swing.SpringLayout;
  15. import lab4.data.GomokuGameState;
  16.  
  17. /*
  18.  * The GUI class
  19.  */
  20.  
  21. public class GomokuGUI implements Observer {
  22.  
  23.     private GomokuClient client;
  24.     private GomokuGameState gamestate;
  25.    
  26.     JLabel messageLabel = new JLabel("Welcome to Gomoku!");
  27.  
  28.     JButton connectButton = new JButton("Connect");
  29.  
  30.     JButton newGameButton = new JButton("New game");
  31.  
  32.     JButton disconnectButton = new JButton("Disconnect");
  33.  
  34.     /**
  35.      * The constructor
  36.      *
  37.      * @param g
  38.      *            The game state that the GUI will visualize
  39.      * @param c
  40.      *            The client that is responsible for the communication
  41.      */
  42.     public GomokuGUI(GomokuGameState g, GomokuClient c) {
  43.         this.client = c;
  44.         this.gamestate = g;
  45.         client.addObserver(this);
  46.         gamestate.addObserver(this);
  47.  
  48.         final GamePanel gameGridPanel = new GamePanel(g.getGameGrid());
  49.  
  50.         JFrame square1 = new JFrame("Gomoku");
  51.         square1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  52.         // square1.setLocation(500,500);
  53.         square1.setSize(800, 600);
  54.         square1.setVisible(true);
  55.  
  56.         JPanel panel = new JPanel();
  57.         square1.add(panel);
  58.         panel.add(gameGridPanel);
  59.         gameGridPanel.addMouseListener(new MouseListener() {
  60.  
  61.             @Override
  62.             public void mouseClicked(MouseEvent arg0) {
  63.                 int xCoordinate = arg0.getX();
  64.                 int yCoordinate = arg0.getY();
  65.                 System.out.println("x= " + xCoordinate + " y= " + yCoordinate);
  66.                 int intPosition[] = gameGridPanel.getGridPosition(xCoordinate,
  67.                         yCoordinate);
  68.                 System.out.println(intPosition[0] + "  " + intPosition[1]);
  69.                 gamestate.move(intPosition[0], intPosition[1]);
  70.                 gamestate.notifyObservers();
  71.  
  72.             }
  73.  
  74.             // #
  75.             // public void mousePressed(MouseEvent e) {
  76.             // #
  77.             // int[] gridPosition = grid.getGridPosition(e.getX(), e.getY());
  78.             // #
  79.             // int xSquare = gridPosition[0];
  80.             // #
  81.             // int ySquare = gridPosition[1];
  82.             // #
  83.             // gamestate.move(xSquare, ySquare);
  84.             // #
  85.             // System.out.println("x:"+xSquare+"y:"+ySquare);
  86.             // #
  87.             // gamestate.notifyObservers();
  88.             // #
  89.             // //grid.updateUI(); ?
  90.             // #
  91.             // }
  92.  
  93.             @Override
  94.             public void mouseEntered(MouseEvent arg0) {
  95.             }
  96.  
  97.             public void mouseExited(MouseEvent arg0) {
  98.             }
  99.  
  100.             public void mousePressed(MouseEvent arg0) {
  101.             }
  102.  
  103.             public void mouseReleased(MouseEvent arg0) {
  104.             }
  105.         });
  106.  
  107.         JLabel messageLabel = new JLabel("Welcome to Gomoku!");
  108.         panel.add(messageLabel);
  109.         messageLabel.setVisible(true);
  110.  
  111.         JButton connectButton = new JButton("Connect");
  112.         connectButton.setVisible(true);
  113.         panel.add(connectButton);
  114.         connectButton.addActionListener(new ActionListener() {
  115.             public void actionPerformed(ActionEvent g) {
  116.                 System.out.println("connectbutton");
  117.                 ConnectionWindow window = new ConnectionWindow(client);
  118.  
  119.             }
  120.         });
  121.  
  122.         JButton newGameButton = new JButton("New Game");
  123.         newGameButton.setVisible(true);
  124.         messageLabel.setSize(120, 20);
  125.         panel.add(newGameButton);
  126.         newGameButton.addActionListener(new ActionListener() {
  127.             public void actionPerformed(ActionEvent e) {
  128.                 System.out.println("newgame");
  129.                 gamestate.newGame();
  130.  
  131.             }
  132.  
  133.         }
  134.  
  135.         );
  136.  
  137.         JButton disconnectButton = new JButton("Disconnect");
  138.         disconnectButton.setVisible(true);
  139.         messageLabel.setSize(120, 20);
  140.         panel.add(disconnectButton);
  141.         disconnectButton.addActionListener(new ActionListener() {
  142.             public void actionPerformed(ActionEvent f) {
  143.                 System.out.println("disconnect");
  144.                 gamestate.disconnect();
  145.             }
  146.         });
  147.  
  148.         SpringLayout layout = new SpringLayout();
  149.         panel.setLayout(layout);
  150.         layout.putConstraint(SpringLayout.SOUTH, connectButton, 500,
  151.                 SpringLayout.NORTH, panel);
  152.         layout.putConstraint(SpringLayout.SOUTH, newGameButton, 500,
  153.                 SpringLayout.NORTH, panel);
  154.         layout.putConstraint(SpringLayout.SOUTH, disconnectButton, 500,
  155.                 SpringLayout.NORTH, panel);
  156.         layout.putConstraint(SpringLayout.WEST, newGameButton, 150,
  157.                 SpringLayout.WEST, panel);
  158.         layout.putConstraint(SpringLayout.WEST, connectButton, 100,
  159.                 SpringLayout.EAST, newGameButton);
  160.         layout.putConstraint(SpringLayout.WEST, disconnectButton, 100,
  161.                 SpringLayout.EAST, connectButton);
  162.         layout.putConstraint(SpringLayout.SOUTH, messageLabel, 560,
  163.                 SpringLayout.NORTH, panel);
  164.  
  165.     }
  166.  
  167.     public void update(Observable arg0, Object arg1) {
  168.  
  169.         // Update the buttons if the connection status has changed
  170.         if (arg0 == client) {
  171.             if (client.getConnectionStatus() == GomokuClient.UNCONNECTED) {
  172.                 connectButton.setEnabled(true);
  173.                 newGameButton.setEnabled(false);
  174.                 disconnectButton.setEnabled(false);
  175.             } else {
  176.                 connectButton.setEnabled(false);
  177.                 newGameButton.setEnabled(true);
  178.                 disconnectButton.setEnabled(true);
  179.             }
  180.         }
  181.  
  182.         // Update the status text if the game state has changed
  183.         if (arg0 == gamestate) {
  184.             messageLabel.setText(gamestate.getMessageString());
  185.         }
  186.     }
  187.  
  188. }
  189.  
  190. // private class MouseListener implements ActionListener {
  191. //
  192. // public void actionPerformed(ActionEvent e) {
  193. // System.out.println("test");
  194. // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement