/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* ClientF.java
*
* Created on 27-mag-2010, 16.06.46
*/
package jchat;
import com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
/**
*
* @author Paolo
*/
public class jChat
extends javax.
swing.
JFrame {
private class Refresh
extends Thread {
@Override
public void run() {
try {
while ((line = br.readLine()) != null) {
msgStore.append(line + "\n");
msgStore.setCaretPosition(msgStore.getText().length());
String nome
=line.
split(":")[0
];
if(!nome.equals(nameField.getText()) && sound.isSelected()){
play.start();
initSound();
}
}
}
}
}
/** Creates new form ClientF */
public jChat() {
try {
UIManager.
setLookAndFeel(new NimbusLookAndFeel
());
}
initComponents();
this.msgStore.setLineWrap(true);
this.initSound();
}
private void initSound(){
try {
Sequence midi
= MidiSystem.
getSequence(getClass
().
getResource("/jchat/sound/newMsg.mid"));
this.play.setSequence(midi);
this.play.open();
}
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
sendButton
= new javax.
swing.
JButton();
nameLabel
= new javax.
swing.
JLabel();
hostLabel
= new javax.
swing.
JLabel();
connectButton
= new javax.
swing.
JButton();
setTitle("JChat - by Paolo Ricciuti (C)");
setResizable(false);
sendButton.setText("Send");
sendButton.setEnabled(false);
public void actionPerformed
(java.
awt.
event.
ActionEvent evt
) {
sendButtonActionPerformed(evt);
}
});
msgField.setEnabled(false);
msgField.
addKeyListener(new java.
awt.
event.
KeyAdapter() {
public void keyPressed
(java.
awt.
event.
KeyEvent evt
) {
msgFieldKeyPressed(evt);
}
});
msgStore.setColumns(20);
msgStore.setRows(5);
msgStore.setEnabled(false);
scrollPane.setViewportView(msgStore);
nameLabel.setText("Nome:");
nameField.setEnabled(false);
hostField.
setHorizontalAlignment(javax.
swing.
JTextField.
RIGHT);
hostField.setText("localhost");
hostLabel.setText("Host:");
connectButton.setText("Connect");
public void actionPerformed
(java.
awt.
event.
ActionEvent evt
) {
connectButtonActionPerformed(evt);
}
});
sound.setText("Abilita suono");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(22, 22, 22)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(sound)
.addContainerGap())
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(msgField, javax.swing.GroupLayout.PREFERRED_SIZE, 281, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(sendButton)
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.
addComponent(scrollPane, javax.
swing.
GroupLayout.
DEFAULT_SIZE, 360,
Short.
MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(nameLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(nameField, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
.
addPreferredGap(javax.
swing.
LayoutStyle.
ComponentPlacement.
RELATED, 69,
Short.
MAX_VALUE)
.addComponent(hostLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(hostField, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(connectButton)))
.addGap(18, 18, 18)))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(nameLabel)
.addComponent(nameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(hostField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(connectButton)
.addComponent(hostLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(msgField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(sendButton))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(sound)
.
addPreferredGap(javax.
swing.
LayoutStyle.
ComponentPlacement.
RELATED, 9,
Short.
MAX_VALUE)
.addComponent(scrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void sendButtonActionPerformed
(java.
awt.
event.
ActionEvent evt
) {//GEN-FIRST:event_sendButtonActionPerformed
if (!this.nameField.getText().trim().equals("")) {
try {
dos.writeBytes(this.nameField.getText() + ": " + this.msgField.getText() + "\n");
}
this.msgField.setText("");
this.nameField.setEditable(false);
}else{
JOptionPane.
showMessageDialog(rootPane,
"Inserire un nome utente!");
}
}//GEN-LAST:event_sendButtonActionPerformed
private void connectButtonActionPerformed
(java.
awt.
event.
ActionEvent evt
) {//GEN-FIRST:event_connectButtonActionPerformed
try {
this.
sock = new Socket(this.
hostField.
getText(), 2223
);
}
new Refresh().start();
this.msgField.setEnabled(true);
this.nameField.setEnabled(true);
this.sendButton.setEnabled(true);
this.msgStore.setEnabled(true);
this.msgStore.setEditable(false);
this.hostField.setEnabled(false);
this.connectButton.setEnabled(false);
this.nameField.requestFocus();
}//GEN-LAST:event_connectButtonActionPerformed
private void msgFieldKeyPressed
(java.
awt.
event.
KeyEvent evt
) {//GEN-FIRST:event_msgFieldKeyPressed
if (evt.
getKeyCode() == KeyEvent.
VK_ENTER) {
this.sendButtonActionPerformed(null);
}
}//GEN-LAST:event_msgFieldKeyPressed
/**
* @param args the command line arguments
*/
public static void main
(String args
[]) {
public void run() {
jChat chat=new jChat();
int x=(screen.width-chat.getSize().width)/2;
int y=(screen.height-chat.getSize().height)/2;
chat.setLocation(x, y);
chat.setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.
swing.
JButton connectButton
;
private javax.
swing.
JLabel hostLabel
;
private javax.
swing.
JLabel nameLabel
;
private javax.
swing.
JButton sendButton
;
// End of variables declaration//GEN-END:variables
}