Motivasi Menulis

LAN Chatting

Lan Chatting menggunakan java



import java.awt.Image;
import java.awt.Component;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.border.EtchedBorder;
import javax.swing.JFileChooser;
import java.io.File;

public class TwoChat extends javax.swing.JFrame {

    ServerSocket ss;
    Socket s,t;
   JLabel lb1=new JLabel();
   JLabel lb2=new JLabel();
   JLabel lb3=new JLabel();
   JLabel lb4=new JLabel();
    JTextField text=new JTextField();
    JButton send=new JButton();
    JButton emot=new JButton();

JScrollPane sp;
    JTextArea namane=new JTextArea();
    JTextArea daring=new JTextArea();
    JTextField ipnya=new JTextField();
    JTextField usernya=new JTextField();
    PrintWriter pout;
    BufferedReader br;
    ActionListener al;
    ActionListener em;
    String ipstring;
    boolean ready2send=false;
    boolean emotmuncul=false;
    TwoChat pt;
    boolean HorC;
    String cliOrServ;

    JTextPane messages = new JTextPane();

    public TwoChat(boolean hostOrConnect,String ip)
    {

    sp = new JScrollPane(messages);


    al=new ActionListener()


 { public void actionPerformed(ActionEvent e)
  {
  ready2send=true;
  }
 };
  em=new ActionListener()
 { public void actionPerformed(ActionEvent e)
  {
   emotmuncul=true;
  }
 };
//////////////////////////////////
////////emot////////////
messages.setEditorKit(new StyledEditorKit());
        SimpleAttributeSet attrs = new SimpleAttributeSet();
        StyleConstants.setIcon(attrs, getImage());
        messages.addCaretListener(new CaretListener() {

            public void caretUpdate(CaretEvent e) {
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        try {
                            StyledDocument doc = (StyledDocument) messages.getDocument();
                            String text = doc.getText(0, messages.getDocument().getLength());
                            int index = text.indexOf(":)");

                            int start = 0;
                            while (index > -1) {
                                Element el = doc.getCharacterElement(index);
                                if (StyleConstants.getIcon(el.getAttributes()) == null) {
                                    doc.remove(index, 2);
                                    SimpleAttributeSet attrs = new SimpleAttributeSet();
                                    StyleConstants.setIcon(attrs, getImage());
                                    doc.insertString(index, ":)", attrs);
                                }

                                start = index + 2;
                                index = text.indexOf(":)", start);

                            }
                        }
                        catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }
                });
            }
        });
/////////////
        messages.setEditorKit(new StyledEditorKit());
        SimpleAttributeSet attr = new SimpleAttributeSet();

        StyleConstants.setIcon(attr, getImage1());
        messages.addCaretListener(new CaretListener() {
            public void caretUpdate(CaretEvent e) {
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        try {
                            StyledDocument doc = (StyledDocument) messages.getDocument();
                            String text = doc.getText(0, messages.getDocument().getLength());
                            int index = text.indexOf(":(");

                            int start = 0;
                            while (index > -1) {
                                Element el = doc.getCharacterElement(index);
                                if (StyleConstants.getIcon(el.getAttributes()) == null) {
                                    doc.remove(index, 2);
                                    SimpleAttributeSet attr = new SimpleAttributeSet();
                                    StyleConstants.setIcon(attr, getImage1());
                                    doc.insertString(index, ":(", attr);
                                }

                                start = index + 2;
                                index = text.indexOf(":(", start);

                            }
                        }
                        catch (Exception ex1) {
                            ex1.printStackTrace();
                        }
                    }
                });
            }
        });
