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.util.Observable;
- import java.util.Observer;
- import lab4.client.GomokuClient;
- import lab4.data.GameGrid;
- import lab4.data.GomokuGameState;
- import lab4.gui.*;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.SpringLayout;
- /*
- * The GUI class
- */
- public class GomokuGUI implements Observer {
- private GomokuClient client;
- private GomokuGameState gamestate;
- /**
- * 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);
- GamePanel grid = new GamePanel( test);
- 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(grid);
- 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){
- // JA vad ska hända här?
- }
- });
- 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){
- 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){
- disconnect();
- }
- }
- );
- final 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 gamestate 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