Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
JChat - jChat.java

jChat.java

Caricato da:
Scarica il programma completo

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. /*
  7.  * ClientF.java
  8.  *
  9.  * Created on 27-mag-2010, 16.06.46
  10.  */
  11. package jchat;
  12.  
  13. import com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel;
  14. import java.awt.Dimension;
  15. import java.awt.Toolkit;
  16. import java.awt.event.KeyEvent;
  17. import java.io.BufferedReader;
  18. import java.io.DataOutputStream;
  19. import java.io.IOException;
  20. import java.io.InputStreamReader;
  21. import java.net.Socket;
  22. import java.net.UnknownHostException;
  23. import java.util.logging.Level;
  24. import java.util.logging.Logger;
  25. import javax.sound.midi.InvalidMidiDataException;
  26. import javax.sound.midi.MidiSystem;
  27. import javax.sound.midi.MidiUnavailableException;
  28. import javax.sound.midi.Sequence;
  29. import javax.sound.midi.Sequencer;
  30. import javax.swing.JOptionPane;
  31. import javax.swing.UIManager;
  32. import javax.swing.UnsupportedLookAndFeelException;
  33.  
  34. /**
  35.  *
  36.  * @author Paolo
  37.  */
  38. public class jChat extends javax.swing.JFrame {
  39.  
  40.     private Socket sock;
  41.     private Sequencer play;
  42.  
  43.     private class Refresh extends Thread {
  44.  
  45.         @Override
  46.         public void run() {
  47.             try {
  48.                 BufferedReader br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
  49.                 String line = null;
  50.                 while ((line = br.readLine()) != null) {
  51.                     msgStore.append(line + "\n");
  52.                     msgStore.setCaretPosition(msgStore.getText().length());
  53.                     String nome=line.split(":")[0];
  54.                     if(!nome.equals(nameField.getText()) && sound.isSelected()){
  55.                         play.start();
  56.                         initSound();
  57.                     }
  58.                 }
  59.             } catch (IOException ex) {
  60.             }
  61.         }
  62.     }
  63.  
  64.     /** Creates new form ClientF */
  65.     public jChat() {
  66.         try {
  67.             UIManager.setLookAndFeel(new NimbusLookAndFeel());
  68.         } catch (UnsupportedLookAndFeelException ex) {
  69.         }
  70.         initComponents();
  71.         this.msgStore.setLineWrap(true);
  72.         this.initSound();
  73.     }
  74.  
  75.     private void initSound(){
  76.         try {
  77.             Sequence midi = MidiSystem.getSequence(getClass().getResource("/jchat/sound/newMsg.mid"));
  78.             this.play= MidiSystem.getSequencer();
  79.             this.play.setSequence(midi);
  80.             this.play.open();
  81.         } catch (InvalidMidiDataException ex) {
  82.         } catch (IOException ex) {
  83.         } catch (MidiUnavailableException ex) {
  84.         }
  85.     }
  86.  
  87.     /** This method is called from within the constructor to
  88.      * initialize the form.
  89.      * WARNING: Do NOT modify this code. The content of this method is
  90.      * always regenerated by the Form Editor.
  91.      */
  92.     @SuppressWarnings("unchecked")
  93.     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  94.     private void initComponents() {
  95.  
  96.         sendButton = new javax.swing.JButton();
  97.         msgField = new javax.swing.JTextField();
  98.         scrollPane = new javax.swing.JScrollPane();
  99.         msgStore = new javax.swing.JTextArea();
  100.         nameLabel = new javax.swing.JLabel();
  101.         nameField = new javax.swing.JTextField();
  102.         hostField = new javax.swing.JTextField();
  103.         hostLabel = new javax.swing.JLabel();
  104.         connectButton = new javax.swing.JButton();
  105.         sound = new javax.swing.JCheckBox();
  106.  
  107.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  108.         setTitle("JChat - by Paolo Ricciuti (C)");
  109.         setResizable(false);
  110.  
  111.         sendButton.setText("Send");
  112.         sendButton.setEnabled(false);
  113.         sendButton.addActionListener(new java.awt.event.ActionListener() {
  114.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  115.                 sendButtonActionPerformed(evt);
  116.             }
  117.         });
  118.  
  119.         msgField.setEnabled(false);
  120.         msgField.addKeyListener(new java.awt.event.KeyAdapter() {
  121.             public void keyPressed(java.awt.event.KeyEvent evt) {
  122.                 msgFieldKeyPressed(evt);
  123.             }
  124.         });
  125.  
  126.         msgStore.setColumns(20);
  127.         msgStore.setRows(5);
  128.         msgStore.setEnabled(false);
  129.         scrollPane.setViewportView(msgStore);
  130.  
  131.         nameLabel.setText("Nome:");
  132.  
  133.         nameField.setEnabled(false);
  134.  
  135.         hostField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
  136.         hostField.setText("localhost");
  137.  
  138.         hostLabel.setText("Host:");
  139.  
  140.         connectButton.setText("Connect");
  141.         connectButton.addActionListener(new java.awt.event.ActionListener() {
  142.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  143.                 connectButtonActionPerformed(evt);
  144.             }
  145.         });
  146.  
  147.         sound.setText("Abilita suono");
  148.  
  149.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  150.         getContentPane().setLayout(layout);
  151.         layout.setHorizontalGroup(
  152.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  153.             .addGroup(layout.createSequentialGroup()
  154.                 .addGap(22, 22, 22)
  155.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  156.                     .addGroup(layout.createSequentialGroup()
  157.                         .addComponent(sound)
  158.                         .addContainerGap())
  159.                     .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  160.                         .addGroup(layout.createSequentialGroup()
  161.                             .addComponent(msgField, javax.swing.GroupLayout.PREFERRED_SIZE, 281, javax.swing.GroupLayout.PREFERRED_SIZE)
  162.                             .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  163.                             .addComponent(sendButton)
  164.                             .addContainerGap())
  165.                         .addGroup(layout.createSequentialGroup()
  166.                             .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  167.                                 .addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE)
  168.                                 .addGroup(layout.createSequentialGroup()
  169.                                     .addComponent(nameLabel)
  170.                                     .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  171.                                     .addComponent(nameField, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
  172.                                     .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 69, Short.MAX_VALUE)
  173.                                     .addComponent(hostLabel)
  174.                                     .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  175.                                     .addComponent(hostField, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
  176.                                     .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  177.                                     .addComponent(connectButton)))
  178.                             .addGap(18, 18, 18)))))
  179.         );
  180.         layout.setVerticalGroup(
  181.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  182.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  183.                 .addContainerGap()
  184.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  185.                     .addComponent(nameLabel)
  186.                     .addComponent(nameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  187.                     .addComponent(hostField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  188.                     .addComponent(connectButton)
  189.                     .addComponent(hostLabel))
  190.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  191.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  192.                     .addComponent(msgField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  193.                     .addComponent(sendButton))
  194.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  195.                 .addComponent(sound)
  196.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 9, Short.MAX_VALUE)
  197.                 .addComponent(scrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
  198.                 .addContainerGap())
  199.         );
  200.  
  201.         pack();
  202.     }// </editor-fold>//GEN-END:initComponents
  203.  
  204.     private void sendButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sendButtonActionPerformed
  205.         if (!this.nameField.getText().trim().equals("")) {
  206.             try {
  207.                 DataOutputStream dos = new DataOutputStream(this.sock.getOutputStream());
  208.                 dos.writeBytes(this.nameField.getText() + ": " + this.msgField.getText() + "\n");
  209.             } catch (IOException ex) {
  210.             }
  211.             this.msgField.setText("");
  212.             this.nameField.setEditable(false);
  213.         }else{
  214.             JOptionPane.showMessageDialog(rootPane, "Inserire un nome utente!");
  215.         }
  216.     }//GEN-LAST:event_sendButtonActionPerformed
  217.  
  218.     private void connectButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_connectButtonActionPerformed
  219.         try {
  220.             this.sock = new Socket(this.hostField.getText(), 2223);
  221.         } catch (UnknownHostException ex) {
  222.         } catch (IOException ex) {
  223.         }
  224.         new Refresh().start();
  225.         this.msgField.setEnabled(true);
  226.         this.nameField.setEnabled(true);
  227.         this.sendButton.setEnabled(true);
  228.         this.msgStore.setEnabled(true);
  229.         this.msgStore.setEditable(false);
  230.         this.hostField.setEnabled(false);
  231.         this.connectButton.setEnabled(false);
  232.         this.nameField.requestFocus();
  233.     }//GEN-LAST:event_connectButtonActionPerformed
  234.  
  235.     private void msgFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_msgFieldKeyPressed
  236.         if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
  237.             this.sendButtonActionPerformed(null);
  238.         }
  239.     }//GEN-LAST:event_msgFieldKeyPressed
  240.  
  241.     /**
  242.      * @param args the command line arguments
  243.      */
  244.     public static void main(String args[]) {
  245.         java.awt.EventQueue.invokeLater(new Runnable() {
  246.  
  247.             public void run() {
  248.                 jChat chat=new jChat();
  249.                 Dimension screen= Toolkit.getDefaultToolkit().getScreenSize();
  250.                 int x=(screen.width-chat.getSize().width)/2;
  251.                 int y=(screen.height-chat.getSize().height)/2;
  252.                 chat.setLocation(x, y);
  253.                 chat.setVisible(true);
  254.             }
  255.         });
  256.     }
  257.     // Variables declaration - do not modify//GEN-BEGIN:variables
  258.     private javax.swing.JButton connectButton;
  259.     private javax.swing.JTextField hostField;
  260.     private javax.swing.JLabel hostLabel;
  261.     private javax.swing.JTextField msgField;
  262.     private javax.swing.JTextArea msgStore;
  263.     private javax.swing.JTextField nameField;
  264.     private javax.swing.JLabel nameLabel;
  265.     private javax.swing.JScrollPane scrollPane;
  266.     private javax.swing.JButton sendButton;
  267.     private javax.swing.JCheckBox sound;
  268.     // End of variables declaration//GEN-END:variables
  269. }