//////////////
         messages.setEditorKit(new StyledEditorKit());
        SimpleAttributeSet att = new SimpleAttributeSet();
        StyleConstants.setIcon(att, getImage2());
        messages.addCaretListener(new CaretListener() {

            public void caretUpdate(CaretEvent e) {
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        try {
                            StyledDocument doc = (StyledDocument) messages.getDocument();
                            String text = doc.getText(0, messages.getDocument().getLength());
                            int index = text.indexOf(":P");
                            int start = 0;
                            while (index > -1) {
                                Element el = doc.getCharacterElement(index);
                                if (StyleConstants.getIcon(el.getAttributes()) == null) {
                                    doc.remove(index, 2);
                                    SimpleAttributeSet att = new SimpleAttributeSet();
                                    StyleConstants.setIcon(att, getImage2());
                                    doc.insertString(index, ":P", att);
                                }

                                start = index + 2;
                                index = text.indexOf(":P", start);

                            }
                        }
                        catch (Exception a1) {
                            a1.printStackTrace();
                        }
                    }
                });
            }
        });
//////////////
         messages.setEditorKit(new StyledEditorKit());
        SimpleAttributeSet at = new SimpleAttributeSet();
        StyleConstants.setIcon(at, getImage3());
        messages.addCaretListener(new CaretListener() {

            public void caretUpdate(CaretEvent e) {
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        try {
                            StyledDocument doc = (StyledDocument) messages.getDocument();
                            String text = doc.getText(0, messages.getDocument().getLength());
                            int index = text.indexOf(":/");

                            int start = 0;
                            while (index > -1) {
                                Element el = doc.getCharacterElement(index);
                                if (StyleConstants.getIcon(el.getAttributes()) == null) {
                                    doc.remove(index, 2);
                                    SimpleAttributeSet at = new SimpleAttributeSet();
                                    StyleConstants.setIcon(at, getImage3());
                                    doc.insertString(index, ":/", at);
                                }

                                start = index + 2;
                                index = text.indexOf(":/", start);

                            }
                        }
                        catch (Exception z1) {
                            z1.printStackTrace();
                        }
                    }
                });
            }
        });
/////////////////////////////////
    messages.setEditorKit(new StyledEditorKit());
        SimpleAttributeSet at1 = new SimpleAttributeSet();
        StyleConstants.setIcon(at1, getImage4());
        messages.addCaretListener(new CaretListener() {

            public void caretUpdate(CaretEvent e) {
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        try {
                            StyledDocument doc = (StyledDocument) messages.getDocument();
                            String text = doc.getText(0, messages.getDocument().getLength());
                            int index = text.indexOf(":D");

                            int start = 0;
                            while (index > -1) {
                                Element el = doc.getCharacterElement(index);
                                if (StyleConstants.getIcon(el.getAttributes()) == null) {
                                    doc.remove(index, 2);
                                    SimpleAttributeSet at1 = new SimpleAttributeSet();
                                    StyleConstants.setIcon(at1, getImage4());
                                    doc.insertString(index, ":D", at1);
                                }

                                start = index + 2;
                                index = text.indexOf(":D", start);

                            }
                        }
                        catch (Exception g1) {
                            g1.printStackTrace();
                        }
                    }
                });
            }
        });

///////////////////////////////////
setTitle("Chattingan Antar PC");
setLocation(300,80);
        ipstring=ip;
        setLayout(null);
        setSize(650,590);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        add(sp);
messages.setEditable(false);
        sp.setBorder(new EtchedBorder());
        sp.setLocation(5,107);
        sp.setSize(5+450,398);

add(namane);
        namane.setEditable(false);
        namane.setBorder(new EtchedBorder());
        namane.setLocation(5,50);
        namane.setSize(5+450,52);

add(lb2);
lb2.setLocation(7,17);
lb2.setSize(20,10);
lb2.setText("IP");

add(ipnya);
        ipnya.setEditable(false);
        ipnya.setBorder(new EtchedBorder());
        ipnya.setLocation(30,10);
        ipnya.setSize(150,25);

add(lb3);
lb3.setLocation(230,17);
lb3.setSize(40,10);
lb3.setText("User");

add(usernya);
        usernya.setEditable(false);
        usernya.setBorder(new EtchedBorder());
        usernya.setLocation(270,10);
        usernya.setSize(150,25);

