Final implementation, ready for presentation!
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,166 @@
|
||||
package demonstration;
|
||||
|
||||
@SuppressWarnings( "javadoc" )
|
||||
public class ChatDEMO extends javax.swing.JFrame {
|
||||
|
||||
public boolean messageReady = false ;
|
||||
public String message ;
|
||||
public String username ;
|
||||
/*
|
||||
public void giveTitle(String name)
|
||||
{
|
||||
this.username = name ;
|
||||
setTitle("logged in as:" + name ) ;
|
||||
}
|
||||
*/
|
||||
public String getMessageOut()
|
||||
{
|
||||
if( this.message.contentEquals( "" ) || this.message.contentEquals( " " ) )
|
||||
{
|
||||
this.messageReady = false ;
|
||||
return null ;
|
||||
}
|
||||
if( this.messageReady )
|
||||
{
|
||||
this.messageReady = false ;
|
||||
return this.message ;
|
||||
}
|
||||
return null ;
|
||||
}
|
||||
|
||||
|
||||
public void printMessage( String messageOut )
|
||||
{
|
||||
messageOut = String.format( "%s\n%s", this.jTextArea1.getText(), messageOut ) ;
|
||||
this.jTextArea1.setText( null ) ;
|
||||
this.jTextArea1.setText( messageOut ) ;
|
||||
jTextArea1.moveCaretPosition( jTextArea1.getDocument().getLength() );
|
||||
}
|
||||
|
||||
|
||||
public ChatDEMO() {
|
||||
initComponents();
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
// <editor-fold defaultstate="collapsed" desc="Generated Code">
|
||||
private void initComponents() {
|
||||
|
||||
jTextField1 = new javax.swing.JTextField();
|
||||
jButton1 = new javax.swing.JButton();
|
||||
jScrollPane2 = new javax.swing.JScrollPane();
|
||||
jTextArea1 = new javax.swing.JTextArea();
|
||||
|
||||
setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
|
||||
setTitle("logged in as: " + this.username );
|
||||
|
||||
jButton1.setText("send");
|
||||
jButton1.addActionListener(new java.awt.event.ActionListener() {
|
||||
public void actionPerformed(java.awt.event.ActionEvent evt) {
|
||||
jButton1ActionPerformed(evt);
|
||||
}
|
||||
});
|
||||
|
||||
jTextField1.addActionListener(new java.awt.event.ActionListener() {
|
||||
public void actionPerformed(java.awt.event.ActionEvent evt) {
|
||||
jTextField1ActionPerformed(evt);
|
||||
}
|
||||
});
|
||||
|
||||
setResizable(false);
|
||||
jTextArea1.setEditable(false);
|
||||
jTextArea1.setColumns(20);
|
||||
jTextArea1.setRows(5);
|
||||
jTextArea1.setWrapStyleWord(true);
|
||||
jTextArea1.setLineWrap( true );
|
||||
jTextArea1.moveCaretPosition( jTextArea1.getDocument().getLength() );
|
||||
jScrollPane2.setViewportView(jTextArea1);
|
||||
|
||||
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
|
||||
getContentPane().setLayout(layout);
|
||||
layout.setHorizontalGroup(
|
||||
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
||||
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
|
||||
.addContainerGap(50, Short.MAX_VALUE)
|
||||
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
|
||||
.addComponent(jScrollPane2)
|
||||
.addGroup(layout.createSequentialGroup()
|
||||
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 610, javax.swing.GroupLayout.PREFERRED_SIZE)
|
||||
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
|
||||
.addComponent(jButton1)))
|
||||
.addGap(50, 50, 50))
|
||||
);
|
||||
layout.setVerticalGroup(
|
||||
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
||||
.addGroup(layout.createSequentialGroup()
|
||||
.addContainerGap(50, Short.MAX_VALUE)
|
||||
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 311, javax.swing.GroupLayout.PREFERRED_SIZE)
|
||||
.addGap(18, 18, 18)
|
||||
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
|
||||
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
|
||||
.addComponent(jButton1))
|
||||
.addGap(50, 50, 50))
|
||||
);
|
||||
|
||||
pack();
|
||||
setLocationRelativeTo(null);
|
||||
}// </editor-fold>
|
||||
|
||||
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt)
|
||||
{
|
||||
if( this.jTextField1.getText() != null )
|
||||
{
|
||||
this.message = this.jTextField1.getText() ;
|
||||
this.messageReady = true ;
|
||||
this.jTextField1.setText( null ) ;
|
||||
}
|
||||
}
|
||||
|
||||
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
|
||||
|
||||
if( this.jTextField1.getText() != null )
|
||||
{
|
||||
this.message = this.jTextField1.getText() ;
|
||||
this.messageReady = true ;
|
||||
this.jTextField1.setText( null ) ;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static void main(String args[]) {
|
||||
|
||||
try {
|
||||
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
|
||||
if ("Nimbus".equals(info.getName())) {
|
||||
javax.swing.UIManager.setLookAndFeel(info.getClassName());
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch (ClassNotFoundException ex) {
|
||||
java.util.logging.Logger.getLogger(ChatDEMO.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
|
||||
} catch (InstantiationException ex) {
|
||||
java.util.logging.Logger.getLogger(ChatDEMO.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
|
||||
} catch (IllegalAccessException ex) {
|
||||
java.util.logging.Logger.getLogger(ChatDEMO.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
|
||||
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
|
||||
java.util.logging.Logger.getLogger(ChatDEMO.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
|
||||
}
|
||||
//</editor-fold>
|
||||
|
||||
/* Create and display the form */
|
||||
java.awt.EventQueue.invokeLater(new Runnable() {
|
||||
public void run() {
|
||||
new ChatDEMO().setVisible(true);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Variables declaration - do not modify
|
||||
private javax.swing.JButton jButton1;
|
||||
private javax.swing.JScrollPane jScrollPane2;
|
||||
private javax.swing.JTextArea jTextArea1;
|
||||
private javax.swing.JTextField jTextField1;
|
||||
// End of variables declaration
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,110 @@
|
||||
package demonstration;
|
||||
|
||||
import java.net.* ;
|
||||
import java.io.* ;
|
||||
|
||||
@SuppressWarnings( "javadoc" )
|
||||
public class ClientDEMO
|
||||
{
|
||||
|
||||
static LoginDEMO loginDemo = new LoginDEMO() ;
|
||||
static ChatDEMO chatRoom = new ChatDEMO() ;
|
||||
|
||||
@SuppressWarnings( "javadoc" )
|
||||
public static void main( String[] args ) throws IOException
|
||||
{
|
||||
|
||||
loginDemo.setVisible( true );
|
||||
|
||||
boolean done = false ;
|
||||
while( !done )
|
||||
{
|
||||
done = loginDemo.isDone() ;
|
||||
System.out.print("");
|
||||
}
|
||||
|
||||
String ip = loginDemo.getIP() ;
|
||||
String username = loginDemo.getUsername() ;
|
||||
|
||||
// Open your connection to a server, at port 6789
|
||||
Socket socket = new Socket( ip , 6789 ) ;
|
||||
|
||||
// Create communication streams
|
||||
DataInputStream dataStreamIn = new DataInputStream( socket.getInputStream() ) ;
|
||||
DataOutputStream dataStreamOut = new DataOutputStream( socket.getOutputStream() ) ;
|
||||
|
||||
dataStreamOut.writeUTF( username ) ;
|
||||
|
||||
chatRoom.setTitle( "logged in as: " + username ) ;
|
||||
chatRoom.setVisible( true ) ;
|
||||
|
||||
// Thread for sending messages
|
||||
Thread sendMessage = new Thread(new Runnable()
|
||||
{
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
while( true )
|
||||
{
|
||||
try
|
||||
{
|
||||
// if-statement is only triggered if client closes window (disconnects)
|
||||
if( !chatRoom.isVisible() )
|
||||
{
|
||||
dataStreamOut.writeUTF( "/quit" ) ;
|
||||
dataStreamIn.close() ;
|
||||
dataStreamOut.close() ;
|
||||
socket.close() ;
|
||||
System.exit( 0 ) ;
|
||||
}
|
||||
// extract message information from GUI text field
|
||||
if( chatRoom.messageReady )
|
||||
{
|
||||
String messageOut = chatRoom.getMessageOut() ;
|
||||
if( messageOut == null )
|
||||
{
|
||||
continue ;
|
||||
}
|
||||
dataStreamOut.writeUTF( messageOut ) ;
|
||||
}
|
||||
System.out.print("") ;
|
||||
}
|
||||
catch( IOException x )
|
||||
{
|
||||
x.printStackTrace() ;
|
||||
System.exit( 0 ) ;
|
||||
}
|
||||
}
|
||||
}
|
||||
}) ;
|
||||
|
||||
// Thread for receiving messages
|
||||
Thread receiveMessage = new Thread( new Runnable()
|
||||
{
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
while( true )
|
||||
{
|
||||
try
|
||||
{
|
||||
String messageIn = dataStreamIn.readUTF() ;
|
||||
chatRoom.printMessage( messageIn ) ;
|
||||
}
|
||||
catch( IOException x )
|
||||
{
|
||||
x.printStackTrace() ;
|
||||
System.exit( 0 ) ;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Begin threads
|
||||
sendMessage.start() ;
|
||||
receiveMessage.start() ;
|
||||
|
||||
}
|
||||
}
|
||||
// end class ClientGUI
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,158 @@
|
||||
package demonstration;
|
||||
|
||||
@SuppressWarnings( "javadoc" )
|
||||
public class LoginDEMO extends javax.swing.JFrame {
|
||||
|
||||
/**
|
||||
* Creates new form loginGUI
|
||||
*/
|
||||
|
||||
public String ip ;
|
||||
public String username ;
|
||||
public boolean done ;
|
||||
|
||||
public String getIP()
|
||||
{
|
||||
return this.ip ;
|
||||
}
|
||||
|
||||
public String getUsername()
|
||||
{
|
||||
return this.username ;
|
||||
}
|
||||
|
||||
public boolean isDone()
|
||||
{
|
||||
return this.done ;
|
||||
}
|
||||
|
||||
public LoginDEMO() {
|
||||
initComponents();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
// <editor-fold defaultstate="collapsed" desc="Generated Code">
|
||||
private void initComponents() {
|
||||
|
||||
jPasswordField1 = new javax.swing.JPasswordField();
|
||||
jLabel1 = new javax.swing.JLabel();
|
||||
jTextField1 = new javax.swing.JTextField();
|
||||
jLabel2 = new javax.swing.JLabel();
|
||||
jLabel3 = new javax.swing.JLabel();
|
||||
|
||||
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
|
||||
setTitle("java chat login DEMO");
|
||||
setAlwaysOnTop(true);
|
||||
setResizable(false);
|
||||
|
||||
jPasswordField1.setEditable(false);
|
||||
jPasswordField1.setHorizontalAlignment(javax.swing.JTextField.CENTER);
|
||||
jPasswordField1.setText("12.34.56.78");
|
||||
|
||||
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
|
||||
jLabel1.setText("Enter Server's IP:");
|
||||
|
||||
jTextField1.setHorizontalAlignment(javax.swing.JTextField.CENTER);
|
||||
jTextField1.setToolTipText("");
|
||||
jTextField1.addActionListener(new java.awt.event.ActionListener() {
|
||||
public void actionPerformed(java.awt.event.ActionEvent evt) {
|
||||
jTextField1ActionPerformed(evt);
|
||||
}
|
||||
});
|
||||
|
||||
jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
|
||||
jLabel2.setText("Enter Username:");
|
||||
|
||||
jLabel3.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
|
||||
jLabel3.setText("username must be at least one character");
|
||||
jLabel3.setEnabled(false);
|
||||
|
||||
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
|
||||
getContentPane().setLayout(layout);
|
||||
layout.setHorizontalGroup(
|
||||
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
||||
.addGroup(layout.createSequentialGroup()
|
||||
.addContainerGap(43, Short.MAX_VALUE)
|
||||
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
||||
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
|
||||
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
|
||||
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
|
||||
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
|
||||
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
|
||||
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
|
||||
.addComponent(jTextField1)
|
||||
.addComponent(jPasswordField1, javax.swing.GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE))
|
||||
.addGap(40, 40, 40))
|
||||
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
|
||||
.addComponent(jLabel3)
|
||||
.addContainerGap())))
|
||||
);
|
||||
layout.setVerticalGroup(
|
||||
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
||||
.addGroup(layout.createSequentialGroup()
|
||||
.addGap(40, 40, 40)
|
||||
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
|
||||
.addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
|
||||
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
|
||||
.addGap(18, 18, 18)
|
||||
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
|
||||
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
|
||||
.addComponent(jLabel2))
|
||||
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
|
||||
.addComponent(jLabel3)
|
||||
.addContainerGap(22, Short.MAX_VALUE))
|
||||
);
|
||||
|
||||
pack();
|
||||
setLocationRelativeTo(null);
|
||||
}// </editor-fold>
|
||||
|
||||
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
|
||||
this.ip = "74.69.251.12" ;
|
||||
if( jTextField1.getText().isBlank() )
|
||||
{
|
||||
jLabel3.setEnabled( true );
|
||||
}
|
||||
else
|
||||
{
|
||||
this.username = jTextField1.getText() ;
|
||||
this.done = true ;
|
||||
dispose();
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
try {
|
||||
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
|
||||
if ("Nimbus".equals(info.getName())) {
|
||||
javax.swing.UIManager.setLookAndFeel(info.getClassName());
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch (ClassNotFoundException ex) {
|
||||
java.util.logging.Logger.getLogger(LoginDEMO.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
|
||||
} catch (InstantiationException ex) {
|
||||
java.util.logging.Logger.getLogger(LoginDEMO.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
|
||||
} catch (IllegalAccessException ex) {
|
||||
java.util.logging.Logger.getLogger(LoginDEMO.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
|
||||
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
|
||||
java.util.logging.Logger.getLogger(LoginDEMO.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
|
||||
}
|
||||
//</editor-fold>
|
||||
|
||||
/* Create and display the form */
|
||||
java.awt.EventQueue.invokeLater(new Runnable() {
|
||||
public void run() {
|
||||
new LoginDEMO().setVisible(true);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Variables declaration - do not modify
|
||||
private javax.swing.JLabel jLabel1;
|
||||
private javax.swing.JLabel jLabel2;
|
||||
private javax.swing.JLabel jLabel3;
|
||||
private javax.swing.JPasswordField jPasswordField1;
|
||||
private javax.swing.JTextField jTextField1;
|
||||
// End of variables declaration
|
||||
}
|
||||
Reference in New Issue
Block a user