Java Chat Client Sample Applet
By: Rajan Printer Friendly Format
This applet can be used to write a Java Chat Client Application. For thisChat client to work, you need to create a HTML file and embed this applet code inside.
First create the following HTML file and add this code inside the Body tag.
<code> <param name="host" value="naken.inlink.com">
<script>
document.write('<applet code="chatclient.class" height=400
width=600>');
n=navigator.appVersion;
n=n.substring(0,n.indexOf(' '));
if (navigator.appName.indexOf("Microsoft")==0)
{ document.write("<param name='java11' value='0'>"); }
else
if (n<4.06) { document.write("<param name='java11'
value='0'>"); }
</script>
<param name="port" value="6666">
<param name="channel1" value="Linux Talk">
<param name="channel2" value="FreeBSD">
<param name="channel3" value="Windows Room">
<param name="font" value="MonoSpaced">
<param name="fontsize" value="13">
Java Chat Client Sample Code
import java.awt.*;
import java.applet.*;
import java.net.*;
import java.lang.*;
import java.io.*;
public class chatclient extends Applet implements Runnable
{
Scrollbar vertScroll;
DisplayArea myDisplay;
Thread myThread;
Socket mySocket;
InputStream myInputStream;
BufferedInputStream myBuffInput;
OutputStream myOutputStream;
Graphics gg;
TextField messageText,channelText,nameText;
Button LogButt;
int Online=0;
Checkbox echoBox,beepsBox,yellsBox;
Choice bgChoice,chanChoice,fontsizeChoice,fontChoice;
String YourName="";
int java11=1;
public void init()
{
Panel NorthPanel=new Panel();
Panel SouthPanel=new Panel();
Panel EastPanel=new Panel();
Panel CenterPanel=new Panel();
int r=0;
Label chanLabel;
NorthPanel.setBackground(Color.lightGray);
SouthPanel.setBackground(Color.lightGray);
EastPanel.setBackground(Color.lightGray);
CenterPanel.setBackground(Color.lightGray);
setLayout(new BorderLayout());
NorthPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
SouthPanel.setLayout(new BorderLayout());
SouthPanel.add("West",new Label("Message:"));
SouthPanel.add("Center",messageText=new TextField());
NorthPanel.add(chanLabel=new Label("Channel:",Label.RIGHT));
NorthPanel.add(chanChoice=new Choice());
NorthPanel.add(channelText=new TextField("Main",20));
NorthPanel.add(new Label("Name:",Label.RIGHT));
NorthPanel.add(nameText=new TextField("",15));
chanChoice.addItem("---->");
chanChoice.addItem("Main");
chanChoice.addItem("Masquerade");
r=1;
while(getParameter("channel"+r)!=null)
{ chanChoice.addItem(getParameter("channel"+(r++))); }
EastPanel.setLayout(new GridLayout(13,1));
EastPanel.add(new Label("Background Color"));
EastPanel.add(bgChoice=new Choice());
EastPanel.add(new Label("Font"));
EastPanel.add(fontChoice=new Choice());
EastPanel.add(new Label("Font Size"));
EastPanel.add(fontsizeChoice=new Choice());
EastPanel.add(echoBox=new Checkbox("Private Echos",null,false));
EastPanel.add(yellsBox=new Checkbox("Hush Yells",null,false));
EastPanel.add(new Button("Who is On"));
EastPanel.add(new Button("Finger"));
EastPanel.add(new Button("List Channels"));
EastPanel.add(new Button("Help"));
EastPanel.add(LogButt=new Button("Logoff"));
bgChoice.addItem("Black");
bgChoice.addItem("White");
fontChoice.addItem("Dialog");
fontChoice.addItem("DialogInput");
fontChoice.addItem("Helvetica");
fontChoice.addItem("TimesRoman");
fontChoice.addItem("Courier");
fontChoice.addItem("Serif");
fontChoice.addItem("SansSerif");
fontChoice.addItem("Monospaced");
for (r=6; r<20; r++) { fontsizeChoice.addItem(""+r); }
CenterPanel.setLayout(new BorderLayout());
CenterPanel.add("Center",myDisplay=new DisplayArea());
CenterPanel.add("East",
vertScroll=new Scrollbar(Scrollbar.VERTICAL,100,100,0,100));
vertScroll.show();
add("South",SouthPanel);
add("North",NorthPanel);
add("East",EastPanel);
add("Center",CenterPanel);
if (getParameter("fontsize")!=null)
{ myDisplay.fontsize=Integer.parseInt(getParameter("fontsize")); }
else
{ myDisplay.fontsize=13; }
if (getParameter("java11")!=null)
{ java11=Integer.parseInt(getParameter("java11")); }
if (getParameter("font")!=null)
{ myDisplay.font=getParameter("font"); }
else
{ myDisplay.font="Dialog"; }
myDisplay.setup(vertScroll.size().width,java11);
// System.out.println(java11);
myDisplay.myBar=vertScroll;
gg=myDisplay.getGraphics();
fontsizeChoice.select(""+myDisplay.fontsize);
fontChoice.select(myDisplay.font);
}
public void start()
{
int l;
l=logon();
if (l==0)
{
myThread=new Thread(this);
myThread.start();
}
}
public int logon()
{
try
{
mySocket=new Socket(getParameter("host"),Integer.parseInt(getParameter("port")));
myInputStream=mySocket.getInputStream();
myBuffInput=new BufferedInputStream(myInputStream,2048);
myOutputStream=mySocket.getOutputStream();
Online=1;
}
catch (Exception e) { Online=0; }
if (Online==1) return 0;
return 1;
}
public void run()
{
byte[] in=new byte[2048];
int r,c=0;
String myString="";
if (Online==1)
{
if (getParameter("YourName")!=null)
{
nameText.setText(getParameter("YourName"));
try
{ myOutputStream.write(convb(".n"+getParameter("YourName")+"\n")); }
catch (Exception e) {}
YourName=getParameter("YourName");
}
}
while(true)
{
if (Online==1)
{
try { c=myBuffInput.read(in,0,2048); } catch (Exception e) { Online=0; }
for(r=0; r> Private beeps are now on."); }
else
{ myDisplay.AddText(">> Private beeps are now off."); }
}
else
if (message.startsWith(".e"))
{ echoBox.setState(!echoBox.getState()); }
else
if (message.startsWith(".h"))
{
message="";
myDisplay.AddText(">> This is automatic here.");
}
myOutputStream.write(convb(message+"\n"));
messageText.setText("");
}
else
if (e.target==nameText)
{
if (!nameText.getText().equals(YourName))
myOutputStream.write(convb(".n"+nameText.getText()+"\n"));
YourName=nameText.getText();
}
else
if (e.target==chanChoice)
{
if (chanChoice.getSelectedIndex()!=0)
{
myOutputStream.write(convb(".c"+chanChoice.getSelectedItem()+"\n"));
channelText.setText(chanChoice.getSelectedItem());
chanChoice.select(0);
}
}
else
if (obj.equals("Who is On"))
{ myOutputStream.write(convb(".w\n")); }
else
if (obj.equals("Finger"))
{ myOutputStream.write(convb(".f\n")); }
else
if (obj.equals("List Channels"))
{ myOutputStream.write(convb(".a\n")); }
else
if (obj.equals("Help"))
{ myOutputStream.write(convb(".he\n")); }
else
if (obj.equals("Logoff"))
{
myOutputStream.write(convb(".q\n"));
Online=0;
LogButt.setLabel("Log On");
}
else
if (obj.equals("Log On"))
{
logon();
LogButt.setLabel("Logoff");
}
else
if (e.target.toString().indexOf("Private Beeps")>=0)
{
myDisplay.beeps=myDisplay.beeps^1;
if (myDisplay.beeps==1)
{ myDisplay.AddText(">> Private beeps are now on."); }
else
{ myDisplay.AddText(">> Private beeps are now off."); }
}
else
if (e.target.toString().indexOf("Hush Yells")>=0)
{ myOutputStream.write(convb(".hu\n")); }
else
if (e.target.toString().indexOf("Private Echos")>=0)
{ myOutputStream.write(convb(".e\n")); }
else
if (e.target.toString().indexOf("Back To Main")>=0)
{
myOutputStream.write(convb(".c0\n"));
channelText.setText("Main");
}
else
if (e.target==fontsizeChoice)
{
myDisplay.fontsize=Integer.parseInt(fontsizeChoice.getSelectedItem());
myDisplay.recalc();
myDisplay.paint(gg);
}
else
if (e.target==fontChoice)
{
myDisplay.font=fontChoice.getSelectedItem();
myDisplay.recalc();
myDisplay.paint(gg);
}
else
if (obj.equals("Black"))
{
myDisplay.fgColor=Color.white;
myDisplay.bgColor=Color.black;
myDisplay.paint(gg);
}
else
if (obj.equals("White"))
{
myDisplay.fgColor=Color.black;
myDisplay.bgColor=Color.white;
myDisplay.paint(gg);
}
}
catch (Exception ex) {}
return false;
}
byte[] convb(String myString)
{
byte[] myBytes=new byte[2048];
int r;
for(r=0; r0)
{
if (myString.charAt(0)=='>') sflag=1;
else
if (myString.charAt(0)=='<')
{
sflag=2;
if (beeps!=0) System.out.println((char)7);
}
else
if (myString.charAt(0)=='#') sflag=3;
else
if (myString.charAt(0)=='-') sflag=4;
}
while(myString.length()>0)
{
if (fm.stringWidth(myString)=0 &&
fm.stringWidth(myString.substring(0,r))>d.width-scrollbarSize)
{ r--; }
spc=0; last=0;
while((spc=myString.indexOf(' ',spc+1))0) last=spc;
if (last<=0) { last=r; }
AddLine(myString.substring(0,last),sflag);
myString=myString.substring(last,myString.length()).trim();
}
}
public void AddLine(String myString,byte flags)
{
BFlags[topptr]=flags;
Buffer[topptr++]=myString;
if(topptr==max) topptr=0;
ScrollRecalc(0);
}
public void ScrollRecalc(int i)
{
int scrollvis=max;
int value=0;
/* recalculate scrollbar size */
/* max is maximum number of lines */
/* bsize the current number of text lines in buffer (including on screen) */
/* lastbsize the last bsize */
/* lines is the number of visible text lines on the screen */
/* here */
if (bsizelines)
{
scrollvis=bsize-lines;
value=bsize-lines;
}
else
{
/* beginning state */
scrollvis=1;
value=1;
}
if (java11==1)
{ myBar.setValues(bsize,lines,0,bsize); }
else
{ myBar.setValues(value,scrollvis*2,1,value); }
scrollval=myBar.getValue();
}
else
{
if (i==1)
{
value=bsize-lines;
scrollvis=bsize-lines;
if (java11==1)
{ myBar.setValues(bsize,lines,0,bsize); }
else
{ myBar.setValues(value,scrollvis*2,1,value); }
}
else
{ myBar.setValue(bsize); }
}
scrollval=bsize;
}
public void paint(Graphics ggReal)
{
Dimension d;
int r,l;
int ptr,offset;
d=this.size();
if (OffScreen==null)
{
OffScreen=createImage(d.width,d.height);
gg=OffScreen.getGraphics();
}
gg.setColor(bgColor);
gg.setFont(myFont);
gg.fillRect(0,0,d.width,d.height);
l=0;
lines=d.height/spacing;
if (bsize<=lines) { offset=0; }
else
{ offset=(bsize-lines-myBar.getValue()); }
myBar.setPageIncrement(lines);
ptr=topptr-lines-offset;
while (ptr<0) ptr=ptr+max;
for (r=0; r0)
{
if (BFlags[ptr]==1) { gg.setColor(msColor); }
else
if (BFlags[ptr]==2) { gg.setColor(Color.blue); }
else
if (BFlags[ptr]==3) { gg.setColor(Color.red); }
else
{ gg.setColor(fgColor); }
}
else
{ gg.setColor(fgColor); }
l=l+spacing;
gg.drawString(Buffer[ptr],3,l);
}
if (++ptr==max) ptr=0;
}
}
ggReal.drawImage(OffScreen,0,0,null);
}
}
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Subscribe to Tutorials
Related Tutorials
Program using concept of byte long short and int in java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Count number of vowels, consonants and digits in a String in Java
Student marks calculation program in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java
Archived Comments
1. Can you please send me complete sample code of cha
View Tutorial By: Brajesh at 2011-04-21 05:48:17
2. DisplayArea myDisplay;
Line giving error my
View Tutorial By: Dinesh Kandpal at 2017-04-30 22:40:43
3. refer this site
View Tutorial By: Manikandan k at 2017-07-24 13:16:03