add(lb1);
lb1.setLocation(500,20);
lb1.setSize(100,10);
lb1.setText("SEDANG ONLINE");

        add(daring);
        daring.setEditable(false);
        daring.setBorder(new EtchedBorder());
        daring.setLocation(465,50);
        daring.setSize(164,455);

  add(text);
  text.setBorder(new EtchedBorder());
        text.setLocation(5,510);
        text.setSize(455,30);
        text.setEnabled(false);

      text.addActionListener(al);

        add(send);
        send.setBorder(new EtchedBorder());
        send.setLocation(465,510);
        send.setText("Kirim");
        send.setSize(80,30);
send.addActionListener(al);

add(emot);
emot.setBorder(new EtchedBorder());
        emot.setLocation(555,510);
        emot.setText("Tema");
        emot.setSize(73,30);
emot.addActionListener(em);

        pt=this;
        HorC=hostOrConnect;
        if(HorC)
            cliOrServ="\nServer: ";
        else
            cliOrServ="\nKlien: ";

        Messenger.start();

    }

    public static void main(String args[])  {

        int inp=JOptionPane.showConfirmDialog(null,"Silahkan Pilih\n(Yes)Server Atau (No)Client","LAN Chating",JOptionPane.YES_NO_OPTION);
        if(inp==0)
            {
            new TwoChat(true,null).setVisible(true);
            }
        else if(inp==1)
            {
            String ipstring=JOptionPane.showInputDialog("Masukkan IP Address Server");
            try{
                InetAddress.getByName(ipstring);
                new TwoChat(false,ipstring).setVisible(true);
            }catch(Exception e){JOptionPane.showMessageDialog(null,"IP Address Salah");}
            }
            else
            {
       System.exit(0);
            }

    }
Thread Messenger=new Thread()
{
 public void run()
 {
  try{
  if(HorC)
        {
       ipnya.setText(" Server : "+InetAddress.getLocalHost().getHostAddress());
        usernya.setText(" "+InetAddress.getLocalHost().getHostName());
        ss=new ServerSocket(9999);
        s= ss.accept();
        s.setKeepAlive(true);
        }
  else
        {
        namane.setText("Menghubungkan ke~"+ipstring+":"+"\n");
        ipnya.setText(" Klien : "+InetAddress.getLocalHost().getHostAddress());
        usernya.setText(" "+InetAddress.getLocalHost().getHostName());
        s=new Socket(InetAddress.getByName(ipstring),9999);
        }
  text.setEnabled(true);
  pout = new PrintWriter(s.getOutputStream(),true);
  br=new BufferedReader(new InputStreamReader(s.getInputStream()));
  namane.setText(namane.getText()+"Terhubung dengan:"+s.getInetAddress().getHostAddress()+"\nPort : 9999");
InetAddress host=null;
host = InetAddress.getLocalHost();
//menampilkan yang sedang online
  daring.setText(daring.getText()+"\n "+host.getHostName()+"\n "+s.getInetAddress().getHostName());

  while(true)
  {
  //perulangan chat
   if(ready2send==true)
        {
        pout.println(text.getText());
        messages.setText(messages.getText()+"\n  + "+host.getHostName()+"    : "+text.getText());
        text.setText("");
        ready2send=false;
        }

    if(br.ready())
        {
        messages.setText(messages.getText()+"\n  - "+s.getInetAddress().getHostName()+"    : "+br.readLine());
        }
    Thread.sleep(80);
// perulangan emo
  if(emotmuncul==true)
        {
JFileChooser chooser = new JFileChooser();
        chooser.showOpenDialog(null);
        File file = chooser.getSelectedFile();

        try {
            Image image = ImageIO.read(file);

            ImageIcon imageIcon = new ImageIcon(image.getScaledInstance(650, 590, 300));

add(lb4);
lb4.setBorder(new EtchedBorder());
lb4.setLocation(0,0);
lb4.setSize(650,590);

            lb4.setIcon(imageIcon);
            text.setText("");

        }
         catch (Exception e) {
        }
           //if the user click on save in Jfilechooser

            //if the user click on save in Jfilechooser
emotmuncul=false;
        }
  }
 }catch(Exception ex)
    {
    JOptionPane.showMessageDialog(pt,ex.getMessage());
    namane.setText("Tidak Bisa Terhubung!");
      try {
          wait(3000);
      } catch (InterruptedException ex1) {}
    System.exit(0);
    }
 }
};
/////////////////////////

  /////emot graficnya///
