Advertisement
bbpgrs

Untitled

Nov 27th, 2014
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 16.07 KB | None | 0 0
  1. package client;
  2.  
  3. import common.UIIF;
  4. import java.applet.Applet;
  5. import java.applet.AudioClip;
  6. import java.awt.BorderLayout;
  7. import java.awt.Container;
  8. import java.awt.Dimension;
  9. import java.awt.event.*;
  10. import java.awt.Font;
  11. import java.net.URL;
  12. import javax.swing.*;
  13. import java.io.*;
  14.  
  15. public class ClientGUI extends JFrame implements UIIF, ActionListener
  16. {
  17.     //Class and Instance variables *************************************************
  18.  
  19.     /**
  20.      * The default port to connect on.
  21.      */
  22.     final public static int DEFAULT_PORT = 5555;
  23.    
  24.     final private static long serialVersionUID=0;  
  25.  
  26.     /**
  27.      * The instance of the client that created this GUI.
  28.      */
  29.     Client client;
  30.    
  31.     //GUI variables ******************************************************
  32.  
  33.     private boolean running = false;   
  34.     private boolean loggedIn = false;
  35.    
  36.     private boolean soundON = true;
  37.     private AudioClip clickSound;
  38.    
  39.     private Container pane = getContentPane();        //create main pane
  40.        
  41.     private JPanel mainDisplay = new JPanel();          //create sub panels
  42.     private JPanel controlBar = new JPanel();
  43.     private JPanel statusBar = new JPanel();
  44.    
  45.     //create all the buttons and labels and text fields (UI elements)
  46.     private JButton startButton = new JButton("");
  47.     private ImageIcon startIcon = new ImageIcon(getClass().getResource("start.png"));
  48.    
  49.     private JButton loginButton = new JButton("");  
  50.     private ImageIcon loginIcon = new ImageIcon(getClass().getResource("login.png"));
  51.    
  52.     private JButton quitButton = new JButton("");
  53.     private ImageIcon quitIcon = new ImageIcon(getClass().getResource("quit.png"));
  54.     private ImageIcon dcIcon = new ImageIcon(getClass().getResource("dc.png"));
  55.    
  56.     /*
  57.     private JButton registerButton = new JButton("");
  58.     private ImageIcon regIcon = new ImageIcon(getClass().getResource("reg.png"));
  59.     private ImageIcon unregIcon = new ImageIcon(getClass().getResource("unreg.png"));
  60.     */
  61.    
  62.     private JButton debitButton = new JButton("");
  63.     private ImageIcon debitIcon = new ImageIcon(getClass().getResource("debit.png"));
  64.    
  65.     private JButton creditButton = new JButton("");
  66.     private ImageIcon creditIcon = new ImageIcon(getClass().getResource("credit.png"));
  67.    
  68.     private ImageIcon titleIcon = new ImageIcon(getClass().getResource("title.png"));
  69.     private JLabel title = new JLabel("");    
  70.     private JLabel note = new JLabel("Welcome Screen");  
  71.     private JLabel status = new JLabel("Disconnected");
  72.    
  73.     private JLabel loginLabel = new JLabel("ID (7-Digit Number):");
  74.     private JTextField loginField = new JTextField("");    
  75.    
  76.     private JLabel hostLabel = new JLabel("Hostname:");
  77.     private JTextField hostField = new JTextField("");      
  78.    
  79.     private JLabel portLabel = new JLabel("port:");
  80.     private JTextField portField = new JTextField("");  
  81.    
  82.     private JLabel balanceLabel = new JLabel("---");
  83.     private Font balanceFont = new Font("Dialog", Font.PLAIN, 20);
  84.    
  85.     private JCheckBox soundBox = new JCheckBox("Click Sounds");
  86.  
  87.     //Constructors ****************************************************
  88.  
  89.     /**
  90.      * Constructs an instance of the ClientConsole UI.
  91.      *
  92.      * @param host The host to connect to.
  93.      * @param port The port to connect on.
  94.      */
  95.  
  96.     public ClientGUI(String host, int port)
  97.     {
  98.         super("University Account System");
  99.        
  100.         this.createClient(host, port); 
  101.                
  102.         this.balanceLabel.setFont(this.balanceFont);
  103.         this.hostField.setText(host);
  104.         this.hostField.setPreferredSize(new Dimension(50,20));     
  105.         this.portField.setText(""+port);       
  106.        
  107.         URL urlClickSound = ClientGUI.class.getResource("click.wav");  //get all the sound files
  108.         this.clickSound = Applet.newAudioClip(urlClickSound);
  109.        
  110.         this.startButton.addActionListener(this);
  111.         this.quitButton.addActionListener(this);
  112.         //this.registerButton.addActionListener(this);
  113.         this.loginButton.addActionListener(this);
  114.         this.debitButton.addActionListener(this);
  115.         this.creditButton.addActionListener(this);      
  116.              
  117.         this.soundBox.setSelected(true);
  118.         this.title.setIcon(titleIcon);    // add title icon
  119.        
  120.         this.startButton.setMaximumSize(getPreferredSize());      //add icons to main menu buttons
  121.         this.startButton.setIcon(startIcon);
  122.         this.startButton.setContentAreaFilled(false);
  123.         this.startButton.setBorderPainted(false);
  124.         this.quitButton.setMaximumSize(getPreferredSize());
  125.         this.quitButton.setIcon(quitIcon);
  126.         this.quitButton.setContentAreaFilled(false);
  127.         this.quitButton.setBorderPainted(false);
  128.         /*
  129.         this.registerButton.setMaximumSize(getPreferredSize());
  130.         this.registerButton.setIcon(regIcon);
  131.         this.registerButton.setContentAreaFilled(false);
  132.         this.registerButton.setBorderPainted(false);
  133.         */
  134.         this.loginButton.setMaximumSize(getPreferredSize());
  135.         this.loginButton.setIcon(loginIcon);
  136.         this.loginButton.setContentAreaFilled(false);
  137.         this.loginButton.setBorderPainted(false);  
  138.         this.debitButton.setMaximumSize(getPreferredSize());
  139.         this.debitButton.setIcon(debitIcon);
  140.         this.debitButton.setContentAreaFilled(false);
  141.         this.debitButton.setBorderPainted(false);
  142.         this.creditButton.setMaximumSize(getPreferredSize());
  143.         this.creditButton.setIcon(creditIcon);
  144.         this.creditButton.setContentAreaFilled(false);
  145.         this.creditButton.setBorderPainted(false);
  146.        
  147.         this.pane.setLayout(new BorderLayout());         //set up the mainDisplay
  148.        
  149.         this.pane.add(this.statusBar, BorderLayout.NORTH);
  150.         this.statusBar.add(this.note);
  151.         this.statusBar.add(Box.createRigidArea(new Dimension(150,0)));        
  152.         this.statusBar.add(this.status);
  153.        
  154.         this.pane.add(this.mainDisplay, BorderLayout.CENTER);          
  155.         this.mainDisplay.setLayout(new BoxLayout(this.mainDisplay, BoxLayout.PAGE_AXIS));        
  156.         this.mainDisplay.add(Box.createRigidArea(new Dimension(0,20)));
  157.         this.mainDisplay.add(this.title);
  158.         this.mainDisplay.add(Box.createRigidArea(new Dimension(0,30)));
  159.         this.mainDisplay.add(this.hostLabel);        
  160.         this.mainDisplay.add(this.hostField);
  161.         this.mainDisplay.add(Box.createRigidArea(new Dimension(0,10)));
  162.         this.mainDisplay.add(this.portLabel);
  163.         this.mainDisplay.add(this.portField);  
  164.         this.mainDisplay.add(Box.createRigidArea(new Dimension(0,10)));
  165.         this.mainDisplay.add(this.soundBox);
  166.         this.mainDisplay.add(Box.createRigidArea(new Dimension(0,100)));
  167.        
  168.         this.pane.add("South", this.controlBar);              //set up the control bar
  169.         this.pane.add(this.controlBar, BorderLayout.SOUTH);          
  170.         this.controlBar.add(this.startButton);
  171.         this.controlBar.add(this.quitButton);
  172.        
  173.         this.toggleSound();  //check whether sound should be on or off
  174.        
  175.         this.setSize (450,500);
  176.         this.setResizable(false);
  177.         this.setVisible(true);
  178.         this.repaint();
  179.     }
  180.    
  181.     private void createClient(String host, int port)
  182.     {
  183.         try
  184.         {
  185.             this.client = new Client(host, port, this);
  186.         }
  187.         catch (IOException e)
  188.         {
  189.             JOptionPane.showMessageDialog(null, "Failed to create client.", "Critical Error!", JOptionPane.ERROR_MESSAGE);
  190.         }
  191.     }
  192.    
  193.     @Override
  194.     public void actionPerformed(ActionEvent e)
  195.     {
  196.         if (e.getSource() == this.startButton) //start (or continue) game
  197.         {
  198.             if (this.soundON == true)
  199.             {
  200.                 this.clickSound.play();
  201.             }
  202.            
  203.             this.client.handleMessageFromClientUI("#sethost "+this.hostField.getText());
  204.             this.client.handleMessageFromClientUI("#setport "+this.portField.getText());
  205.             this.client.handleMessageFromClientUI("#connect");                                         
  206.                        
  207.             if (this.client.isConnected())
  208.             {          
  209.                 this.running = true;
  210.                 this.toggleSound();                            
  211.                
  212.                 this.status.setText("Connected. Not logged-in.");              
  213.                 this.note.setText("Login Screen");
  214.                 this.quitButton.setIcon(this.dcIcon);
  215.                
  216.                 this.mainDisplay.removeAll();
  217.                 this.controlBar.removeAll();
  218.                
  219.                 this.mainDisplay.add(Box.createRigidArea(new Dimension(0,30)));
  220.                 this.mainDisplay.add(this.title);                              
  221.                 this.mainDisplay.add(Box.createRigidArea(new Dimension(0,20)));
  222.                 this.mainDisplay.add(this.loginLabel);        
  223.                 this.mainDisplay.add(this.loginField);                                  
  224.                 this.mainDisplay.add(Box.createRigidArea(new Dimension(0,110)));
  225.                
  226.                 this.pane.add("South", this.controlBar);              //set up the control bar
  227.                 this.pane.add(this.controlBar, BorderLayout.SOUTH);          
  228.                 this.controlBar.add(this.loginButton);             
  229.                 this.controlBar.add(this.quitButton);
  230.                            
  231.                 this.repaint();  
  232.             }
  233.             else
  234.             {              
  235.                 JOptionPane.showMessageDialog(null, "Connection to Server Failed.", "alert", JOptionPane.ERROR_MESSAGE);
  236.             }
  237.         }
  238.         else if (e.getSource() == this.quitButton)
  239.         {          
  240.             if (this.soundON == true)
  241.             {
  242.                 this.clickSound.play();
  243.             }                      
  244.            
  245.             if (this.running == true) // if the game is running, it returns to the main menu
  246.             {
  247.                 int exit = JOptionPane.showConfirmDialog(null, "Go back to Welcome Screen?", "Warning", JOptionPane.YES_NO_OPTION);  //confirm          
  248.                 if (exit == JOptionPane.YES_OPTION)
  249.                 {      
  250.                     this.client.handleMessageFromClientUI("#disconnect");
  251.                    
  252.                     this.running = false;
  253.                    
  254.                     this.loginField.setText("");
  255.                     this.quitButton.setIcon(this.quitIcon);
  256.                     this.note.setText("Welcome Screen");
  257.                     this.status.setText("Disconnected");
  258.                    
  259.                     this.mainDisplay.removeAll();
  260.                     this.controlBar.removeAll();                                                          
  261.                    
  262.                     this.mainDisplay.add(Box.createRigidArea(new Dimension(0,20)));
  263.                     this.mainDisplay.add(this.title);
  264.                     this.mainDisplay.add(Box.createRigidArea(new Dimension(0,30)));
  265.                     this.mainDisplay.add(this.hostLabel);        
  266.                     this.mainDisplay.add(this.hostField);
  267.                     this.mainDisplay.add(Box.createRigidArea(new Dimension(0,10)));
  268.                     this.mainDisplay.add(this.portLabel);
  269.                     this.mainDisplay.add(this.portField);  
  270.                     this.mainDisplay.add(Box.createRigidArea(new Dimension(0,10)));
  271.                     this.mainDisplay.add(this.soundBox);
  272.                     this.mainDisplay.add(Box.createRigidArea(new Dimension(0,100)));
  273.                    
  274.                     this.pane.add("South", this.controlBar);              //set up the control bar
  275.                     this.pane.add(this.controlBar, BorderLayout.SOUTH);          
  276.                     this.controlBar.add(this.startButton);
  277.                     this.controlBar.add(this.quitButton);
  278.                    
  279.                     this.toggleSound();  //check whether sound should be on or off
  280.                                        
  281.                     this.repaint();
  282.                 }
  283.             }
  284.             else //if user was on the menu, quit program
  285.             {
  286.                 int exit = JOptionPane.showConfirmDialog(null, "Really quit?", "Warning", JOptionPane.YES_NO_OPTION);            
  287.                 if (exit == JOptionPane.YES_OPTION)
  288.                 {                                                              
  289.                     this.client.handleMessageFromClientUI("#quit");
  290.                     System.exit(0);                                                
  291.                 }  
  292.             }
  293.         }
  294.         /*
  295.         else if (e.getSource() == this.registerButton)
  296.         {
  297.             if (this.soundON == true)
  298.             {
  299.                 this.clickSound.play();
  300.             }
  301.         }
  302.         */
  303.         else if (e.getSource() == this.loginButton)
  304.         {              
  305.             if (this.soundON == true)
  306.             {
  307.                 this.clickSound.play();
  308.             }  
  309.                        
  310.             this.client.handleMessageFromClientUI("#login "+this.loginField.getText());
  311.             this.client.handleMessageFromClientUI("#balance");
  312.             this.client.setLoginID(this.loginField.getText());                     
  313.                        
  314.             if (this.loggedIn)
  315.             {
  316.                 this.loginButton.setEnabled(true);
  317.                
  318.                 this.note.setText("Account Screen");
  319.                 this.status.setText("Logged in to: "+this.client.getLoginID());            
  320.                
  321.                 this.mainDisplay.removeAll();          
  322.                 this.controlBar.removeAll();
  323.                
  324.                 this.mainDisplay.add(Box.createRigidArea(new Dimension(0,30)));
  325.                 this.mainDisplay.add(this.title);                              
  326.                 this.mainDisplay.add(Box.createRigidArea(new Dimension(0,50)));
  327.                 this.mainDisplay.add(Box.createRigidArea(new Dimension(100,0)));
  328.                 this.mainDisplay.add(this.balanceLabel);                                                         
  329.                 this.mainDisplay.add(Box.createRigidArea(new Dimension(0,110)));
  330.                
  331.                 this.controlBar.add(debitButton);
  332.                 this.controlBar.add(creditButton);
  333.                 this.controlBar.add(quitButton);
  334.                
  335.                 this.repaint();                
  336.             }
  337.             else
  338.             {
  339.                 JOptionPane.showMessageDialog(null, "Log-in Failed.", "alert", JOptionPane.ERROR_MESSAGE);
  340.             }
  341.         }
  342.         else if (e.getSource() == this.debitButton)
  343.         {
  344.             String amount = JOptionPane.showInputDialog("Please input an amount to add");
  345.             if (amount != null)
  346.             {
  347.                 this.client.handleMessageFromClientUI("#debit "+amount);
  348.                 this.repaint();
  349.             }
  350.         }
  351.         else if (e.getSource() == this.creditButton)
  352.         {  
  353.             String amount = JOptionPane.showInputDialog("Please input an amount to remove");
  354.             if (amount != null)
  355.             {
  356.                 this.client.handleMessageFromClientUI("#credit "+amount);
  357.                 this.repaint();
  358.             }
  359.         }
  360.         else if (e.getSource() == this.loginField)
  361.         {
  362.            
  363.         }
  364.         else if (e.getSource() == this.soundBox) //sound check-box
  365.         {                                      
  366.             this.toggleSound();
  367.            
  368.             if (this.soundON == true)
  369.             {
  370.                 this.clickSound.play();
  371.             }
  372.         }
  373.     }
  374.    
  375.     public void loginScreen()
  376.     {
  377.        
  378.     }
  379.    
  380.     @Override
  381.     public void display(String msg)
  382.     {      
  383.         if (msg.startsWith("@LOGIN"))
  384.         {
  385.             this.loggedIn = true;
  386.         }
  387.         else if (msg.startsWith("@BALANCE"))
  388.         {
  389.             this.balanceLabel.setText("Balance: $"+msg.substring(43));
  390.             this.repaint();
  391.         }
  392.         else if (msg.startsWith("@ALERT"))
  393.         {
  394.             JOptionPane.showMessageDialog(null, msg.substring(7), "Server Message", JOptionPane.PLAIN_MESSAGE);
  395.         }
  396.         else
  397.         {
  398.             //JOptionPane.showMessageDialog(null, msg, "Server Message", JOptionPane.INFORMATION_MESSAGE); //For debugging
  399.         }
  400.     }
  401.    
  402.     public void toggleSound() //switches sound on or off
  403.     {
  404.         if (this.soundBox.isSelected() == true) //if the check box is checked
  405.         {
  406.             this.soundON = true;            
  407.         }
  408.         else
  409.         {
  410.             this.soundON = false;          
  411.         }
  412.     }
  413.  
  414.     public static void main(String[] args)
  415.     {
  416.         String host = ""; //the hostname
  417.         int port = 0;  //The port number             
  418.        
  419.         try
  420.         {
  421.           host = args[0];        
  422.         }
  423.         catch(ArrayIndexOutOfBoundsException e)
  424.         {
  425.           host = "localhost";
  426.         }
  427.        
  428.         //Obtaining the port number from command line
  429.         try
  430.         {
  431.           port = Integer.parseInt(args[1]);      
  432.         }
  433.         catch(ArrayIndexOutOfBoundsException e)
  434.         {
  435.           port = DEFAULT_PORT;
  436.         }
  437.        
  438.         ClientGUI gui = new ClientGUI(host,port);
  439.         gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     // allows you to hit the X button to close
  440.     }
  441.  
  442. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement