- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package boggleclient;
- /**
- *
- * @author Frog
- */
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import javax.swing.plaf.metal.*;
- import java.lang.Integer.*;
- import java.net.*;
- import java.io.*;
- public class BoggleClient extends JPanel implements ActionListener {
- static protected JTextField textField;
- //protected JButton enterButton;
- static protected JTextArea textArea;
- static protected Socket socket = null;
- static protected BufferedReader input = null;
- static protected BufferedWriter output = null;
- static String fromServer;
- // Specify the look and feel to use by defining the LOOKANDFEEL constant
- // Valid values are: null (use the default), "Metal", "System", "Motif",
- // and "GTK"
- final static String LOOKANDFEEL = "Metal";
- // If you choose the Metal L&F, you can also choose a theme.
- // Specify the theme to use by defining the THEME constant
- // Valid values are: "DefaultMetal", "Ocean", and [["Test"]] - Test is deprecated
- final static String THEME = "Ocean";
- public BoggleClient() {
- super(new GridBagLayout());
- textField = new JTextField(35);
- textField.addActionListener(this);
- //enterButton = new JButton("Out");
- textArea = new JTextArea(15, 35);
- textArea.setEditable(false);
- JScrollPane scrollPane = new JScrollPane(textArea);
- //Add Components to this panel.
- GridBagConstraints c = new GridBagConstraints();
- c.gridwidth = GridBagConstraints.REMAINDER;
- c.fill = GridBagConstraints.HORIZONTAL;
- add(textField, c);
- c.fill = GridBagConstraints.BOTH;
- c.weightx = 1.0;
- c.weighty = 1.0;
- add(scrollPane, c);
- }
- public void actionPerformed(ActionEvent evt) {
- String toServer = textField.getText();
- send(toServer);
- }
- public static void print(String output) {
- if (output == null) {
- return;
- }
- if (textArea.getText().equals("")) {
- textArea.append(output);
- } else {
- textArea.append("\n" + output);
- }
- // Make sure the new text is visible.
- textArea.setCaretPosition(textArea.getDocument().getLength());
- textField.selectAll();
- }
- private static void initLookAndFeel() {
- String lookAndFeel = null;
- if (LOOKANDFEEL != null) {
- if (LOOKANDFEEL.equals("Metal")) {
- lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName();
- // an alternative way to set the Metal L&F is to replace the
- // previous line with:
- // lookAndFeel = "javax.swing.plaf.metal.MetalLookAndFeel";
- }
- else if (LOOKANDFEEL.equals("System")) {
- lookAndFeel = UIManager.getSystemLookAndFeelClassName();
- }
- else if (LOOKANDFEEL.equals("Motif")) {
- lookAndFeel = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
- }
- else if (LOOKANDFEEL.equals("GTK")) {
- lookAndFeel = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
- }
- else {
- System.err.println("Unexpected value of LOOKANDFEEL specified: "
- + LOOKANDFEEL);
- lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName();
- }
- try {
- UIManager.setLookAndFeel(lookAndFeel);
- // If L&F = "Metal", set the theme
- if (LOOKANDFEEL.equals("Metal")) {
- if (THEME.equals("DefaultMetal"))
- MetalLookAndFeel.setCurrentTheme(new DefaultMetalTheme());
- else if (THEME.equals("Ocean"))
- MetalLookAndFeel.setCurrentTheme(new OceanTheme());
- //else
- //MetalLookAndFeel.setCurrentTheme(new TestTheme());
- UIManager.setLookAndFeel(new MetalLookAndFeel());
- }
- }
- catch (ClassNotFoundException e) {
- System.err.println("Couldn't find class for specified look and feel:"
- + lookAndFeel);
- System.err.println("Did you include the L&F library in the class path?");
- System.err.println("Using the default look and feel.");
- }
- catch (UnsupportedLookAndFeelException e) {
- System.err.println("Can't use the specified look and feel ("
- + lookAndFeel
- + ") on this platform.");
- System.err.println("Using the default look and feel.");
- }
- catch (Exception e) {
- System.err.println("Couldn't get specified look and feel ("
- + lookAndFeel
- + "), for some reason.");
- System.err.println("Using the default look and feel.");
- e.printStackTrace();
- }
- }
- }
- /**
- * Create the GUI and show it. For thread safety,
- * this method should be invoked from the
- * event dispatch thread.
- */
- private static void createAndShowGUI() throws IOException {
- //Set the look and feel.
- initLookAndFeel();
- //Make sure we have nice window decorations.
- JFrame.setDefaultLookAndFeelDecorated(true);
- //Create and set up the window.
- final JFrame frame = new JFrame("Boggle!");
- frame.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) throws IOException {
- input.close();
- output.close();
- socket.close();
- frame.dispose();
- System.exit(0);
- }
- });
- //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- //Add contents to the window.
- frame.add(new BoggleClient());
- //Display the window.
- frame.pack();
- frame.setVisible(true);
- }
- public static void main(String[] args) throws IOException {
- //Schedule a job for the event dispatch thread:
- //creating and showing this application's GUI.
- /*javax.swing.SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- createAndShowGUI();
- }
- });*/
- createAndShowGUI();
- setupSocket();
- }
- private static void setupSocket() {
- //Socket socket = null;
- //BufferedReader input = null;
- //BufferedWriter output = null;
- JOptionPane o = new JOptionPane();
- String host = "ancalagon.bethesdatech.net";
- int port = 14000;
- /*try {
- try {
- host = o.showInputDialog("Host name:");
- } catch (Exception e) {
- o.showMessageDialog(null, "Invalid host name.", "Error", JOptionPane.ERROR_MESSAGE);
- System.exit(1);
- }
- try {
- port = java.lang.Integer.parseInt(o.showInputDialog("Port:"));
- } catch (Exception e) {
- o.showMessageDialog(null, "Invalid port.", "Error", JOptionPane.ERROR_MESSAGE);
- System.exit(1);
- }
- } catch (Exception e) {
- o.showMessageDialog(null, "Something bad happened.", "Error", JOptionPane.ERROR_MESSAGE);
- System.exit(1);
- }*/
- try {
- socket = new Socket(host, port);
- input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- output = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
- String fromServer;
- while ((fromServer = input.readLine()) != null) {
- print(fromServer);
- }
- input.close();
- output.close();
- socket.close();
- } catch (UnknownHostException e) {
- o.showMessageDialog(null, "Could not acquire host: " + host, "Error", JOptionPane.ERROR_MESSAGE);
- System.exit(1);
- } catch (IOException e) {
- o.showMessageDialog(null, "Could not get IO for host: " + host, "Error", JOptionPane.ERROR_MESSAGE);
- System.exit(1);
- }
- }
- public void send(String toServer) {
- try {
- output.write(toServer);
- } catch (IOException e) {
- JOptionPane o = new JOptionPane();
- o.showMessageDialog(null, "Send error.", "Error", JOptionPane.ERROR_MESSAGE);
- }
- }
- }