Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package client;
- import common.UIIF;
- import java.applet.Applet;
- import java.applet.AudioClip;
- import java.awt.BorderLayout;
- import java.awt.Container;
- import java.awt.Dimension;
- import java.awt.event.*;
- import java.awt.Font;
- import java.net.URL;
- import javax.swing.*;
- import java.io.*;
- public class ClientGUI extends JFrame implements UIIF, ActionListener
- {
- //Class and Instance variables *************************************************
- /**
- * The default port to connect on.
- */
- final public static int DEFAULT_PORT = 5555;
- final private static long serialVersionUID=0;
- /**
- * The instance of the client that created this GUI.
- */
- Client client;
- //GUI variables ******************************************************
- private boolean running = false;
- private boolean loggedIn = false;
- private boolean soundON = true;
- private AudioClip clickSound;
- private Container pane = getContentPane(); //create main pane
- private JPanel mainDisplay = new JPanel(); //create sub panels
- private JPanel controlBar = new JPanel();
- private JPanel statusBar = new JPanel();
- //create all the buttons and labels and text fields (UI elements)
- private JButton startButton = new JButton("");
- private ImageIcon startIcon = new ImageIcon(getClass().getResource("start.png"));
- private JButton loginButton = new JButton("");
- private ImageIcon loginIcon = new ImageIcon(getClass().getResource("login.png"));
- private JButton quitButton = new JButton("");
- private ImageIcon quitIcon = new ImageIcon(getClass().getResource("quit.png"));
- private ImageIcon dcIcon = new ImageIcon(getClass().getResource("dc.png"));
- /*
- private JButton registerButton = new JButton("");
- private ImageIcon regIcon = new ImageIcon(getClass().getResource("reg.png"));
- private ImageIcon unregIcon = new ImageIcon(getClass().getResource("unreg.png"));
- */
- private JButton debitButton = new JButton("");
- private ImageIcon debitIcon = new ImageIcon(getClass().getResource("debit.png"));
- private JButton creditButton = new JButton("");
- private ImageIcon creditIcon = new ImageIcon(getClass().getResource("credit.png"));
- private ImageIcon titleIcon = new ImageIcon(getClass().getResource("title.png"));
- private JLabel title = new JLabel("");
- private JLabel note = new JLabel("Welcome Screen");
- private JLabel status = new JLabel("Disconnected");
- private JLabel loginLabel = new JLabel("ID (7-Digit Number):");
- private JTextField loginField = new JTextField("");
- private JLabel hostLabel = new JLabel("Hostname:");
- private JTextField hostField = new JTextField("");
- private JLabel portLabel = new JLabel("port:");
- private JTextField portField = new JTextField("");
- private JLabel balanceLabel = new JLabel("---");
- private Font balanceFont = new Font("Dialog", Font.PLAIN, 20);
- private JCheckBox soundBox = new JCheckBox("Click Sounds");
- //Constructors ****************************************************
- /**
- * Constructs an instance of the ClientConsole UI.
- *
- * @param host The host to connect to.
- * @param port The port to connect on.
- */
- public ClientGUI(String host, int port)
- {
- super("University Account System");
- this.createClient(host, port);
- this.balanceLabel.setFont(this.balanceFont);
- this.hostField.setText(host);
- this.hostField.setPreferredSize(new Dimension(50,20));
- this.portField.setText(""+port);
- URL urlClickSound = ClientGUI.class.getResource("click.wav"); //get all the sound files
- this.clickSound = Applet.newAudioClip(urlClickSound);
- this.startButton.addActionListener(this);
- this.quitButton.addActionListener(this);
- //this.registerButton.addActionListener(this);
- this.loginButton.addActionListener(this);
- this.debitButton.addActionListener(this);
- this.creditButton.addActionListener(this);
- this.soundBox.setSelected(true);
- this.title.setIcon(titleIcon); // add title icon
- this.startButton.setMaximumSize(getPreferredSize()); //add icons to main menu buttons
- this.startButton.setIcon(startIcon);
- this.startButton.setContentAreaFilled(false);
- this.startButton.setBorderPainted(false);
- this.quitButton.setMaximumSize(getPreferredSize());
- this.quitButton.setIcon(quitIcon);
- this.quitButton.setContentAreaFilled(false);
- this.quitButton.setBorderPainted(false);
- /*
- this.registerButton.setMaximumSize(getPreferredSize());
- this.registerButton.setIcon(regIcon);
- this.registerButton.setContentAreaFilled(false);
- this.registerButton.setBorderPainted(false);
- */
- this.loginButton.setMaximumSize(getPreferredSize());
- this.loginButton.setIcon(loginIcon);
- this.loginButton.setContentAreaFilled(false);
- this.loginButton.setBorderPainted(false);
- this.debitButton.setMaximumSize(getPreferredSize());
- this.debitButton.setIcon(debitIcon);
- this.debitButton.setContentAreaFilled(false);
- this.debitButton.setBorderPainted(false);
- this.creditButton.setMaximumSize(getPreferredSize());
- this.creditButton.setIcon(creditIcon);
- this.creditButton.setContentAreaFilled(false);
- this.creditButton.setBorderPainted(false);
- this.pane.setLayout(new BorderLayout()); //set up the mainDisplay
- this.pane.add(this.statusBar, BorderLayout.NORTH);
- this.statusBar.add(this.note);
- this.statusBar.add(Box.createRigidArea(new Dimension(150,0)));
- this.statusBar.add(this.status);
- this.pane.add(this.mainDisplay, BorderLayout.CENTER);
- this.mainDisplay.setLayout(new BoxLayout(this.mainDisplay, BoxLayout.PAGE_AXIS));
- this.mainDisplay.add(Box.createRigidArea(new Dimension(0,20)));
- this.mainDisplay.add(this.title);
- this.mainDisplay.add(Box.createRigidArea(new Dimension(0,30)));
- this.mainDisplay.add(this.hostLabel);
- this.mainDisplay.add(this.hostField);
- this.mainDisplay.add(Box.createRigidArea(new Dimension(0,10)));
- this.mainDisplay.add(this.portLabel);
- this.mainDisplay.add(this.portField);
- this.mainDisplay.add(Box.createRigidArea(new Dimension(0,10)));
- this.mainDisplay.add(this.soundBox);
- this.mainDisplay.add(Box.createRigidArea(new Dimension(0,100)));
- this.pane.add("South", this.controlBar); //set up the control bar
- this.pane.add(this.controlBar, BorderLayout.SOUTH);
- this.controlBar.add(this.startButton);
- this.controlBar.add(this.quitButton);
- this.toggleSound(); //check whether sound should be on or off
- this.setSize (450,500);
- this.setResizable(false);
- this.setVisible(true);
- this.repaint();
- }
- private void createClient(String host, int port)
- {
- try
- {
- this.client = new Client(host, port, this);
- }
- catch (IOException e)
- {
- JOptionPane.showMessageDialog(null, "Failed to create client.", "Critical Error!", JOptionPane.ERROR_MESSAGE);
- }
- }
- @Override
- public void actionPerformed(ActionEvent e)
- {
- if (e.getSource() == this.startButton) //start (or continue) game
- {
- if (this.soundON == true)
- {
- this.clickSound.play();
- }
- this.client.handleMessageFromClientUI("#sethost "+this.hostField.getText());
- this.client.handleMessageFromClientUI("#setport "+this.portField.getText());
- this.client.handleMessageFromClientUI("#connect");
- if (this.client.isConnected())
- {
- this.running = true;
- this.toggleSound();
- this.status.setText("Connected. Not logged-in.");
- this.note.setText("Login Screen");
- this.quitButton.setIcon(this.dcIcon);
- this.mainDisplay.removeAll();
- this.controlBar.removeAll();
- this.mainDisplay.add(Box.createRigidArea(new Dimension(0,30)));
- this.mainDisplay.add(this.title);
- this.mainDisplay.add(Box.createRigidArea(new Dimension(0,20)));
- this.mainDisplay.add(this.loginLabel);
- this.mainDisplay.add(this.loginField);
- this.mainDisplay.add(Box.createRigidArea(new Dimension(0,110)));
- this.pane.add("South", this.controlBar); //set up the control bar
- this.pane.add(this.controlBar, BorderLayout.SOUTH);
- this.controlBar.add(this.loginButton);
- this.controlBar.add(this.quitButton);
- this.repaint();
- }
- else
- {
- JOptionPane.showMessageDialog(null, "Connection to Server Failed.", "alert", JOptionPane.ERROR_MESSAGE);
- }
- }
- else if (e.getSource() == this.quitButton)
- {
- if (this.soundON == true)
- {
- this.clickSound.play();
- }
- if (this.running == true) // if the game is running, it returns to the main menu
- {
- int exit = JOptionPane.showConfirmDialog(null, "Go back to Welcome Screen?", "Warning", JOptionPane.YES_NO_OPTION); //confirm
- if (exit == JOptionPane.YES_OPTION)
- {
- this.client.handleMessageFromClientUI("#disconnect");
- this.running = false;
- this.loginField.setText("");
- this.quitButton.setIcon(this.quitIcon);
- this.note.setText("Welcome Screen");
- this.status.setText("Disconnected");
- this.mainDisplay.removeAll();
- this.controlBar.removeAll();
- this.mainDisplay.add(Box.createRigidArea(new Dimension(0,20)));
- this.mainDisplay.add(this.title);
- this.mainDisplay.add(Box.createRigidArea(new Dimension(0,30)));
- this.mainDisplay.add(this.hostLabel);
- this.mainDisplay.add(this.hostField);
- this.mainDisplay.add(Box.createRigidArea(new Dimension(0,10)));
- this.mainDisplay.add(this.portLabel);
- this.mainDisplay.add(this.portField);
- this.mainDisplay.add(Box.createRigidArea(new Dimension(0,10)));
- this.mainDisplay.add(this.soundBox);
- this.mainDisplay.add(Box.createRigidArea(new Dimension(0,100)));
- this.pane.add("South", this.controlBar); //set up the control bar
- this.pane.add(this.controlBar, BorderLayout.SOUTH);
- this.controlBar.add(this.startButton);
- this.controlBar.add(this.quitButton);
- this.toggleSound(); //check whether sound should be on or off
- this.repaint();
- }
- }
- else //if user was on the menu, quit program
- {
- int exit = JOptionPane.showConfirmDialog(null, "Really quit?", "Warning", JOptionPane.YES_NO_OPTION);
- if (exit == JOptionPane.YES_OPTION)
- {
- this.client.handleMessageFromClientUI("#quit");
- System.exit(0);
- }
- }
- }
- /*
- else if (e.getSource() == this.registerButton)
- {
- if (this.soundON == true)
- {
- this.clickSound.play();
- }
- }
- */
- else if (e.getSource() == this.loginButton)
- {
- if (this.soundON == true)
- {
- this.clickSound.play();
- }
- this.client.handleMessageFromClientUI("#login "+this.loginField.getText());
- this.client.handleMessageFromClientUI("#balance");
- this.client.setLoginID(this.loginField.getText());
- if (this.loggedIn)
- {
- this.loginButton.setEnabled(true);
- this.note.setText("Account Screen");
- this.status.setText("Logged in to: "+this.client.getLoginID());
- this.mainDisplay.removeAll();
- this.controlBar.removeAll();
- this.mainDisplay.add(Box.createRigidArea(new Dimension(0,30)));
- this.mainDisplay.add(this.title);
- this.mainDisplay.add(Box.createRigidArea(new Dimension(0,50)));
- this.mainDisplay.add(Box.createRigidArea(new Dimension(100,0)));
- this.mainDisplay.add(this.balanceLabel);
- this.mainDisplay.add(Box.createRigidArea(new Dimension(0,110)));
- this.controlBar.add(debitButton);
- this.controlBar.add(creditButton);
- this.controlBar.add(quitButton);
- this.repaint();
- }
- else
- {
- JOptionPane.showMessageDialog(null, "Log-in Failed.", "alert", JOptionPane.ERROR_MESSAGE);
- }
- }
- else if (e.getSource() == this.debitButton)
- {
- String amount = JOptionPane.showInputDialog("Please input an amount to add");
- if (amount != null)
- {
- this.client.handleMessageFromClientUI("#debit "+amount);
- this.repaint();
- }
- }
- else if (e.getSource() == this.creditButton)
- {
- String amount = JOptionPane.showInputDialog("Please input an amount to remove");
- if (amount != null)
- {
- this.client.handleMessageFromClientUI("#credit "+amount);
- this.repaint();
- }
- }
- else if (e.getSource() == this.loginField)
- {
- }
- else if (e.getSource() == this.soundBox) //sound check-box
- {
- this.toggleSound();
- if (this.soundON == true)
- {
- this.clickSound.play();
- }
- }
- }
- public void loginScreen()
- {
- }
- @Override
- public void display(String msg)
- {
- if (msg.startsWith("@LOGIN"))
- {
- this.loggedIn = true;
- }
- else if (msg.startsWith("@BALANCE"))
- {
- this.balanceLabel.setText("Balance: $"+msg.substring(43));
- this.repaint();
- }
- else if (msg.startsWith("@ALERT"))
- {
- JOptionPane.showMessageDialog(null, msg.substring(7), "Server Message", JOptionPane.PLAIN_MESSAGE);
- }
- else
- {
- //JOptionPane.showMessageDialog(null, msg, "Server Message", JOptionPane.INFORMATION_MESSAGE); //For debugging
- }
- }
- public void toggleSound() //switches sound on or off
- {
- if (this.soundBox.isSelected() == true) //if the check box is checked
- {
- this.soundON = true;
- }
- else
- {
- this.soundON = false;
- }
- }
- public static void main(String[] args)
- {
- String host = ""; //the hostname
- int port = 0; //The port number
- try
- {
- host = args[0];
- }
- catch(ArrayIndexOutOfBoundsException e)
- {
- host = "localhost";
- }
- //Obtaining the port number from command line
- try
- {
- port = Integer.parseInt(args[1]);
- }
- catch(ArrayIndexOutOfBoundsException e)
- {
- port = DEFAULT_PORT;
- }
- ClientGUI gui = new ClientGUI(host,port);
- gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // allows you to hit the X button to close
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement