I'm writing a Java IRC client and I have a problem in that, whenever the
program connects to an IRC server the graphics freezes. Someone said that I
needed to use threads using invokeLater so I came up with this...
//Smeg - Java IRC Client
//Written by Darrell Blake and released under the GNU GPL license. See
COPYING for more information on this license.
import java.awt.*;
import java.awt.event. *;
import javax.swing.*;
import java.io.*;
import java.net.*;
public class Smeg extends JFrame implements ActionListener
{
private JButton send;
private static JTextArea chatWindow;
private JTextField chatBox;
private static Container pane;
private static InetAddress serverName;
private static int connectionPort;
private static Socket link;
private static PrintWriter out;
private static BufferedReader in;
private String progName = "Smeg";
private String[] cmdArgs;
private String progVersion = "0.01";
private static String nick;
private static String realname;
private boolean connected;
public static void main(String[] args) throws IOException
{
Smeg SmegMain = new Smeg();
SmegMain.setSiz e(600,600);
SmegMain.setVis ible(true);
serverName = InetAddress.get ByName("irc.fre enode.net"); //Set server
defaults.
connectionPort = 6667;
nick = "SmegNick";
realname = "SmegName";
}
public Smeg()
{
connected = false;
pane = getContentPane( );
cmdArgs = new String[15];
setTitle(progNa me + " v" + progVersion);
pane.setLayout( new FlowLayout());
send = new JButton("Send") ;
send.addActionL istener(this);
chatWindow = new JTextArea(35,52 );
chatWindow.setW rapStyleWord(tr ue);
chatWindow.setL ineWrap(true);
chatWindow.setE ditable(false);
chatWindow.setT ext("Welcome to " + progName + " v" + progVersion + "!\
\n");
chatBox = new JTextField(45);
chatBox.addActi onListener(this );
pane.add(new JScrollPane(cha tWindow));
pane.add(chatBo x);
pane.add(send);
this.addWindowL istener(
new WindowAdapter()
{
public void windowClosing(W indowEvent e)
{
System.exit(0);
}
}
);
}
public void actionPerformed (ActionEvent e)
{
String message;
message = chatBox.getText ();
chatBox.setText ("");
if (!message.start sWith("/"))
chatWindow.setT ext(chatWindow. getText() + message + "\n");
else
{
cmdArgs = message.split(" ");
if (cmdArgs[0].equals("/server"))
SwingUtilities. invokeLater(new Runnable()
{
public void run() {
cmdServer();
}
});
else
JOptionPane.sho wMessageDialog( pane, "Unknown command!");
}
}
public void cmdServer()
{
String response;
try
{
serverName = InetAddress.get ByName(cmdArgs[1]);
if (cmdArgs.length > 2)
connectionPort = Integer.parseIn t(cmdArgs[2]);
System.out.prin tln("Connecting to server " + serverName + " on port " +
Integer.toStrin g(connectionPor t) + "\n");
try
{
link = new Socket(serverNa me, connectionPort) ;
in = new BufferedReader
(new InputStreamRead er
(link.getInputS tream()));
out = new PrintWriter(
link.getOutputS tream(),true);
out.println("US ER " + nick + " +iw " + nick + " :" + realname);
out.println("NI CK " + nick);
out.println("US ERHOST " + nick);
out.println("MO DE " + nick + " +iw");
while(true)
{
response = in.readLine();
System.out.prin tln(response);
chatWindow.setT ext(chatWindow. getText() + response + "\n");
}
}
catch(IOExcepti on ex)
{
ex.printStackTr ace();
}
}
catch (UnknownHostExc eption ex)
{
JOptionPane.sho wMessageDialog( pane, "Server not found!");
}
}
public void cmdJoin()
{
}
public void cmdPart()
{
}
}
The graphics is still freezing though. I've even tried sticking the GUI in a
method on it's own and running that from main in an invokeLater but the
graphics still freezes when I try to connect to a server. Can someone
please take a look at my code and tell me what I'm doing wrong?
Darrell
program connects to an IRC server the graphics freezes. Someone said that I
needed to use threads using invokeLater so I came up with this...
//Smeg - Java IRC Client
//Written by Darrell Blake and released under the GNU GPL license. See
COPYING for more information on this license.
import java.awt.*;
import java.awt.event. *;
import javax.swing.*;
import java.io.*;
import java.net.*;
public class Smeg extends JFrame implements ActionListener
{
private JButton send;
private static JTextArea chatWindow;
private JTextField chatBox;
private static Container pane;
private static InetAddress serverName;
private static int connectionPort;
private static Socket link;
private static PrintWriter out;
private static BufferedReader in;
private String progName = "Smeg";
private String[] cmdArgs;
private String progVersion = "0.01";
private static String nick;
private static String realname;
private boolean connected;
public static void main(String[] args) throws IOException
{
Smeg SmegMain = new Smeg();
SmegMain.setSiz e(600,600);
SmegMain.setVis ible(true);
serverName = InetAddress.get ByName("irc.fre enode.net"); //Set server
defaults.
connectionPort = 6667;
nick = "SmegNick";
realname = "SmegName";
}
public Smeg()
{
connected = false;
pane = getContentPane( );
cmdArgs = new String[15];
setTitle(progNa me + " v" + progVersion);
pane.setLayout( new FlowLayout());
send = new JButton("Send") ;
send.addActionL istener(this);
chatWindow = new JTextArea(35,52 );
chatWindow.setW rapStyleWord(tr ue);
chatWindow.setL ineWrap(true);
chatWindow.setE ditable(false);
chatWindow.setT ext("Welcome to " + progName + " v" + progVersion + "!\
\n");
chatBox = new JTextField(45);
chatBox.addActi onListener(this );
pane.add(new JScrollPane(cha tWindow));
pane.add(chatBo x);
pane.add(send);
this.addWindowL istener(
new WindowAdapter()
{
public void windowClosing(W indowEvent e)
{
System.exit(0);
}
}
);
}
public void actionPerformed (ActionEvent e)
{
String message;
message = chatBox.getText ();
chatBox.setText ("");
if (!message.start sWith("/"))
chatWindow.setT ext(chatWindow. getText() + message + "\n");
else
{
cmdArgs = message.split(" ");
if (cmdArgs[0].equals("/server"))
SwingUtilities. invokeLater(new Runnable()
{
public void run() {
cmdServer();
}
});
else
JOptionPane.sho wMessageDialog( pane, "Unknown command!");
}
}
public void cmdServer()
{
String response;
try
{
serverName = InetAddress.get ByName(cmdArgs[1]);
if (cmdArgs.length > 2)
connectionPort = Integer.parseIn t(cmdArgs[2]);
System.out.prin tln("Connecting to server " + serverName + " on port " +
Integer.toStrin g(connectionPor t) + "\n");
try
{
link = new Socket(serverNa me, connectionPort) ;
in = new BufferedReader
(new InputStreamRead er
(link.getInputS tream()));
out = new PrintWriter(
link.getOutputS tream(),true);
out.println("US ER " + nick + " +iw " + nick + " :" + realname);
out.println("NI CK " + nick);
out.println("US ERHOST " + nick);
out.println("MO DE " + nick + " +iw");
while(true)
{
response = in.readLine();
System.out.prin tln(response);
chatWindow.setT ext(chatWindow. getText() + response + "\n");
}
}
catch(IOExcepti on ex)
{
ex.printStackTr ace();
}
}
catch (UnknownHostExc eption ex)
{
JOptionPane.sho wMessageDialog( pane, "Server not found!");
}
}
public void cmdJoin()
{
}
public void cmdPart()
{
}
}
The graphics is still freezing though. I've even tried sticking the GUI in a
method on it's own and running that from main in an invokeLater but the
graphics still freezes when I try to connect to a server. Can someone
please take a look at my code and tell me what I'm doing wrong?
Darrell
Comment