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.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,4 +1,4 @@
|
|||||||
package javaChat;
|
package backend;
|
||||||
|
|
||||||
import java.net.* ;
|
import java.net.* ;
|
||||||
|
|
||||||
@@ -6,7 +6,7 @@ import javax.swing.JFrame ;
|
|||||||
|
|
||||||
import java.io.* ;
|
import java.io.* ;
|
||||||
|
|
||||||
public class ChatClient
|
public class ChatClientCMD
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Binary file not shown.
@@ -0,0 +1,309 @@
|
|||||||
|
|
||||||
|
package backend ;
|
||||||
|
|
||||||
|
import java.io.DataInputStream ;
|
||||||
|
import java.io.DataOutputStream ;
|
||||||
|
import java.io.IOException ;
|
||||||
|
import java.net.ServerSocket ;
|
||||||
|
import java.net.Socket ;
|
||||||
|
import java.util.ArrayList ;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ChatServer class accepts Client requests to join
|
||||||
|
* and creates an individual thread for each client.
|
||||||
|
* The "brain" of the server.
|
||||||
|
*
|
||||||
|
* @author osheas1
|
||||||
|
* @author gohringj
|
||||||
|
* @version Semi-Final Draft
|
||||||
|
*/
|
||||||
|
public class ChatServer
|
||||||
|
{
|
||||||
|
|
||||||
|
// List of all active users in Server
|
||||||
|
public static ArrayList<ClientHandler> userList = new ArrayList<>() ;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param args (unused)
|
||||||
|
* @throws IOException (is thrown if connection is unexpectedly closed)
|
||||||
|
*/
|
||||||
|
public static void main( final String[] args ) throws IOException
|
||||||
|
{
|
||||||
|
// Register service on port 6789
|
||||||
|
ServerSocket serverSocket = new ServerSocket( 6789 ) ;
|
||||||
|
Socket socket = null ;
|
||||||
|
|
||||||
|
System.out.println( "\nServer established. Awaiting connections..." ) ;
|
||||||
|
|
||||||
|
// Loop to accept client requests
|
||||||
|
while ( true )
|
||||||
|
{
|
||||||
|
socket = serverSocket.accept() ;
|
||||||
|
|
||||||
|
DataOutputStream dataStreamOut = new DataOutputStream( socket.getOutputStream() ) ;
|
||||||
|
DataInputStream dataStreamIn = new DataInputStream( socket.getInputStream() ) ;
|
||||||
|
|
||||||
|
System.out.println( "\nClient connection detected, requesting username..." ) ;
|
||||||
|
String username = dataStreamIn.readUTF() ;
|
||||||
|
|
||||||
|
ClientHandler handle = new ClientHandler( socket, username, dataStreamOut, dataStreamIn ) ;
|
||||||
|
|
||||||
|
System.out.println( "\nCreating new thread...\n" ) ;
|
||||||
|
Thread thread = new Thread( handle ) ;
|
||||||
|
|
||||||
|
userList.add( handle ) ;
|
||||||
|
|
||||||
|
thread.start() ;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} // end class ChatServer
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ClientHandler class manages the actual messages being sent
|
||||||
|
* between the users. The "heart" of the server.
|
||||||
|
*
|
||||||
|
* @author osheas1
|
||||||
|
* @author gohringj
|
||||||
|
* @version Semi-Final Draft
|
||||||
|
*/
|
||||||
|
|
||||||
|
class ClientHandler implements Runnable
|
||||||
|
{
|
||||||
|
Socket socket ;
|
||||||
|
String username ;
|
||||||
|
DataOutputStream dataStreamOut ;
|
||||||
|
DataInputStream dataStreamIn ;
|
||||||
|
boolean loggedIn ;
|
||||||
|
int commandCheck ;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
* @param s = client socket
|
||||||
|
* @param user = client's user-name
|
||||||
|
* @param dos = data stream to client
|
||||||
|
* @param dis = data stream from client
|
||||||
|
*/
|
||||||
|
public ClientHandler( Socket s, String user, DataOutputStream dos, DataInputStream dis )
|
||||||
|
{
|
||||||
|
this.socket = s ;
|
||||||
|
this.username = user ;
|
||||||
|
this.dataStreamOut = dos ;
|
||||||
|
this.dataStreamIn = dis ;
|
||||||
|
this.loggedIn = true ;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run()
|
||||||
|
{
|
||||||
|
// Note the connection of the new user
|
||||||
|
String announcement = String.format( "\n>>> User \"%s\" has successfully connected (type /help for a list of commands) <<<\n", this.username ) ;
|
||||||
|
System.out.println( announcement ) ;
|
||||||
|
|
||||||
|
// Distribute announcement to entire server
|
||||||
|
sendMessage( announcement ) ;
|
||||||
|
|
||||||
|
// Message management
|
||||||
|
String message = "" ;
|
||||||
|
while ( true )
|
||||||
|
{
|
||||||
|
// Try-Catch block interprets incoming messages
|
||||||
|
try
|
||||||
|
{
|
||||||
|
message = this.dataStreamIn.readUTF() ;
|
||||||
|
|
||||||
|
if(message == "" || message == " ")
|
||||||
|
{
|
||||||
|
continue ;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch ( IOException x )
|
||||||
|
{
|
||||||
|
x.printStackTrace() ;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check message for user commands, returns TRUE if user "/quit"s
|
||||||
|
if( commands( message, this ) )
|
||||||
|
{
|
||||||
|
break ;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Print the message server-side
|
||||||
|
message = String.format( "%s: %s", this.username, message ) ;
|
||||||
|
System.out.println( message ) ;
|
||||||
|
|
||||||
|
// Distribute the message to all users
|
||||||
|
if ( this.commandCheck == 0 )
|
||||||
|
{
|
||||||
|
sendMessage( message ) ;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
this.commandCheck = 0 ;
|
||||||
|
}
|
||||||
|
} // end while
|
||||||
|
|
||||||
|
// close streams when user disconnects
|
||||||
|
try
|
||||||
|
{
|
||||||
|
this.loggedIn = false ;
|
||||||
|
this.dataStreamIn.close() ;
|
||||||
|
this.dataStreamOut.close() ;
|
||||||
|
this.socket.close() ;
|
||||||
|
}
|
||||||
|
catch ( IOException x )
|
||||||
|
{
|
||||||
|
x.printStackTrace() ;
|
||||||
|
}
|
||||||
|
} // end run
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The 'commands' method manages the usage of user-commands
|
||||||
|
* by distributing private messages, providing a user-list,
|
||||||
|
* etc.
|
||||||
|
*
|
||||||
|
* @param message = the command itself
|
||||||
|
* @param user = the user that
|
||||||
|
* @return = boolean that indicates that the user has disconnected
|
||||||
|
*/
|
||||||
|
public boolean commands( String message, final ClientHandler user )
|
||||||
|
{
|
||||||
|
final String[] commands = new String[] { "/help", "/quit", "/userlist", "/pm" } ;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Quit command terminates client's connection, with an announcement
|
||||||
|
if ( message.equals( "/quit" ) )
|
||||||
|
{
|
||||||
|
this.commandCheck++ ;
|
||||||
|
String announcement = String.format( "\n-= \"%s\" has disconnected =- \n", user.username ) ;
|
||||||
|
System.out.println( announcement ) ;
|
||||||
|
sendMessage( announcement ) ;
|
||||||
|
return true ;
|
||||||
|
}
|
||||||
|
|
||||||
|
// User-List command provides the user a list of everyone else connected to the server
|
||||||
|
if ( message.equals( "/userlist" ) )
|
||||||
|
{
|
||||||
|
this.commandCheck++ ;
|
||||||
|
user.dataStreamOut.writeUTF( "> Current list of users in the server: " ) ;
|
||||||
|
for ( ClientHandler users : ChatServer.userList )
|
||||||
|
{
|
||||||
|
if ( !users.loggedIn )
|
||||||
|
{
|
||||||
|
continue ;
|
||||||
|
}
|
||||||
|
|
||||||
|
user.dataStreamOut.writeUTF( "> " + users.username ) ;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Help command lists all available commands
|
||||||
|
if ( message.equals( "/help" ) )
|
||||||
|
{
|
||||||
|
this.commandCheck++ ;
|
||||||
|
user.dataStreamOut.writeUTF( "\n> All available commands: " ) ;
|
||||||
|
for ( String command : commands )
|
||||||
|
{
|
||||||
|
user.dataStreamOut.writeUTF( "> " +command ) ;
|
||||||
|
}
|
||||||
|
|
||||||
|
user.dataStreamOut.writeUTF( "\n" ) ;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Private Message Command allows users to send messages to a single recipient
|
||||||
|
if ( message.contains( "/pm" ) )
|
||||||
|
{
|
||||||
|
this.commandCheck++ ;
|
||||||
|
user.dataStreamOut.writeUTF( "\n> Who would you like to private message?" ) ;
|
||||||
|
String recipient = this.dataStreamIn.readUTF() ;
|
||||||
|
user.dataStreamOut.writeUTF( "> \"" + recipient + "\"" ) ;
|
||||||
|
|
||||||
|
ClientHandler sendTo = null ;
|
||||||
|
|
||||||
|
// loop determines which ClientHandler object to send the message to
|
||||||
|
for ( ClientHandler users : ChatServer.userList )
|
||||||
|
{
|
||||||
|
if ( !users.loggedIn )
|
||||||
|
{
|
||||||
|
continue ;
|
||||||
|
}
|
||||||
|
else if ( users.username.equals( recipient ) )
|
||||||
|
{
|
||||||
|
sendTo = users ;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( sendTo == null )
|
||||||
|
{
|
||||||
|
user.dataStreamOut.writeUTF( "\n> User not found, please try again" ) ;
|
||||||
|
return false ;
|
||||||
|
}
|
||||||
|
|
||||||
|
user.dataStreamOut.writeUTF( "\n> What would you like to say?" ) ;
|
||||||
|
String privateMessage = this.dataStreamIn.readUTF() ;
|
||||||
|
user.dataStreamOut.writeUTF( "> sent: \"" + privateMessage + "\"" ) ;
|
||||||
|
sendPrivateMessage( privateMessage, user.username, sendTo ) ;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
catch ( IOException x )
|
||||||
|
{
|
||||||
|
while ( ChatServer.userList.size() != 0 )
|
||||||
|
{
|
||||||
|
System.out.println( x ) ;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false ;
|
||||||
|
} // end commands
|
||||||
|
|
||||||
|
|
||||||
|
// Convenience method to send messages privately
|
||||||
|
private static void sendPrivateMessage( String message, final String sentfrom, final ClientHandler sendto )
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
for ( ClientHandler users : ChatServer.userList )
|
||||||
|
{
|
||||||
|
if ( users.equals( sendto ) )
|
||||||
|
{
|
||||||
|
message = String.format( "\nFrom <%s>: %s", sentfrom, message ) ;
|
||||||
|
users.dataStreamOut.writeUTF( message ) ;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch ( final IOException x )
|
||||||
|
{
|
||||||
|
x.printStackTrace() ;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Convenience method to distribute messages to all clients
|
||||||
|
private static void sendMessage( String message )
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
for ( ClientHandler users : ChatServer.userList )
|
||||||
|
{
|
||||||
|
if ( !users.loggedIn )
|
||||||
|
{
|
||||||
|
continue ;
|
||||||
|
}
|
||||||
|
users.dataStreamOut.writeUTF( message ) ;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch ( IOException x )
|
||||||
|
{
|
||||||
|
x.printStackTrace() ;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// end class ClientHandler
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,167 @@
|
|||||||
|
|
||||||
|
package chatApp ;
|
||||||
|
|
||||||
|
@SuppressWarnings( "javadoc" )
|
||||||
|
public class ChatGUI 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 ChatGUI() {
|
||||||
|
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(ChatGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
|
||||||
|
} catch (InstantiationException ex) {
|
||||||
|
java.util.logging.Logger.getLogger(ChatGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
|
||||||
|
} catch (IllegalAccessException ex) {
|
||||||
|
java.util.logging.Logger.getLogger(ChatGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
|
||||||
|
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
|
||||||
|
java.util.logging.Logger.getLogger(ChatGUI.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 ChatGUI().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.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,22 +1,18 @@
|
|||||||
package javaChat;
|
package chatApp;
|
||||||
|
|
||||||
import java.net.* ;
|
import java.net.* ;
|
||||||
import java.io.* ;
|
import java.io.* ;
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @author osheas1
|
|
||||||
* @version 1.0.0 2020-12-07 Initial implementation
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class ClientGUI
|
public class ClientGUI
|
||||||
{
|
{
|
||||||
|
|
||||||
|
static LoginGUI login = new LoginGUI() ;
|
||||||
|
static ChatGUI chatRoom = new ChatGUI() ;
|
||||||
|
|
||||||
|
@SuppressWarnings( "javadoc" )
|
||||||
public static void main( String[] args ) throws IOException
|
public static void main( String[] args ) throws IOException
|
||||||
{
|
{
|
||||||
|
|
||||||
UserInterface login = new UserInterface() ;
|
|
||||||
login.setVisible( true );
|
login.setVisible( true );
|
||||||
|
|
||||||
boolean done = false ;
|
boolean done = false ;
|
||||||
@@ -36,24 +32,42 @@ public class ClientGUI
|
|||||||
DataInputStream dataStreamIn = new DataInputStream( socket.getInputStream() ) ;
|
DataInputStream dataStreamIn = new DataInputStream( socket.getInputStream() ) ;
|
||||||
DataOutputStream dataStreamOut = new DataOutputStream( socket.getOutputStream() ) ;
|
DataOutputStream dataStreamOut = new DataOutputStream( socket.getOutputStream() ) ;
|
||||||
|
|
||||||
BufferedReader keyboard = new BufferedReader( new InputStreamReader( System.in ) ) ;
|
|
||||||
|
|
||||||
dataStreamOut.writeUTF( username ) ;
|
dataStreamOut.writeUTF( username ) ;
|
||||||
|
|
||||||
|
chatRoom.setTitle( "logged in as: " + username ) ;
|
||||||
|
chatRoom.setVisible( true ) ;
|
||||||
|
|
||||||
// Thread for sending messages
|
// Thread for sending messages
|
||||||
Thread sendMessage = new Thread(new Runnable()
|
Thread sendMessage = new Thread(new Runnable()
|
||||||
{
|
{
|
||||||
@Override
|
@Override
|
||||||
public void run()
|
public void run()
|
||||||
{
|
{
|
||||||
System.out.print( "\n-= Type /help for a list of commands =-" ) ;
|
|
||||||
while( true )
|
while( true )
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
String messageOut = keyboard.readLine() ;
|
// 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 ) ;
|
dataStreamOut.writeUTF( messageOut ) ;
|
||||||
}
|
}
|
||||||
|
System.out.print("") ;
|
||||||
|
}
|
||||||
catch( IOException x )
|
catch( IOException x )
|
||||||
{
|
{
|
||||||
x.printStackTrace() ;
|
x.printStackTrace() ;
|
||||||
@@ -75,7 +89,7 @@ public class ClientGUI
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
String messageIn = dataStreamIn.readUTF() ;
|
String messageIn = dataStreamIn.readUTF() ;
|
||||||
System.out.printf( "%n%s\n", messageIn ) ;
|
chatRoom.printMessage( messageIn ) ;
|
||||||
}
|
}
|
||||||
catch( IOException x )
|
catch( IOException x )
|
||||||
{
|
{
|
||||||
@@ -90,8 +104,6 @@ public class ClientGUI
|
|||||||
sendMessage.start() ;
|
sendMessage.start() ;
|
||||||
receiveMessage.start() ;
|
receiveMessage.start() ;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
// end class ClientGUI
|
// end class ClientGUI
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,182 @@
|
|||||||
|
package chatApp;
|
||||||
|
|
||||||
|
import java.awt.* ;
|
||||||
|
import java.util.* ;
|
||||||
|
import javax.swing.* ;
|
||||||
|
|
||||||
|
public class LoginGUI extends javax.swing.JFrame {
|
||||||
|
|
||||||
|
|
||||||
|
String ip ;
|
||||||
|
String username ;
|
||||||
|
boolean done ;
|
||||||
|
|
||||||
|
|
||||||
|
public String getIP()
|
||||||
|
{
|
||||||
|
return ip ;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUsername()
|
||||||
|
{
|
||||||
|
return username ;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isDone()
|
||||||
|
{
|
||||||
|
return done ;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public LoginGUI() {
|
||||||
|
initComponents();
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
// <editor-fold defaultstate="collapsed" desc="Generated Code">
|
||||||
|
private void initComponents()
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
jLabel1 = new javax.swing.JLabel();
|
||||||
|
jTextField1 = new javax.swing.JTextField();
|
||||||
|
jLabel2 = new javax.swing.JLabel();
|
||||||
|
jLabel3 = new javax.swing.JLabel();
|
||||||
|
ipBox = new javax.swing.JTextField();
|
||||||
|
|
||||||
|
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
|
||||||
|
setTitle("java chat login");
|
||||||
|
setAlwaysOnTop(true);
|
||||||
|
setResizable(false);
|
||||||
|
|
||||||
|
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
|
||||||
|
jLabel1.setText("Enter Server's IP:");
|
||||||
|
|
||||||
|
jTextField1.setHorizontalAlignment(javax.swing.JTextField.CENTER);
|
||||||
|
ipBox.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("inputs must be at least one character");
|
||||||
|
jLabel3.setEnabled(false);
|
||||||
|
|
||||||
|
ipBox.addActionListener(new java.awt.event.ActionListener() {
|
||||||
|
public void actionPerformed(java.awt.event.ActionEvent evt) {
|
||||||
|
jTextField2ActionPerformed(evt);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
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, javax.swing.GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE)
|
||||||
|
.addComponent(ipBox))
|
||||||
|
.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(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
|
||||||
|
.addComponent(ipBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, 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 jTextField2ActionPerformed(java.awt.event.ActionEvent evt)
|
||||||
|
{
|
||||||
|
if( ipBox.getText().isBlank() )
|
||||||
|
{
|
||||||
|
jLabel3.setEnabled( true );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
jTextField1.grabFocus();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt)
|
||||||
|
{
|
||||||
|
if( jTextField1.getText().isBlank() || ipBox.getText().isBlank() )
|
||||||
|
{
|
||||||
|
jLabel3.setEnabled( true );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
this.ip = ipBox.getText() ;
|
||||||
|
this.username = jTextField1.getText() ;
|
||||||
|
this.done = true ;
|
||||||
|
dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @param args the command line arguments
|
||||||
|
*/
|
||||||
|
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(LoginGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
|
||||||
|
} catch (InstantiationException ex) {
|
||||||
|
java.util.logging.Logger.getLogger(LoginGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
|
||||||
|
} catch (IllegalAccessException ex) {
|
||||||
|
java.util.logging.Logger.getLogger(LoginGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
|
||||||
|
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
|
||||||
|
java.util.logging.Logger.getLogger(LoginGUI.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 LoginGUI().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.JTextField jTextField1;
|
||||||
|
private javax.swing.JTextField ipBox;
|
||||||
|
// End of variables declaration
|
||||||
|
}
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
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
|
||||||
|
}
|
||||||
@@ -1,237 +0,0 @@
|
|||||||
package javaChat;
|
|
||||||
|
|
||||||
import java.net.* ;
|
|
||||||
import java.util.* ;
|
|
||||||
import java.io.* ;
|
|
||||||
|
|
||||||
public class ChatServer
|
|
||||||
{
|
|
||||||
|
|
||||||
// List of all active users in Server
|
|
||||||
static ArrayList<ClientHandler> userList = new ArrayList<>() ;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Accepts Client requests to join chat server, and
|
|
||||||
* creates new thread for each individual client.
|
|
||||||
*
|
|
||||||
* @param args
|
|
||||||
* @throws IOException
|
|
||||||
*/
|
|
||||||
public static void main( String[] args ) throws IOException
|
|
||||||
{
|
|
||||||
|
|
||||||
// Register service on port 6789
|
|
||||||
ServerSocket serverSocket = new ServerSocket( 6789 ) ;
|
|
||||||
Socket socket = null ;
|
|
||||||
|
|
||||||
System.out.println( "\nServer established. Awaiting connections..." ) ;
|
|
||||||
|
|
||||||
// Accept client requests
|
|
||||||
while( true )
|
|
||||||
{
|
|
||||||
socket = serverSocket.accept() ;
|
|
||||||
|
|
||||||
DataOutputStream dataStreamOut = new DataOutputStream( socket.getOutputStream() ) ;
|
|
||||||
DataInputStream dataStreamIn = new DataInputStream ( socket.getInputStream() ) ;
|
|
||||||
|
|
||||||
System.out.println( "\nClient connection detected, requesting username..." ) ;
|
|
||||||
String username = dataStreamIn.readUTF() ;
|
|
||||||
|
|
||||||
ClientHandler handle = new ClientHandler( socket, username, dataStreamOut, dataStreamIn ) ;
|
|
||||||
|
|
||||||
System.out.println( "\nCreating new thread...\n" ) ;
|
|
||||||
Thread thread = new Thread( handle ) ;
|
|
||||||
|
|
||||||
userList.add( handle ) ;
|
|
||||||
|
|
||||||
thread.start() ;
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
// end class ChatClient
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// This class manages the actual messages being sent around
|
|
||||||
@SuppressWarnings( "javadoc" )
|
|
||||||
class ClientHandler implements Runnable
|
|
||||||
{
|
|
||||||
|
|
||||||
Socket socket ;
|
|
||||||
String username ;
|
|
||||||
final DataOutputStream dataStreamOut ;
|
|
||||||
final DataInputStream dataStreamIn ;
|
|
||||||
boolean loggedIn ;
|
|
||||||
private ArrayList<ClientHandler> userList ;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor
|
|
||||||
* @param s : client socket
|
|
||||||
* @param user : client's user-name
|
|
||||||
* @param dos : data stream to client
|
|
||||||
* @param dis : data stream from client
|
|
||||||
*/
|
|
||||||
public ClientHandler( Socket s, String user,
|
|
||||||
DataOutputStream dos,
|
|
||||||
DataInputStream dis )
|
|
||||||
{
|
|
||||||
this.username = user ;
|
|
||||||
this.socket = s ;
|
|
||||||
this.dataStreamOut = dos ;
|
|
||||||
this.dataStreamIn = dis ;
|
|
||||||
this.loggedIn = true ;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run()
|
|
||||||
{
|
|
||||||
|
|
||||||
// Note the connection of the new user
|
|
||||||
String announcement = String.format( "-= User \"%s\" has successfully connected =-\n", this.username ) ;
|
|
||||||
System.out.println( announcement ) ;
|
|
||||||
|
|
||||||
// Distribute announcement to entire server
|
|
||||||
distributeAll( announcement ) ;
|
|
||||||
|
|
||||||
// Message management
|
|
||||||
String message = "" ;
|
|
||||||
while( true )
|
|
||||||
{
|
|
||||||
// Read incoming messages
|
|
||||||
try
|
|
||||||
{
|
|
||||||
message = this.dataStreamIn.readUTF() ;
|
|
||||||
}
|
|
||||||
catch ( IOException x )
|
|
||||||
{
|
|
||||||
x.printStackTrace() ;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check message for user commands, returns TRUE is user quits
|
|
||||||
if( commands(message, this) )
|
|
||||||
{
|
|
||||||
break ;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Print the message server-side
|
|
||||||
message = String.format( "%s: %s", this.username, message ) ;
|
|
||||||
System.out.println( message ) ;
|
|
||||||
|
|
||||||
// Distribute the message to all users
|
|
||||||
sendMessage( message ) ;
|
|
||||||
// end try
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
this.loggedIn = false ;
|
|
||||||
this.socket.close() ;
|
|
||||||
}
|
|
||||||
catch( IOException x )
|
|
||||||
{
|
|
||||||
x.printStackTrace() ;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// This method manages the usage of user-commands
|
|
||||||
public boolean commands(String message, ClientHandler user)
|
|
||||||
{
|
|
||||||
String[] commands = new String[] {"/help", "/quit", "/userlist"} ;
|
|
||||||
|
|
||||||
try {
|
|
||||||
|
|
||||||
// Quit command terminates connection, with an announcement
|
|
||||||
if( message.equals( "/quit" ))
|
|
||||||
{
|
|
||||||
String announcement = String.format( "\n-= \"%s\" has disconnected =- \n", user.username ) ;
|
|
||||||
System.out.println( announcement ) ;
|
|
||||||
distributeAll( announcement ) ;
|
|
||||||
return true ;
|
|
||||||
}
|
|
||||||
|
|
||||||
if( message.equals( "/userlist" ) )
|
|
||||||
{
|
|
||||||
user.dataStreamOut.writeUTF( "Current list of users in the server: " ) ;
|
|
||||||
for( ClientHandler users : ChatServer.userList )
|
|
||||||
{
|
|
||||||
if( !users.loggedIn )
|
|
||||||
{
|
|
||||||
continue ;
|
|
||||||
}
|
|
||||||
|
|
||||||
user.dataStreamOut.writeUTF( users.username ) ;
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if( message.contentEquals( "/help" ) )
|
|
||||||
{
|
|
||||||
user.dataStreamOut.writeUTF( "\nAll available commands: " );
|
|
||||||
for(int i = 0; i < commands.length; i++ )
|
|
||||||
{
|
|
||||||
user.dataStreamOut.writeUTF( commands[i] );
|
|
||||||
}
|
|
||||||
|
|
||||||
user.dataStreamOut.writeUTF( "\n" ) ;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
catch( IOException x )
|
|
||||||
{
|
|
||||||
System.out.println( x ) ;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false ;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Convenience method to send messages (but not to the user that's sending the message)
|
|
||||||
private void sendMessage(String message)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
for( ClientHandler users : ChatServer.userList )
|
|
||||||
{
|
|
||||||
if( !users.loggedIn || users.equals( this ) )
|
|
||||||
{
|
|
||||||
continue ;
|
|
||||||
}
|
|
||||||
users.dataStreamOut.writeUTF( "> " + message ) ;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch ( IOException x )
|
|
||||||
{
|
|
||||||
x.printStackTrace() ;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Convenience method to distribute messages to all clients (announcements)
|
|
||||||
private void distributeAll(String message)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
for( ClientHandler users : ChatServer.userList )
|
|
||||||
{
|
|
||||||
if( !users.loggedIn )
|
|
||||||
{
|
|
||||||
continue ;
|
|
||||||
}
|
|
||||||
users.dataStreamOut.writeUTF( message ) ;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch ( IOException x )
|
|
||||||
{
|
|
||||||
x.printStackTrace() ;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
// end class ClientHandler
|
|
||||||
@@ -1,288 +0,0 @@
|
|||||||
package javaChat;
|
|
||||||
|
|
||||||
import java.awt.* ;
|
|
||||||
import java.util.* ;
|
|
||||||
import javax.swing.* ;
|
|
||||||
|
|
||||||
public class UserInterface extends javax.swing.JFrame {
|
|
||||||
|
|
||||||
|
|
||||||
String ip ;
|
|
||||||
String username ;
|
|
||||||
boolean done ;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates new form loginFrame
|
|
||||||
*/
|
|
||||||
public UserInterface() {
|
|
||||||
initComponents();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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">
|
|
||||||
private void initComponents()
|
|
||||||
{
|
|
||||||
|
|
||||||
jTextField1 = new javax.swing.JTextField();
|
|
||||||
jLabel1 = new javax.swing.JLabel();
|
|
||||||
jLabel2 = new javax.swing.JLabel();
|
|
||||||
jTextField2 = new javax.swing.JTextField();
|
|
||||||
jButton1 = new javax.swing.JButton();
|
|
||||||
jSlider1 = new javax.swing.JSlider();
|
|
||||||
jSlider2 = new javax.swing.JSlider();
|
|
||||||
jSlider3 = new javax.swing.JSlider();
|
|
||||||
jSlider4 = new javax.swing.JSlider();
|
|
||||||
|
|
||||||
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
|
|
||||||
setTitle("Java Chat");
|
|
||||||
setAlwaysOnTop(true);
|
|
||||||
setBackground(new java.awt.Color(255, 255, 255));
|
|
||||||
setForeground(java.awt.Color.white);
|
|
||||||
setResizable(false);
|
|
||||||
|
|
||||||
jTextField1.setEditable(false);
|
|
||||||
jTextField1.setHorizontalAlignment(javax.swing.JTextField.CENTER);
|
|
||||||
jTextField1.setText("0.0.0.0");
|
|
||||||
|
|
||||||
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
|
|
||||||
jLabel1.setText("Enter Server's I.P. Address:");
|
|
||||||
|
|
||||||
jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
|
|
||||||
jLabel2.setText("Enter your desired username:");
|
|
||||||
|
|
||||||
jTextField2.setHorizontalAlignment(javax.swing.JTextField.CENTER);
|
|
||||||
jTextField2.setActionCommand("<Not Set>");
|
|
||||||
|
|
||||||
jButton1.setText("Connect");
|
|
||||||
jButton1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
|
|
||||||
jButton1.setOpaque(false);
|
|
||||||
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
|
|
||||||
public void mouseClicked(java.awt.event.MouseEvent evt) {
|
|
||||||
jButton1MouseClicked(evt);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
jSlider1.setMaximum(999);
|
|
||||||
jSlider1.setOrientation(javax.swing.JSlider.VERTICAL);
|
|
||||||
jSlider1.setValue(0);
|
|
||||||
jSlider1.addChangeListener(new javax.swing.event.ChangeListener() {
|
|
||||||
public void stateChanged(javax.swing.event.ChangeEvent evt) {
|
|
||||||
jSlider1StateChanged(evt);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
jSlider2.setMaximum(999);
|
|
||||||
jSlider2.setOrientation(javax.swing.JSlider.VERTICAL);
|
|
||||||
jSlider2.setValue(0);
|
|
||||||
jSlider2.addChangeListener(new javax.swing.event.ChangeListener() {
|
|
||||||
public void stateChanged(javax.swing.event.ChangeEvent evt) {
|
|
||||||
jSlider2StateChanged(evt);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
jSlider3.setMaximum(999);
|
|
||||||
jSlider3.setOrientation(javax.swing.JSlider.VERTICAL);
|
|
||||||
jSlider3.setValue(0);
|
|
||||||
jSlider3.addChangeListener(new javax.swing.event.ChangeListener() {
|
|
||||||
public void stateChanged(javax.swing.event.ChangeEvent evt) {
|
|
||||||
jSlider3StateChanged(evt);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
jSlider4.setMaximum(999);
|
|
||||||
jSlider4.setOrientation(javax.swing.JSlider.VERTICAL);
|
|
||||||
jSlider4.setValue(0);
|
|
||||||
jSlider4.addChangeListener(new javax.swing.event.ChangeListener() {
|
|
||||||
public void stateChanged(javax.swing.event.ChangeEvent evt) {
|
|
||||||
jSlider4StateChanged(evt);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
|
|
||||||
getContentPane().setLayout(layout);
|
|
||||||
layout.setHorizontalGroup(
|
|
||||||
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
|
||||||
.addGroup(layout.createSequentialGroup()
|
|
||||||
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
|
||||||
.addGroup(layout.createSequentialGroup()
|
|
||||||
.addGap(60, 60, 60)
|
|
||||||
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
|
|
||||||
.addGroup(layout.createSequentialGroup()
|
|
||||||
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
|
|
||||||
.addGap(18, 18, 18)
|
|
||||||
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE))
|
|
||||||
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
|
|
||||||
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
|
|
||||||
.addGap(18, 18, 18)
|
|
||||||
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE))))
|
|
||||||
.addGroup(layout.createSequentialGroup()
|
|
||||||
.addGap(162, 162, 162)
|
|
||||||
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)))
|
|
||||||
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 60, Short.MAX_VALUE)
|
|
||||||
.addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
|
|
||||||
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
|
|
||||||
.addComponent(jSlider2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
|
|
||||||
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
|
|
||||||
.addComponent(jSlider3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
|
|
||||||
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
|
|
||||||
.addComponent(jSlider4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
|
|
||||||
.addGap(56, 56, 56))
|
|
||||||
);
|
|
||||||
layout.setVerticalGroup(
|
|
||||||
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
|
||||||
.addGroup(layout.createSequentialGroup()
|
|
||||||
.addGap(24, 24, 24)
|
|
||||||
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
|
|
||||||
.addGroup(layout.createSequentialGroup()
|
|
||||||
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
|
|
||||||
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
|
|
||||||
.addComponent(jSlider3, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
|
|
||||||
.addComponent(jSlider2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
|
|
||||||
.addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE))
|
|
||||||
.addComponent(jSlider4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE))
|
|
||||||
.addContainerGap(40, Short.MAX_VALUE))
|
|
||||||
.addGroup(layout.createSequentialGroup()
|
|
||||||
.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(jLabel1))
|
|
||||||
.addGap(36, 36, 36)
|
|
||||||
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
|
|
||||||
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
|
|
||||||
.addComponent(jLabel2))
|
|
||||||
.addGap(28, 28, 28)
|
|
||||||
.addComponent(jButton1)
|
|
||||||
.addGap(30, 30, 30))))
|
|
||||||
);
|
|
||||||
|
|
||||||
pack();
|
|
||||||
}// </editor-fold>
|
|
||||||
|
|
||||||
private void jButton1MouseClicked(java.awt.event.MouseEvent evt)
|
|
||||||
{
|
|
||||||
ip = jTextField1.getText() ;
|
|
||||||
username = jTextField2.getText() ;
|
|
||||||
//System.out.printf( "IP: %s \nUsername: %s\n", ip, username ) ;
|
|
||||||
|
|
||||||
|
|
||||||
done = true ;
|
|
||||||
setVisible( false ) ;
|
|
||||||
dispose() ;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private void jSlider1StateChanged(javax.swing.event.ChangeEvent evt)
|
|
||||||
{
|
|
||||||
Integer a = jSlider1.getValue() ;
|
|
||||||
Integer b = jSlider2.getValue() ;
|
|
||||||
Integer c = jSlider3.getValue() ;
|
|
||||||
Integer d = jSlider4.getValue() ;
|
|
||||||
|
|
||||||
String ip = String.format("%s.%s.%s.%s", a, b, c, d) ;
|
|
||||||
|
|
||||||
jTextField1.setText( ip.toString() ) ;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void jSlider2StateChanged(javax.swing.event.ChangeEvent evt)
|
|
||||||
{
|
|
||||||
Integer a = jSlider1.getValue() ;
|
|
||||||
Integer b = jSlider2.getValue() ;
|
|
||||||
Integer c = jSlider3.getValue() ;
|
|
||||||
Integer d = jSlider4.getValue() ;
|
|
||||||
|
|
||||||
String ip = String.format("%s.%s.%s.%s", a, b, c, d) ;
|
|
||||||
|
|
||||||
jTextField1.setText( ip.toString() ) ;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void jSlider3StateChanged(javax.swing.event.ChangeEvent evt)
|
|
||||||
{
|
|
||||||
Integer a = jSlider1.getValue() ;
|
|
||||||
Integer b = jSlider2.getValue() ;
|
|
||||||
Integer c = jSlider3.getValue() ;
|
|
||||||
Integer d = jSlider4.getValue() ;
|
|
||||||
|
|
||||||
String ip = String.format("%s.%s.%s.%s", a, b, c, d) ;
|
|
||||||
|
|
||||||
jTextField1.setText( ip.toString() ) ;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void jSlider4StateChanged(javax.swing.event.ChangeEvent evt)
|
|
||||||
{
|
|
||||||
Integer a = jSlider1.getValue() ;
|
|
||||||
Integer b = jSlider2.getValue() ;
|
|
||||||
Integer c = jSlider3.getValue() ;
|
|
||||||
Integer d = jSlider4.getValue() ;
|
|
||||||
|
|
||||||
String ip = String.format("%s.%s.%s.%s", a, b, c, d) ;
|
|
||||||
|
|
||||||
jTextField1.setText( ip.toString() ) ;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param args the command line arguments
|
|
||||||
*/
|
|
||||||
public static void main(String args[]) {
|
|
||||||
/* Set the Nimbus look and feel */
|
|
||||||
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
|
|
||||||
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
|
|
||||||
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
|
|
||||||
*/
|
|
||||||
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(UserInterface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
|
|
||||||
} catch (InstantiationException ex) {
|
|
||||||
java.util.logging.Logger.getLogger(UserInterface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
|
|
||||||
} catch (IllegalAccessException ex) {
|
|
||||||
java.util.logging.Logger.getLogger(UserInterface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
|
|
||||||
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
|
|
||||||
java.util.logging.Logger.getLogger(UserInterface.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 UserInterface().setVisible(true);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public String getIP()
|
|
||||||
{
|
|
||||||
return ip ;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getUsername()
|
|
||||||
{
|
|
||||||
return username ;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isDone()
|
|
||||||
{
|
|
||||||
return done ;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Variables declaration - do not modify
|
|
||||||
private javax.swing.JButton jButton1;
|
|
||||||
private javax.swing.JLabel jLabel1;
|
|
||||||
private javax.swing.JLabel jLabel2;
|
|
||||||
private javax.swing.JSlider jSlider1;
|
|
||||||
private javax.swing.JSlider jSlider2;
|
|
||||||
private javax.swing.JSlider jSlider3;
|
|
||||||
private javax.swing.JSlider jSlider4;
|
|
||||||
private javax.swing.JTextField jTextField1;
|
|
||||||
private javax.swing.JTextField jTextField2;
|
|
||||||
// End of variables declaration
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user