Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lab4.gui;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.util.Observable;
- import java.util.Observer;
- import lab4.client.GomokuClient;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.SpringLayout;
- import lab4.data.GomokuGameState;
- /*
- * The GUI class
- */
- public class GomokuGUI implements Observer {
- private GomokuClient client;
- private GomokuGameState gamestate;
- JLabel messageLabel = new JLabel("Welcome to Gomoku!");
- JButton connectButton = new JButton("Connect");
- JButton newGameButton = new JButton("New game");
- JButton disconnectButton = new JButton("Disconnect");
- /**
- * The constructor
- *
- * @param g
- * The game state that the GUI will visualize
- * @param c
- * The client that is responsible for the communication
- */
- public GomokuGUI(GomokuGameState g, GomokuClient c) {
- this.client = c;
- this.gamestate = g;
- client.addObserver(this);
- gamestate.addObserver(this);
- final GamePanel gameGridPanel = new GamePanel(g.getGameGrid());
- JFrame square1 = new JFrame("Gomoku");
- square1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- // square1.setLocation(500,500);
- square1.setSize(800, 600);
- square1.setVisible(true);
- JPanel panel = new JPanel();
- square1.add(panel);
- panel.add(gameGridPanel);
- gameGridPanel.addMouseListener(new MouseListener() {
- @Override
- public void mouseClicked(MouseEvent arg0) {
- int xCoordinate = arg0.getX();
- int yCoordinate = arg0.getY();
- System.out.println("x= " + xCoordinate + " y= " + yCoordinate);
- int intPosition[] = gameGridPanel.getGridPosition(xCoordinate,
- yCoordinate);
- System.out.println(intPosition[0] + " " + intPosition[1]);
- gamestate.move(intPosition[0], intPosition[1]);
- gamestate.notifyObservers();
- }
- // #
- // public void mousePressed(MouseEvent e) {
- // #
- // int[] gridPosition = grid.getGridPosition(e.getX(), e.getY());
- // #
- // int xSquare = gridPosition[0];
- // #
- // int ySquare = gridPosition[1];
- // #
- // gamestate.move(xSquare, ySquare);
- // #
- // System.out.println("x:"+xSquare+"y:"+ySquare);
- // #
- // gamestate.notifyObservers();
- // #
- // //grid.updateUI(); ?
- // #
- // }
- @Override
- public void mouseEntered(MouseEvent arg0) {
- }
- public void mouseExited(MouseEvent arg0) {
- }
- public void mousePressed(MouseEvent arg0) {
- }
- public void mouseReleased(MouseEvent arg0) {
- }
- });
- JLabel messageLabel = new JLabel("Welcome to Gomoku!");
- panel.add(messageLabel);
- messageLabel.setVisible(true);
- JButton connectButton = new JButton("Connect");
- connectButton.setVisible(true);
- panel.add(connectButton);
- connectButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent g) {
- System.out.println("connectbutton");
- ConnectionWindow window = new ConnectionWindow(client);
- }
- });
- JButton newGameButton = new JButton("New Game");
- newGameButton.setVisible(true);
- messageLabel.setSize(120, 20);
- panel.add(newGameButton);
- newGameButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- System.out.println("newgame");
- gamestate.newGame();
- }
- }
- );
- JButton disconnectButton = new JButton("Disconnect");
- disconnectButton.setVisible(true);
- messageLabel.setSize(120, 20);
- panel.add(disconnectButton);
- disconnectButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent f) {
- System.out.println("disconnect");
- gamestate.disconnect();
- }
- });
- SpringLayout layout = new SpringLayout();
- panel.setLayout(layout);
- layout.putConstraint(SpringLayout.SOUTH, connectButton, 500,
- SpringLayout.NORTH, panel);
- layout.putConstraint(SpringLayout.SOUTH, newGameButton, 500,
- SpringLayout.NORTH, panel);
- layout.putConstraint(SpringLayout.SOUTH, disconnectButton, 500,
- SpringLayout.NORTH, panel);
- layout.putConstraint(SpringLayout.WEST, newGameButton, 150,
- SpringLayout.WEST, panel);
- layout.putConstraint(SpringLayout.WEST, connectButton, 100,
- SpringLayout.EAST, newGameButton);
- layout.putConstraint(SpringLayout.WEST, disconnectButton, 100,
- SpringLayout.EAST, connectButton);
- layout.putConstraint(SpringLayout.SOUTH, messageLabel, 560,
- SpringLayout.NORTH, panel);
- }
- public void update(Observable arg0, Object arg1) {
- // Update the buttons if the connection status has changed
- if (arg0 == client) {
- if (client.getConnectionStatus() == GomokuClient.UNCONNECTED) {
- connectButton.setEnabled(true);
- newGameButton.setEnabled(false);
- disconnectButton.setEnabled(false);
- } else {
- connectButton.setEnabled(false);
- newGameButton.setEnabled(true);
- disconnectButton.setEnabled(true);
- }
- }
- // Update the status text if the game state has changed
- if (arg0 == gamestate) {
- messageLabel.setText(gamestate.getMessageString());
- }
- }
- }
- // private class MouseListener implements ActionListener {
- //
- // public void actionPerformed(ActionEvent e) {
- // System.out.println("test");
- // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement