Java tutorial
/* * The MIT License (MIT) * * Copyright (c) 2014 Andrzej Ressel (jereksel@gmail.com) * Copyright (c) 2014 Mateusz Kaleciski * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package client.gui; import client.GlobalVariables; import client.ReceivingData; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; import java.util.Random; class ConnectionDialog extends JDialog implements ActionListener { private final JTextField nick; private final JTextField host; private final JTextField port; private final JButton connect; private final JButton connectDev; private final JButton connectSame; public ConnectionDialog() { connect = new JButton("Connect"); connectDev = new JButton("Connect (Developer)"); connectSame = new JButton("Connect (a as nick)"); nick = new JTextField(); host = new JTextField(); port = new JTextField(); setLayout(new GridBagLayout()); final GridBagConstraints c = new GridBagConstraints(); setSize(new Dimension(400, 200)); host.setPreferredSize(new Dimension(200, 24)); c.fill = GridBagConstraints.HORIZONTAL; c.weightx = 0.0; c.gridx = 0; c.gridy = 0; add(new JLabel("Nick:"), c); c.gridy = 1; add(new JLabel("Host:"), c); c.gridy = 2; add(new JLabel("Port:"), c); c.gridx = 1; c.gridy = 0; add(nick, c); c.gridy = 1; add(host, c); c.gridy = 2; add(port, c); c.gridx = 0; c.gridy = 3; c.gridwidth = 2; add(connect, c); // c.gridy = 4; // add(connectDev, c); // c.gridy = 5; // add(connectSame, c); connect.addActionListener(this); connectDev.addActionListener(this); connectSame.addActionListener(this); addWindowListener(new WindowAdapter() { public void windowClosing(final WindowEvent event) { dispose(); } }); final Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); this.setLocation(dim.width / 2 - this.getSize().width / 2, dim.height / 2 - this.getSize().height / 2); } public void actionPerformed(final ActionEvent event) { String nick = null; String host = null; int port = 0; if (event.getSource() == connect) { nick = this.nick.getText(); host = this.host.getText(); try { port = Integer.parseInt(this.port.getText()); } catch (NumberFormatException e1) { new MyDialog("Port number must be integer").setVisible(true); return; } } else if (event.getSource() == connectDev) { nick = generateNick(); host = "localhost"; port = 5555; } else if (event.getSource() == connectSame) { nick = "a"; host = "localhost"; port = 5555; } if (port <= 0) { new MyDialog("Port number must be bigger than 0").setVisible(true); return; } Socket socket; BufferedReader in; try { socket = new Socket(host, port); GlobalVariables.out = new PrintWriter(socket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); } catch (java.net.UnknownHostException e2) { new MyDialog("Unknown host").setVisible(true); return; } catch (IOException e3) { new MyDialog("Connection unsuccessful").setVisible(true); GlobalVariables.connected = false; return; } catch (java.lang.IllegalArgumentException e4) { new MyDialog("Port number is too big").setVisible(true); return; } System.out.println("Nick: " + nick); final JSONArray toSend = new JSONArray(); try { toSend.put(new JSONObject().put("action", "first_connection")); toSend.put(new JSONObject().put("nick", nick)); } catch (JSONException e1) { e1.printStackTrace(); } GlobalVariables.daemon = true; GlobalVariables.me.setNick(nick); GlobalVariables.connect.setEnabled(false); GlobalVariables.connected = true; setVisible(false); GlobalVariables.out.println(toSend); Thread thread; thread = new ReceivingData(in); thread.setDaemon(true); thread.start(); } private String generateNick() { char[] chars = "abcdefghijklmnopqrstuvwxyz".toCharArray(); final StringBuilder sb = new StringBuilder(); final Random random = new Random(); for (int i = 0; i < 6; i++) { char c = chars[random.nextInt(chars.length)]; sb.append(c); } return sb.toString(); } }