Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5c7452b378 | |||
| 6d632b546f |
-10
@@ -1,10 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk-13.0.1">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
@@ -1,17 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>javaChat</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
@@ -1,14 +0,0 @@
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=12
|
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||
org.eclipse.jdt.core.compiler.compliance=12
|
||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
|
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
|
||||
org.eclipse.jdt.core.compiler.release=enabled
|
||||
org.eclipse.jdt.core.compiler.source=12
|
||||
@@ -1,17 +0,0 @@
|
||||
# javaChat
|
||||
|
||||
Simon O'Shea and Jacob Gohring's Network Programming (COMP-2100-05) Final Project
|
||||
|
||||
javaChat is a barebones group chat application reminiscent of an IRC.
|
||||
|
||||
To host a chat on your own, the only preresiquites are that you have JDK 13 or above, have your router's port 6789 forwarded, and make a firewall rule for port 6789 to permit inbound and outbound access (unfortunately this was the only way I could get the data to transfer cross-network, am looking for another solution and would appreciate feedback).
|
||||
|
||||
********************************
|
||||
|
||||
To use:
|
||||
|
||||
Step 1: To host a server, open "java chat server.jar" (it will open and run silently in the background, check task manager under "background processes" for an "OpenJDK Platform Binary" process and kill it to end the server).
|
||||
|
||||
Step 2: To join the room, open the client "java chat.jar" and enter the IP address of the server. If you're running the server on the same machine, "localhost" will also work. Otherwise you just enter the public IP address of the person's computer that is hosting the server (or the LAN address if you're connected to the same router).
|
||||
|
||||
Step 3: Enter a username, hit enter, and chat away!
|
||||
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,94 +0,0 @@
|
||||
package backend;
|
||||
|
||||
import java.net.* ;
|
||||
|
||||
import javax.swing.JFrame ;
|
||||
|
||||
import java.io.* ;
|
||||
|
||||
public class ChatClientCMD
|
||||
{
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @param args
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void main( String[] args ) throws IOException
|
||||
{
|
||||
|
||||
// Get server IP
|
||||
BufferedReader keyboard = new BufferedReader( new InputStreamReader( System.in ) ) ;
|
||||
System.out.print( "\nPlease enter the IP of the desired server: " ); // GUI: IP BOX
|
||||
|
||||
String serverIP = keyboard.readLine() ;
|
||||
|
||||
// Open your connection to a server, at port 6789
|
||||
Socket socket = new Socket( serverIP , 6789 ) ;
|
||||
|
||||
// Create communication streams
|
||||
DataInputStream dataStreamIn = new DataInputStream( socket.getInputStream() ) ;
|
||||
DataOutputStream dataStreamOut = new DataOutputStream( socket.getOutputStream() ) ;
|
||||
|
||||
// Establish connection, make user-name
|
||||
System.out.print( "\nConnection established,\nPlease enter a username: " ) ;
|
||||
String username = keyboard.readLine() ;
|
||||
dataStreamOut.writeUTF( username ) ;
|
||||
|
||||
/////////////////////////
|
||||
//// Program Running ////
|
||||
/////////////////////////
|
||||
|
||||
// Thread for sending messages
|
||||
Thread sendMessage = new Thread(new Runnable()
|
||||
{
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
System.out.print( "\n-= Type /help for a list of commands =-" ) ;
|
||||
while( true )
|
||||
{
|
||||
try
|
||||
{
|
||||
String messageOut = keyboard.readLine() ;
|
||||
dataStreamOut.writeUTF( messageOut ) ;
|
||||
}
|
||||
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() ;
|
||||
System.out.printf( "%n%s\n", messageIn ) ;
|
||||
}
|
||||
catch( IOException x )
|
||||
{
|
||||
x.printStackTrace() ;
|
||||
System.exit( 0 ) ;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Begin threads
|
||||
sendMessage.start() ;
|
||||
receiveMessage.start() ;
|
||||
}
|
||||
}
|
||||
// end class ChatClient
|
||||
Binary file not shown.
@@ -1,309 +0,0 @@
|
||||
|
||||
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.
@@ -1,167 +0,0 @@
|
||||
|
||||
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,109 +0,0 @@
|
||||
package chatApp;
|
||||
|
||||
import java.net.* ;
|
||||
import java.io.* ;
|
||||
|
||||
public class ClientGUI
|
||||
{
|
||||
|
||||
static LoginGUI login = new LoginGUI() ;
|
||||
static ChatGUI chatRoom = new ChatGUI() ;
|
||||
|
||||
@SuppressWarnings( "javadoc" )
|
||||
public static void main( String[] args ) throws IOException
|
||||
{
|
||||
|
||||
login.setVisible( true );
|
||||
|
||||
boolean done = false ;
|
||||
while( !done )
|
||||
{
|
||||
done = login.isDone() ;
|
||||
System.out.print("");
|
||||
}
|
||||
|
||||
String ip = login.getIP() ;
|
||||
String username = login.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.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,182 +0,0 @@
|
||||
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.
@@ -1,166 +0,0 @@
|
||||
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.
@@ -1,110 +0,0 @@
|
||||
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.
@@ -1,158 +0,0 @@
|
||||
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,11 +0,0 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
/**
|
||||
* @author osheas1
|
||||
*
|
||||
*/
|
||||
module javaChat
|
||||
{
|
||||
requires java.desktop;
|
||||
requires java.logging;}
|
||||
Reference in New Issue
Block a user