package pong;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import pong.client.ClientController;
import pong.common.GlobalSettings;
import pong.server.ServerController;
/**
* This is the main class of the game. This creates a window with two buttons for deciding whether
* to run the client or the server controller.
*
* @author Lorenzo Gatto
*
*/
public class Pong {
private final JFrame frame;
private final JPanel selectionPanel;
/**
* Pong constructor. It creates the window with the panel for selecting to run a client or a
* server.
*/
public Pong() {
frame = new JFrame("Pong");
frame.setSize(GlobalSettings.DEFAULT_FRAME_WIDTH, GlobalSettings.DEFAULT_FRAME_HEIGHT);
frame.setMinimumSize(new Dimension(GlobalSettings.MINIMUM_WINDOW_WIDTH_PX,
GlobalSettings.MINIMUM_WINDOW_HEIGHT_PX));
selectionPanel = new JPanel();
final JButton clientButton = new JButton("Client");
final JButton serverButton = new JButton("Server");
selectionPanel.add(clientButton);
selectionPanel.add(serverButton);
frame.add(selectionPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
clientButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(final ActionEvent e) {
clientButtonPressedEvent();
}
});
serverButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(final ActionEvent e) {
serverButtonPressedEvent();
}
});
frame.setVisible(true);
}
/**
* This method instantiates a server.
*/
protected void serverButtonPressedEvent() {
new ServerController(frame, this);
}
/**
* This method instantiates a client.
*/
protected void clientButtonPressedEvent() {
new ClientController(frame, this);
}
/**
* Prints the main menu after on client or server demand. Warning: this function does not
* terminate the client or the server itself.
*/
public void printMainMenu() {
frame.getContentPane().removeAll();
frame.add(selectionPanel);
frame.validate();
frame.repaint();
}
/**
* The main class of the program. It will just instantiate a Pong object.
*
* @param args
* it won't be considered
*/
public static void main(final String[] args) {
new Pong();
}
}