import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; import java.awt.event.*; import java.io.IOException; import java.net.InetAddress; import java.net.UnknownHostException; /** * @author Ahsan Mussa * @version 1.2 */ /** This class obtains information from the GameAnalyser, Soccer Server and stats classes, to display information - commentary and stats. And will be the main form of interation the end user has with our system. */ public class GUI implements ActionListener{ // Initialize all swing objects. private JFrame f = new JFrame("RoboCup Commentary System"); //create Frame private JPanel pnlNorth = new JPanel(); // North quadrant private JPanel pnlSouth = new JPanel(); // South quadrant private JPanel pnlEast = new JPanel(); // East quadrant private JPanel pnlCenter = new JPanel(); // Center quadrant private JPanel pnlStatsT = new JPanel(); // Stats panel // Buttons some there is something to put in the panels private JButton connect = new JButton("Connect"); private JButton play = new JButton("Play"); private JButton replay = new JButton("Action Re-Play"); //Labels private JLabel lblNorth = new JLabel("Automated Commentaries for Simulated Soccer"); private JLabel lblEast = new JLabel("Statistics"); private JLabel dummy = new JLabel(); private JLabel team1 = new JLabel("Team1",JLabel.LEFT); private JLabel score = new JLabel("0:0",JLabel.CENTER); private JLabel team2 = new JLabel("Team2",JLabel.RIGHT); private JLabel posT1 = new JLabel("0%",JLabel.LEFT); private JLabel pos = new JLabel("Possesion",JLabel.CENTER); private JLabel posT2 = new JLabel("0%",JLabel.RIGHT); private JLabel sOnT1 = new JLabel("0",JLabel.LEFT); private JLabel sOt= new JLabel("Shots on target",JLabel.CENTER); private JLabel sOnT2 = new JLabel("0",JLabel.RIGHT); private JLabel sOffT1 = new JLabel("0",JLabel.LEFT); private JLabel sOfft= new JLabel("Shots off target",JLabel.CENTER); private JLabel sOffT2 = new JLabel("0",JLabel.RIGHT); private JLabel offsideT1 = new JLabel("0",JLabel.LEFT); private JLabel offside= new JLabel("Offside",JLabel.CENTER); private JLabel offsideT2 = new JLabel("0",JLabel.RIGHT); private JLabel conersT1 = new JLabel("0",JLabel.LEFT); private JLabel corners= new JLabel("Corners",JLabel.CENTER); private JLabel cornersT2 = new JLabel("0",JLabel.RIGHT); // Menu private JMenuBar mb = new JMenuBar(); // Menubar private JMenu mnuFile = new JMenu("File"); // File Entry on Menu bar private JMenuItem mnuItemQuit = new JMenuItem("Quit"); // Quit sub item private JMenu mnuHelp = new JMenu("Help"); // Help Menu entry private JMenuItem mnuItemAbout = new JMenuItem("About"); // About Entry private JTextArea textArea; public Soccerconnect soc; private void showAbout() { JOptionPane.showMessageDialog(f, "Project from 2006-2007 © \nGroup members in this project are: \nAdrien Martel, \nAhsan Mussa, \nAkbar Sherwani, \nJustin Hogg", "RoboCup Commentary System", JOptionPane.INFORMATION_MESSAGE); } /** Constructor for the GUI */ public GUI(){ // Set menubar f.setJMenuBar(mb); f.setSize(1500,300); // default size is 0,0 // Title lblNorth.setFont(new Font("Arial", Font.BOLD, 18)); lblNorth.setForeground(Color.blue); // Score score.setFont(new Font("Arial", Font.BOLD, 14)); score.setForeground(Color.blue); //Build Menus mnuFile.add(mnuItemQuit); // Create Quit line mnuHelp.add(mnuItemAbout); // Create About line mb.add(mnuFile); // Add Menu items to form mb.add(mnuHelp); //Logo ImageIcon logo = new ImageIcon("logo.jpg"); JLabel labelLogo = new JLabel(logo, JLabel.LEFT); //Stats panel layout pnlStatsT.setLayout(new GridLayout(0,3,7,7)); // Add widgets pnlNorth.add(labelLogo); pnlNorth.add(lblNorth); pnlSouth.add(connect); pnlSouth.add(play); pnlSouth.add(replay); pnlEast.add(lblEast); pnlCenter.add(dummy); pnlStatsT.add(team1); pnlStatsT.add(score); pnlStatsT.add(team2); pnlStatsT.add(posT1); pnlStatsT.add(pos); pnlStatsT.add(posT2); pnlStatsT.add(sOnT1); pnlStatsT.add(sOt); pnlStatsT.add(sOnT2); pnlStatsT.add(sOffT1); pnlStatsT.add(sOfft); pnlStatsT.add(sOffT2); pnlStatsT.add(offsideT1); pnlStatsT.add(offside); pnlStatsT.add(offsideT2); pnlStatsT.add(conersT1); pnlStatsT.add(corners); pnlStatsT.add(cornersT2); //Stats panel size and border pnlStatsT.setPreferredSize(new Dimension(300, 200)); pnlStatsT.setBorder( BorderFactory.createCompoundBorder( BorderFactory.createCompoundBorder( BorderFactory.createTitledBorder("Statistics"), BorderFactory.createEmptyBorder(5,5,5,5)), pnlStatsT.getBorder())); // Create a text area textArea = new JTextArea(); textArea.setEditable(false); textArea.setFont(new Font("Arial", Font.ITALIC, 14)); textArea.setForeground(Color.blue); textArea.setLineWrap(true); textArea.setWrapStyleWord(true); JScrollPane areaScrollPane = new JScrollPane(textArea); areaScrollPane.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); areaScrollPane.setPreferredSize(new Dimension(250, 200)); areaScrollPane.setBorder( BorderFactory.createCompoundBorder( BorderFactory.createCompoundBorder( BorderFactory.createTitledBorder("Commentary"), BorderFactory.createEmptyBorder(5,5,5,5)), areaScrollPane.getBorder())); JPanel rightPane = new JPanel(); rightPane.setLayout(new BorderLayout()); rightPane.add(pnlStatsT, BorderLayout.NORTH); // Setup Main Frame f.getContentPane().setLayout(new BorderLayout()); f.getContentPane().add(pnlNorth, BorderLayout.NORTH); f.getContentPane().add(pnlSouth, BorderLayout.SOUTH); f.getContentPane().add(rightPane, BorderLayout.EAST); f.getContentPane().add(areaScrollPane, BorderLayout.WEST); f.getContentPane().add(pnlCenter, BorderLayout.CENTER); // Allows the Swing App to be closed f.addWindowListener(new ListenCloseWdw()); // Button Listener connect.addActionListener(this); play.addActionListener(this); replay.addActionListener(this); mnuItemAbout.addActionListener(this); play.setEnabled(false); // Disable Action Re-play until half-time / goal score replay.setEnabled(false); //Add Menu listener mnuItemQuit.addActionListener(new ListenMenuQuit()); } public class ListenMenuQuit implements ActionListener{ public void actionPerformed(ActionEvent e){ System.exit(0); } } public class ListenCloseWdw extends WindowAdapter{ public void windowClosing(WindowEvent e){ System.exit(0); } } public void launchFrame(){ // Display Frame f.setResizable(false); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.pack(); //Adjusts panel to components for display f.setVisible(true); } public static void main(String args[]){ try { // Set cross-platform Java L&F (also called "Metal") UIManager.setLookAndFeel( "com.sun.java.swing.plaf.gtk.GTKLookAndFeel"); } catch (UnsupportedLookAndFeelException e) { // handle exception } catch (ClassNotFoundException e) { // handle exception } catch (InstantiationException e) { // handle exception } catch (IllegalAccessException e) { // handle exception } GUI gui = new GUI(); gui.launchFrame(); } public void actionPerformed(ActionEvent arg0) { String cmd = arg0.getActionCommand (); if(cmd.equals("Connect")) { String s = (String)JOptionPane.showInputDialog( f, "Enter IP/Hostname of Server", "Enter Server Address", JOptionPane.PLAIN_MESSAGE, null, null, "127.0.0.1"); try { InetAddress address = InetAddress.getByName(s); soc = new Soccerconnect(s, this); soc.start(); play.setEnabled(true); connect.setEnabled(false); } catch (UnknownHostException e) { JOptionPane.showMessageDialog( f, "Could not connect to the Server, please try again.", "Server Error", JOptionPane.WARNING_MESSAGE ); } } else if(cmd.equals("Play")) { soc.startGame(); } else if(cmd.equals("About")) { showAbout(); } } /** * @return the conersT1 */ public JLabel getConersT1() { return conersT1; } /** * @return the cornersT2 */ public JLabel getCornersT2() { return cornersT2; } /** * @return the offsideT1 */ public JLabel getOffsideT1() { return offsideT1; } /** * @return the offsideT2 */ public JLabel getOffsideT2() { return offsideT2; } /** * @return the posT1 */ public JLabel getPosT1() { return posT1; } /** * @return the posT2 */ public JLabel getPosT2() { return posT2; } /** * @return the sOffT1 */ public JLabel getSOffT1() { return sOffT1; } /** * @return the sOffT2 */ public JLabel getSOffT2() { return sOffT2; } /** * @return the sOnT1 */ public JLabel getSOnT1() { return sOnT1; } /** * @return the sOnT2 */ public JLabel getSOnT2() { return sOnT2; } /** * @return the team1 */ public JLabel getTeam1() { return team1; } /** * @return the team2 */ public JLabel getTeam2() { return team2; } public JLabel getScore(){ return score; } public JTextArea getTA(){ return textArea; } }