//:)
 ImageIcon getImage() {
        BufferedImage bi = new BufferedImage(17, 17, BufferedImage.TYPE_INT_ARGB);
        Graphics g = bi.getGraphics();
        g.setColor(Color.yellow);
        g.fillOval(0,0,16,16);

        g.setColor(Color.black);
        g.drawOval(0,0,16,16);

        g.drawLine(4,5, 6,5);
        g.drawLine(4,6, 6,6);

        g.drawLine(11,5, 9,5);
        g.drawLine(11,6, 9,6);

        g.drawLine(4,10, 8,12);
        g.drawLine(8,12, 12,10);
        g.dispose();
        return new ImageIcon(bi);
    }

/////////////
//:(
ImageIcon getImage1() {
        BufferedImage bi1 = new BufferedImage(17, 17, BufferedImage.TYPE_INT_ARGB);
        Graphics j = bi1.getGraphics();
        j.setColor(Color.yellow);
        j.fillOval(0,0,16,16);
    j.setColor(Color.black);
        j.drawOval(0,0,16,16);

        j.drawLine(4,5, 6,5);
        j.drawLine(4,6, 6,6);

        j.drawLine(11,5, 9,5);
        j.drawLine(11,6, 9,6);

        j.drawLine(4,10, 8,8);
        j.drawLine(8,8, 12,10);
        j.dispose();
         return new ImageIcon(bi1);
    }

/////////////
//:P
ImageIcon getImage2() {
        BufferedImage bi2 = new BufferedImage(17, 17, BufferedImage.TYPE_INT_ARGB);
        Graphics p = bi2.getGraphics();
        p.setColor(Color.yellow);
        p.fillOval(0,0,16,16);

        p.setColor(Color.black);
        p.drawOval(0,0,16,16);

        p.drawLine(4,5, 6,5);
        p.drawLine(4,6, 6,6);

        p.drawLine(11,5, 9,5);
        p.drawLine(11,6, 9,6);

    p.setColor(Color.black);
        p.drawLine(4,10, 8,12);
        p.drawLine(8,12, 12,10);

        p.setColor(Color.red);
        p.drawLine(8,12, 8,13);
        p.drawLine(7,14, 6,13);
        p.drawLine(5,12, 4,11);
        p.drawLine(4,10, 8,12);
        p.dispose();
        return new ImageIcon(bi2);
    }

/////////////
//:/
ImageIcon getImage3() {
        BufferedImage bi3 = new BufferedImage(17, 17, BufferedImage.TYPE_INT_ARGB);
        Graphics i = bi3.getGraphics();
        i.setColor(Color.yellow);
        i.fillOval(0,0,16,16);
    i.setColor(Color.black);
        i.drawOval(0,0,16,16);

        i.drawLine(4,5, 6,5);
        i.drawLine(4,6, 6,6);

        i.drawLine(11,5, 9,5);
        i.drawLine(11,6, 9,6);

        i.drawLine(4,10, 8,10);
        i.drawLine(8,10, 12,10);
        i.dispose();
         return new ImageIcon(bi3);
}

//////////////////////////////////
//:D
ImageIcon getImage4() {
        BufferedImage bi4 = new BufferedImage(17, 17, BufferedImage.TYPE_INT_ARGB);
        Graphics h = bi4.getGraphics();
  h.setColor(Color.yellow);
        h.fillOval(0,0,16,16);
    h.setColor(Color.black);
        h.drawOval(0,0,16,16);

        h.drawLine(4,5, 6,5);
        h.drawLine(4,6, 6,6);

        h.drawLine(11,5, 9,5);
        h.drawLine(11,6, 9,6);


        h.setColor(Color.white);
        h.drawOval(0,0,0,0);

        h.drawLine(4,10, 8,12);
        h.drawLine(8,12, 12,10);

        h.drawLine(4,10, 8,10);
        h.drawLine(8,10, 12,10);

        h.dispose();
        return new ImageIcon(bi4);
    }
}

0 Comment for "LAN Chatting"

Back To Top