Package game.server

Source Code of game.server.SwingServer

package game.server;

import java.awt.event.*;
import java.io.IOException;
import javax.swing.*;

import game.EventsListener;
import game.GameEvent;
import game.impl.NetworkGameControllerServer;

public class SwingServer extends JFrame implements EventsListener{

   private NetworkGameControllerServer gameControllerServer;
   private JTextArea textArea;

   public SwingServer() {
      setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      JMenuBar menuBar = new JMenuBar();
      setJMenuBar(menuBar);
      JMenu menu = new JMenu("File");
      menuBar.add(menu);
      JMenuItem menuItem = new JMenuItem("Start server");
      menu.add(menuItem);
      menuItem.addActionListener(new ActionListener() {
         @Override
         public void actionPerformed(ActionEvent e) {
            try {
               if (gameControllerServer == null) {
                  gameControllerServer = new NetworkGameControllerServer();
                  gameControllerServer.setEventsListener(SwingServer.this);
               }
            } catch (IOException e1) {
               e1.printStackTrace();
            }
         }
      });
      menuItem = new JMenuItem("Stop server");
      menu.add(menuItem);
      menuItem.addActionListener(new ActionListener() {
         @Override
         public void actionPerformed(ActionEvent e) {
            SwingUtilities.invokeLater(new Runnable() {
               @Override
               public void run() {
                  if (gameControllerServer != null) {
                     gameControllerServer.shutdown();
                     gameControllerServer = null;
                  }
               }
            });
         }
      });
      setSize(200, 400);

      textArea = new JTextArea();
      getContentPane().add(new JScrollPane(textArea));
   }

   public static void main(String[] args) throws Exception {
      SwingUtilities.invokeLater(new Runnable() {
         @Override
         public void run() {
            new SwingServer().setVisible(true);
         }
      });
   }

   @Override
   public void onIncomingEvent(final GameEvent gameEvent) {
      SwingUtilities.invokeLater(new Runnable() {
         @Override
         public void run() {
            textArea.append("\n" + gameEvent);
         }
      });
   }

   @Override
   public void onOutgoingEvent(final GameEvent gameEvent) {
      SwingUtilities.invokeLater(new Runnable() {
         @Override
         public void run() {
            textArea.append("\n" + gameEvent);
         }
      });
   }
}
TOP

Related Classes of game.server.SwingServer

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.