Package pong.server

Source Code of pong.server.ServerController

package pong.server;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import pong.Pong;
import pong.server.model.ServerModel;
import pong.server.model.ServerModelListener;
import pong.server.view.ServerView;
import pong.server.view.ServerViewListener;
import pong.server.view.ViewInterface;

/**
* This is the controller of the server.
*
* @author Lorenzo Gatto
*
*/
public class ServerController implements ServerModelListener, ServerViewListener {
  private final ViewInterface view;
  private final Pong game;
  private Thread model;

  /**
   * Initializes the server graphics.
   *
   * @param window
   *            the game window
   * @param game
   *            the pong object calling this initializer
   */
  public ServerController(final JFrame window, final Pong game) {
    this.view = new ServerView(this, window);
    this.view.drawOptionsPanel();
    this.model = null;
    this.game = game;
  }

  @Override
  public synchronized void createServer(final int portNumber, final int roundNumber) {
    this.model = new ServerModel(portNumber, roundNumber, this);
    this.model.start();
  }

  @Override
  public synchronized void disconnect() {
    this.model.interrupt();
  }

  @Override
  public synchronized void backToMainMenu() {
    this.game.printMainMenu();
  }

  @Override
  public synchronized void printMessage(final String message) {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        view.printMessage(message);
      }
    });
  }

  @Override
  public synchronized void drawOptionsPanel() {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        view.drawOptionsPanel();
      }
    });
  }

  @Override
  public synchronized void drawLogPanel() {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        view.drawLogPanel();
      }
    });
  }

  @Override
  public void showErrorDialog(final String message, final String title) {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        view.showErrorDialog(message, title);
      }
    });
  }

}
TOP

Related Classes of pong.server.ServerController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.