Uploaded by dharpuja596

project

advertisement
Project Name: Single Chatting Application
It is a desktop based application where two members can chat simultaneously. In this
project I have taken help of Java Swing, AWT and Socket Programming.
Technologies Used: Core Java (
Swing, AWT, Socket Programming)
setLocation(x axis, y axis): to set the
location of the appearing frame.
IDE used: Netbeans
getContentPane().setBackground(): to
set the color of the frame. Here color
class is under awt package.
I have used two classes here
respectively “Server” and “Client”.
Server Class:
In this class I have Imported a few
packages called
import javax.swing.*;
setLayout(): to set layout in the frame.
Example: “null” means we will create our
own layout.
JPanel: If we have to do something
over the frame, we can do it with the
help of panel.
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.util.*;
import java.text.*;
import java.net.*;
setBackground(): setting background
color of the panel. We have used object
of color class and used rgb format.
setBounds(starting co-ordinates,
length,height): we have to pass the coordinates of the component.
add() : to set any component over the
frame.
import java.io.*;
I have created a object called Server
which will be accessed by the
constructor itself and all the coding will
be inside the Constructor Server(). To
make frame we have used JFrame class
which is under the swing
package(Java’s extended Package).
Some functions used in this class:
setSize(): used for setting the size of the
frame. And it will be under JFrame
class.
setVisible(): To show the visibility of the
frame.
We have used separate icons folder in
the source code.
ImageIcon class:
ClassLoader.getSystemResource(): we
have to pass the location of the picture.
getImage().getScaledInstance(width,
height, scaling system): to scale image
back.setBounds(x axis, y axis, image’s
length, width): to set JLabel over frame.
ActionListener interface: it is under
awt.event.*
back.addMouseListener : to add action
on clicking some icon by mouse
JLabel: to write anything over frame.
setForeground(): to set the color of the
text
createVerticalStrut(): to create gap after
every message
formatLabel: to pass every message in a
box
EmptyBorder():to add border of the
message boxes which is under
swing.border.* child package
Calendar.getInstance(): to add time of
message-sending which is under util
class
setFont(): to set the font of the text
SimpleDateFormat(): to format the date
which is under text package
setUndecorated(true) : to remove the
extra part over the frame
setText(): to set empty the message box
after sending message
JTextField() : to make textfield. It needs
to be declared globally.
JButton : to make button.
Client Class: All the functions are as
same as the Server() class.
addActionListener(this) : to create action
on clicking on send button which will be
explained in actionPerformed function.
getText(): to take value of the
JTextField() function
BorderLayout():to place element around
border
Box: core layout
repaint(),f.invalidate(),f.validate():
these three functions are used to
repaint/reload the frame.
createVerticalBox(): to align messages
on after another.
Socket programming: to make inter
connection between server to socket. It
consists of two classes. They are under
net package.
Server socket class: to make server. We
have to use try-catch block to remove
exception.
ServerSocket(): to create server socket.
It should be run and accept message
infinitely.
DataInputStream(): to receive message
. it is under io package
DataOutputStream():to send message
readUTF(): protocol to receive message
infinitely. It returns a string.
Socket(): to create Socket. We have to
give address of the server.
writeUTF():protocol to send messages
infinitely.
Rest will be same as the Server Socket
class.
Socket class: to make client. Server
socket will be only one but socket can
be many.
Code and Output of Server Class:
Code:
package chatting.application;
static DataOutputStream dout;
//we have used swing to make a frame
Server()
import javax.swing.*;
{
import javax.swing.border.*;
f.setLayout(null);
import java.awt.*;
JPanel p1 = new JPanel();
import java.awt.event.*;
import java.util.*;
import java.text.*;
import java.net.*;
import java.io.*;
public class Server implements
ActionListener{//it is under awt.event.*
JTextField text;
JPanel a1 ;
static Box vertical =
Box.createVerticalBox();//box is an
inbuilt layout
static JFrame f = new JFrame();
p1.setBackground(new
Color(7,94,84));
p1.setBounds(0,0,450,70);
p1.setLayout(null);
f.add(p1);
ImageIcon i1 = new
ImageIcon(ClassLoader.getSystemReso
urce("icons/3.png"));
Image i2 =
i1.getImage().getScaledInstance(25,25,I
mage.SCALE_DEFAULT);
ImageIcon i3 = new ImageIcon(i2);
JLabel back = new JLabel(i3);
back.setBounds(5,20,25,25);
p1.add(back);
back.addMouseListener(new
MouseAdapter()//to add action on
clicking some icon
{
public void
mouseClicked(MouseEvent ae)
{
System.exit(0);
}
}
);
ImageIcon i4 = new
ImageIcon(ClassLoader.getSystemReso
urce("icons/p2.png"));
Image i5 =
i4.getImage().getScaledInstance(50,50,I
mage.SCALE_DEFAULT);
ImageIcon i6 = new ImageIcon(i5);
JLabel profile = new JLabel(i6);
profile.setBounds(40,10,50,50);
p1.add(profile);
ImageIcon i7 = new
ImageIcon(ClassLoader.getSystemReso
urce("icons/video.png"));
Image i8 =
i7.getImage().getScaledInstance(30,30,I
mage.SCALE_DEFAULT);
ImageIcon i9 = new ImageIcon(i8);
JLabel video = new JLabel(i9);
video.setBounds(300,20,30,30);
p1.add(video);
ImageIcon i10 = new
ImageIcon(ClassLoader.getSystemReso
urce("icons/phone.png"));
Image i11 =
i10.getImage().getScaledInstance(35,30
,Image.SCALE_DEFAULT);
ImageIcon i12 = new
ImageIcon(i11);
JLabel phone = new JLabel(i12);
phone.setBounds(360,20,35,30);
p1.add(phone);
ImageIcon i13 = new
ImageIcon(ClassLoader.getSystemReso
urce("icons/3icon.png"));
Image i14 =
i13.getImage().getScaledInstance(10,25
,Image.SCALE_DEFAULT);
ImageIcon i15 = new
ImageIcon(i14);
JLabel morevert = new JLabel(i15);
morevert.setBounds(420,20,10,25);
p1.add(morevert);
JLabel name = new JLabel("Puja");
name.setBounds(110,15,100,18);
name.setForeground(Color.WHITE);
name.setFont(new
Font("SAN_SERIF",Font.BOLD,18));
p1.add(name);
send.setForeground(Color.WHITE);
//to create action on clicking on
send button which will be explained in
actionPerformed function
JLabel status = new JLabel("Active
Now");
status.setBounds(110,35,100,18);
send.addActionListener(this);
send.setFont(new
Font("SAN_SERIF",Font.PLAIN,16));
f.add(send);
status.setForeground(Color.WHITE);//to
set the color of the text
status.setFont(new
Font("SAN_SERIF",Font.BOLD,14));//to
set the font of the text
f.setSize(450,700);
f.setLocation(200,50);
f.setUndecorated(true);//to remove
the extra part over the frame
p1.add(status);
//text area
a1 = new JPanel();
f.getContentPane().setBackground(Colo
r.WHITE);
f.setVisible(true);
a1.setBounds(5,75,440,570);
f.add(a1);
text = new JTextField();//to make
text field
text.setBounds(5,655,310,40);
text.setFont(new
Font("SAN_SERIF",Font.PLAIN,16));
f.add(text);
JButton send = new
JButton("Send");
send.setBounds(320,655,123,40);
send.setBackground(new
Color(7,94,84));
}
public void
actionPerformed(ActionEvent ae){
try {
String out = text.getText();//to
take value of the JTextField() function
JPanel p2 = formatLabel(out);
a1.setLayout(new
BorderLayout());//to place element
around border
JPanel right = new JPanel(new
BorderLayout());
right.add(p2,BorderLayout.LINE_END);
vertical.add(right);
vertical.add(Box.createVerticalStrut(15))
;//creating gap after text
a1.add(vertical,
BorderLayout.PAGE_START);//aligning
vertical
JLabel output = new
JLabel("<html><p style=\"width:
150px\">" + out + "</p><html>");
output.setFont((new
Font("Tahoma",Font.PLAIN, 16)));
output.setBackground(new
Color(37,211,102));
output.setOpaque(true);
dout.writeUTF(out);//to send
messages infifinitely
//to add border of the message
boxes which is under swing.border.*
child package
text.setText("");//to set empty the
message box after sending message
output.setBorder(new
EmptyBorder(15,15,15,20));
f.repaint();
f.invalidate();
f.validate();
}
catch (Exception e) {
e.printStackTrace();
panel.add(output);
Calendar cal =
Calendar.getInstance();//to add time of
message-sending which is under util
class
SimpleDateFormat sdf = new
SimpleDateFormat("HH:mm");//to format
the date which is under text package
}
JLabel time = new JLabel();
}
// to pass every message in a box
time.setText(sdf.format(cal.getTime()));
panel.add(time);
public static JPanel
formatLabel(String out)
{
return panel;
}
JPanel panel = new JPanel();
panel.setLayout(new
BoxLayout(panel,BoxLayout.Y_AXIS));
public static void main(String[] args)
{
new Server();
try{
}
ServerSocket skt = new
ServerSocket(6001);//to create server
socket
}
catch(Exception e)
{
while(true)
e.printStackTrace();
{
}
Socket s = skt.accept();
DataInputStream din = new
DataInputStream(s.getInputStream());//
to receive message and it is under io
package
dout = new
DataOutputStream(s.getOutputStream()
);// to send message
// to send and receive
messages infinitely
while(true)
{
String msg = din.readUTF();
JPanel panel =
formatLabel(msg);
JPanel left = new JPanel(new
BorderLayout());
//adding on left of the screen
left.add(panel,BorderLayout.LINE_STA
RT);
vertical.add(left);
f.validate();
}
}
}
Output:
Code and output of Client class:
Code:
//to set layout in the frame.
Example: “null” means we will create our
own layout.
f.setLayout(null);
package chatting.application;
import javax.swing.*;
//if we have to do something
over the frame, we can do it with the
help of panel
import java.awt.*;
JPanel p1 = new JPanel();
import java.awt.event.*;
import javax.swing.border.*;
import java.util.*;
import java.text.*;
import java.net.*;
import java.io.*;
public class Client implements
ActionListener{
JTextField text;
p1.setBackground(new
Color(7,94,84));//setting background
color of the panel.Used Color class'es
object and rgb format
p1.setBounds(0,0,450,70);//to
pass the co-ordinates of the component.
p1.setLayout(null);
f.add(p1);//to set any
component over the frame
//we have to pass the location
of the picture.
static JPanel a1;
static Box vertical =
Box.createVerticalBox();
static DataOutputStream dout;
//To make frame we have used
JFrame class which is under the swing
package
static JFrame f = new JFrame();
Client(){
ImageIcon i1 = new
ImageIcon(ClassLoader.getSystemReso
urce("icons/3.png"));
Image i2 =
i1.getImage().getScaledInstance(25, 25,
Image.SCALE_DEFAULT);
ImageIcon i3 = new
ImageIcon(i2);
JLabel back = new JLabel(i3);
back.setBounds(5,20,25,25);//to
set JLabel over frame.
p1.add(back);
back.addMouseListener(new
MouseAdapter() {
public void
mouseClicked(MouseEvent ae) {
System.exit(0);
}
});
ImageIcon i4 = new
ImageIcon(ClassLoader.getSystemReso
urce("icons/p1.png"));
Image i5 =
i4.getImage().getScaledInstance(50, 50,
Image.SCALE_DEFAULT);
ImageIcon i6 = new
ImageIcon(i5);
JLabel profile = new
JLabel(i6);
profile.setBounds(40,10,50,50);
p1.add(profile);
ImageIcon i7 = new
ImageIcon(ClassLoader.getSystemReso
urce("icons/video.png"));
Image i8 =
i7.getImage().getScaledInstance(30, 30,
Image.SCALE_DEFAULT);
ImageIcon i9 = new
ImageIcon(i8);
JLabel video = new JLabel(i9);
video.setBounds(300,20,30,30);
p1.add(video);
ImageIcon i10 = new
ImageIcon(ClassLoader.getSystemReso
urce("icons/phone.png"));
Image i11 =
i10.getImage().getScaledInstance(35,
30, Image.SCALE_DEFAULT);
ImageIcon i12 = new
ImageIcon(i11);
JLabel phone = new
JLabel(i12);
phone.setBounds(360, 20, 35,
30);
p1.add(phone);
ImageIcon i13 = new
ImageIcon(ClassLoader.getSystemReso
urce("icons/3icon.png"));
Image i14 =
i13.getImage().getScaledInstance(10,
25, Image.SCALE_DEFAULT);
ImageIcon i15 = new
ImageIcon(i14);
JLabel morevert = new
JLabel(i15);
morevert.setBounds(420, 20,
10, 25);
p1.add(morevert);
JLabel name = new
JLabel("Rony");
name.setBounds(110, 15,
100, 18);
JButton send = new
JButton("Send");
send.setBounds(320, 655,
name.setForeground(Color.WHITE);
123, 40);
name.setFont(new
Font("SAN_SERIF", Font.BOLD, 18));
send.setBackground(new
Color(7, 94, 84));
p1.add(name);
send.setForeground(Color.WHITE);
JLabel status = new
JLabel("Active Now");
status.setBounds(110, 35,
send.addActionListener(this);
send.setFont(new
Font("SAN_SERIF", Font.PLAIN, 16));
100, 18);
f.add(send);
status.setForeground(Color.WHITE);
f.setSize(450,700);//used for
setting the size of the frame
status.setFont(new
Font("SAN_SERIF", Font.BOLD, 14));
p1.add(status);
a1 = new JPanel();
a1.setBounds(5,75,440,570);
f.add(a1);
f.setLocation(800,50);
f.setUndecorated(true);
//to set the color of the frame.
Here color class is under awt package.
f.getContentPane().setBackgroun
d(Color.WHITE);
f.setVisible(true);//To show the
visibility of the frame.
}
text = new JTextField();
text.setBounds(5, 655, 310,
public void
actionPerformed(ActionEvent ae) {
40);
try {
text.setFont(new
Font("SAN_SERIF", Font.PLAIN, 16));
String out = text.getText();
f.add(text);
JPanel p2 =
formatLabel(out);
JPanel panel = new
a1.setLayout(new
BorderLayout());
JPanel();
panel.setLayout(new
BoxLayout(panel, BoxLayout.Y_AXIS));
JPanel right = new
JPanel(new BorderLayout());
JLabel output = new
JLabel("<html><p style=\"width:
150px\">" + out + "</p></html>");
right.add(p2,
BorderLayout.LINE_END);
output.setFont(new
Font("Tahoma", Font.PLAIN, 16));
vertical.add(right);
vertical.add(Box.createVerticalStr
ut(15));
a1.add(vertical,
BorderLayout.PAGE_START);
dout.writeUTF(out);
text.setText("");
output.setBackground(new Color(37,
211, 102));
output.setOpaque(true);
output.setBorder(new
EmptyBorder(15, 15, 15, 50));
panel.add(output);
Calendar cal =
Calendar.getInstance();
SimpleDateFormat sdf = new
SimpleDateFormat("HH:mm");
f.repaint();
JLabel time = new JLabel();
f.invalidate();
f.validate();
time.setText(sdf.format(cal.getTime()));
}catch(Exception e) {
panel.add(time);
e.printStackTrace();
return panel;
}
}
public static JPanel
formatLabel(String out) {
}
public static void main(String[] args)
{
new Client();
try {
Socket s = new
Socket("127.0.0.1",6001);//to create
Socket
a1.add(vertical,
BorderLayout.PAGE_START);
f.validate();
DataInputStream din =
}
new
DataInputStream(s.getInputStream());
} catch(Exception e) {
e.printStackTrace();
dout = new
DataOutputStream(s.getOutputStream()
);
}
}
}
while(true) {
Output:
a1.setLayout(new BorderLayout());
String msg
= din.readUTF();
JPanel
panel = formatLabel(msg);
JPanel left
= new JPanel(new BorderLayout());
left.add(panel,
BorderLayout.LINE_START);
vertical.add(left);
vertical.add(Box.createVerticalStrut(15))
;
Client And Server receiving and sending messages at the same time:
Download