Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 6th, 2012  |  syntax: None  |  size: 8.77 KB  |  hits: 20  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package boggleclient;
  7.  
  8. /**
  9.  *
  10.  * @author Frog
  11.  */
  12. import java.awt.*;
  13. import java.awt.event.*;
  14. import javax.swing.*;
  15. import javax.swing.plaf.metal.*;
  16.  
  17. import java.lang.Integer.*;
  18.  
  19. import java.net.*;
  20. import java.io.*;
  21.  
  22. public class BoggleClient extends JPanel implements ActionListener {
  23.     static protected JTextField textField;
  24.     //protected JButton enterButton;
  25.     static protected JTextArea textArea;
  26.  
  27.     static protected Socket socket = null;
  28.     static protected BufferedReader input = null;
  29.     static protected BufferedWriter output = null;
  30.     static String fromServer;
  31.  
  32.     // Specify the look and feel to use by defining the LOOKANDFEEL constant
  33.     // Valid values are: null (use the default), "Metal", "System", "Motif",
  34.     // and "GTK"
  35.     final static String LOOKANDFEEL = "Metal";
  36.  
  37.     // If you choose the Metal L&F, you can also choose a theme.
  38.     // Specify the theme to use by defining the THEME constant
  39.     // Valid values are: "DefaultMetal", "Ocean",  and [["Test"]] - Test is deprecated
  40.     final static String THEME = "Ocean";
  41.  
  42.     public BoggleClient() {
  43.         super(new GridBagLayout());
  44.  
  45.         textField = new JTextField(35);
  46.         textField.addActionListener(this);
  47.  
  48.         //enterButton = new JButton("Out");
  49.  
  50.         textArea = new JTextArea(15, 35);
  51.         textArea.setEditable(false);
  52.         JScrollPane scrollPane = new JScrollPane(textArea);
  53.  
  54.         //Add Components to this panel.
  55.         GridBagConstraints c = new GridBagConstraints();
  56.         c.gridwidth = GridBagConstraints.REMAINDER;
  57.  
  58.         c.fill = GridBagConstraints.HORIZONTAL;
  59.         add(textField, c);
  60.  
  61.         c.fill = GridBagConstraints.BOTH;
  62.         c.weightx = 1.0;
  63.         c.weighty = 1.0;
  64.         add(scrollPane, c);
  65.     }
  66.  
  67.     public void actionPerformed(ActionEvent evt) {
  68.         String toServer = textField.getText();
  69.  
  70.         send(toServer);
  71.     }
  72.  
  73.     public static void print(String output) {
  74.         if (output == null) {
  75.             return;
  76.         }
  77.         if (textArea.getText().equals("")) {
  78.             textArea.append(output);
  79.         } else {
  80.             textArea.append("\n" + output);
  81.         }
  82.  
  83.         // Make sure the new text is visible.
  84.         textArea.setCaretPosition(textArea.getDocument().getLength());
  85.  
  86.         textField.selectAll();
  87.     }
  88.  
  89.     private static void initLookAndFeel() {
  90.         String lookAndFeel = null;
  91.  
  92.         if (LOOKANDFEEL != null) {
  93.             if (LOOKANDFEEL.equals("Metal")) {
  94.                 lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName();
  95.               //  an alternative way to set the Metal L&F is to replace the
  96.               // previous line with:
  97.               // lookAndFeel = "javax.swing.plaf.metal.MetalLookAndFeel";
  98.  
  99.             }
  100.  
  101.             else if (LOOKANDFEEL.equals("System")) {
  102.                 lookAndFeel = UIManager.getSystemLookAndFeelClassName();
  103.             }
  104.  
  105.             else if (LOOKANDFEEL.equals("Motif")) {
  106.                 lookAndFeel = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
  107.             }
  108.  
  109.             else if (LOOKANDFEEL.equals("GTK")) {
  110.                 lookAndFeel = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
  111.             }
  112.  
  113.             else {
  114.                 System.err.println("Unexpected value of LOOKANDFEEL specified: "
  115.                                    + LOOKANDFEEL);
  116.                 lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName();
  117.             }
  118.  
  119.             try {
  120.                 UIManager.setLookAndFeel(lookAndFeel);
  121.  
  122.                 // If L&F = "Metal", set the theme
  123.  
  124.                 if (LOOKANDFEEL.equals("Metal")) {
  125.                   if (THEME.equals("DefaultMetal"))
  126.                      MetalLookAndFeel.setCurrentTheme(new DefaultMetalTheme());
  127.                   else if (THEME.equals("Ocean"))
  128.                      MetalLookAndFeel.setCurrentTheme(new OceanTheme());
  129.                   //else
  130.                      //MetalLookAndFeel.setCurrentTheme(new TestTheme());
  131.  
  132.                   UIManager.setLookAndFeel(new MetalLookAndFeel());
  133.                 }
  134.             }
  135.  
  136.             catch (ClassNotFoundException e) {
  137.                 System.err.println("Couldn't find class for specified look and feel:"
  138.                                    + lookAndFeel);
  139.                 System.err.println("Did you include the L&F library in the class path?");
  140.                 System.err.println("Using the default look and feel.");
  141.             }
  142.  
  143.             catch (UnsupportedLookAndFeelException e) {
  144.                 System.err.println("Can't use the specified look and feel ("
  145.                                    + lookAndFeel
  146.                                    + ") on this platform.");
  147.                 System.err.println("Using the default look and feel.");
  148.             }
  149.  
  150.             catch (Exception e) {
  151.                 System.err.println("Couldn't get specified look and feel ("
  152.                                    + lookAndFeel
  153.                                    + "), for some reason.");
  154.                 System.err.println("Using the default look and feel.");
  155.                 e.printStackTrace();
  156.             }
  157.         }
  158.     }
  159.  
  160.     /**
  161.      * Create the GUI and show it.  For thread safety,
  162.      * this method should be invoked from the
  163.      * event dispatch thread.
  164.      */
  165.     private static void createAndShowGUI() throws IOException {
  166.         //Set the look and feel.
  167.         initLookAndFeel();
  168.  
  169.         //Make sure we have nice window decorations.
  170.         JFrame.setDefaultLookAndFeelDecorated(true);
  171.  
  172.         //Create and set up the window.
  173.         final JFrame frame = new JFrame("Boggle!");
  174.         frame.addWindowListener(new WindowAdapter() {
  175.             public void windowClosing(WindowEvent e) throws IOException {
  176.                 input.close();
  177.                 output.close();
  178.                 socket.close();
  179.  
  180.                 frame.dispose();
  181.                 System.exit(0);
  182.             }
  183.         });
  184.         //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  185.  
  186.         //Add contents to the window.
  187.         frame.add(new BoggleClient());
  188.  
  189.         //Display the window.
  190.         frame.pack();
  191.         frame.setVisible(true);
  192.     }
  193.  
  194.     public static void main(String[] args) throws IOException {
  195.         //Schedule a job for the event dispatch thread:
  196.         //creating and showing this application's GUI.
  197.         /*javax.swing.SwingUtilities.invokeLater(new Runnable() {
  198.             public void run() {
  199.                 createAndShowGUI();
  200.             }
  201.         });*/
  202.         createAndShowGUI();
  203.         setupSocket();
  204.     }
  205.  
  206.     private static void setupSocket() {
  207.         //Socket socket = null;
  208.         //BufferedReader input = null;
  209.         //BufferedWriter output = null;
  210.         JOptionPane o = new JOptionPane();
  211.         String host = "ancalagon.bethesdatech.net";
  212.         int port = 14000;
  213.         /*try {
  214.             try {
  215.                 host = o.showInputDialog("Host name:");
  216.             } catch (Exception e) {
  217.                 o.showMessageDialog(null, "Invalid host name.", "Error", JOptionPane.ERROR_MESSAGE);
  218.                 System.exit(1);
  219.             }
  220.             try {
  221.                 port = java.lang.Integer.parseInt(o.showInputDialog("Port:"));
  222.             } catch (Exception e) {
  223.                 o.showMessageDialog(null, "Invalid port.", "Error", JOptionPane.ERROR_MESSAGE);
  224.                 System.exit(1);
  225.             }
  226.         } catch (Exception e) {
  227.             o.showMessageDialog(null, "Something bad happened.", "Error", JOptionPane.ERROR_MESSAGE);
  228.             System.exit(1);
  229.         }*/
  230.  
  231.         try {
  232.             socket = new Socket(host, port);
  233.             input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  234.             output = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
  235.  
  236.             String fromServer;
  237.  
  238.            while ((fromServer = input.readLine()) != null) {
  239.                 print(fromServer);
  240.            }
  241.  
  242.            input.close();
  243.            output.close();
  244.            socket.close();
  245.         } catch (UnknownHostException e) {
  246.             o.showMessageDialog(null, "Could not acquire host: " + host, "Error", JOptionPane.ERROR_MESSAGE);
  247.             System.exit(1);
  248.         } catch (IOException e) {
  249.             o.showMessageDialog(null, "Could not get IO for host: " + host, "Error", JOptionPane.ERROR_MESSAGE);
  250.             System.exit(1);
  251.         }
  252.     }
  253.  
  254.     public void send(String toServer) {
  255.         try {
  256.             output.write(toServer);
  257.         } catch (IOException e) {
  258.             JOptionPane o = new JOptionPane();
  259.             o.showMessageDialog(null, "Send error.", "Error", JOptionPane.ERROR_MESSAGE);
  260.         }
  261.     }
  262. }