Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
L
lab10
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
9731088
lab10
Commits
11abca71
Commit
11abca71
authored
May 26, 2019
by
Roham
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
lab11
parent
6e4d319e
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
88 additions
and
6 deletions
+88
-6
ChatRoomGUI.java
src/ChatRoomGUI.java
+10
-2
Main.java
src/Main.java
+3
-1
MessageArea.java
src/MessageArea.java
+18
-1
NameFrame.java
src/NameFrame.java
+20
-2
NewMessageListener.java
src/NewMessageListener.java
+4
-0
Network.java
src/networking/Network.java
+33
-0
No files found.
src/ChatRoomGUI.java
View file @
11abca71
import
javax.swing.*
;
import
javax.swing.*
;
import
java.awt.*
;
import
java.awt.*
;
public
class
ChatRoomGUI
extends
JFrame
{
public
class
ChatRoomGUI
extends
JFrame
implements
NewMessageListener
{
private
final
String
WINDOWS_TITLE
=
"AUT Chat Room"
;
private
final
String
WINDOWS_TITLE
=
"AUT Chat Room"
;
private
final
int
WIDTH
=
500
,
HEIGHT
=
500
;
private
final
int
WIDTH
=
500
,
HEIGHT
=
500
;
private
final
int
X
=
100
,
Y
=
100
;
private
final
int
X
=
100
,
Y
=
100
;
ChatArea
chatArea
;
ChatArea
chatArea
;
MessageArea
messageArea
;
MessageArea
messageArea
;
ParticipantsArea
participantsArea
;
ParticipantsArea
participantsArea
;
String
username
;
public
ChatRoomGUI
()
{
public
ChatRoomGUI
()
{
super
();
super
();
...
@@ -18,7 +19,7 @@ public class ChatRoomGUI extends JFrame {
...
@@ -18,7 +19,7 @@ public class ChatRoomGUI extends JFrame {
this
.
setLocation
(
X
,
Y
);
this
.
setLocation
(
X
,
Y
);
chatArea
=
new
ChatArea
();
chatArea
=
new
ChatArea
();
this
.
add
(
new
JScrollPane
(
chatArea
),
BorderLayout
.
CENTER
);
this
.
add
(
new
JScrollPane
(
chatArea
),
BorderLayout
.
CENTER
);
messageArea
=
new
MessageArea
();
messageArea
=
new
MessageArea
(
this
);
this
.
add
(
messageArea
,
BorderLayout
.
PAGE_END
);
this
.
add
(
messageArea
,
BorderLayout
.
PAGE_END
);
participantsArea
=
new
ParticipantsArea
();
participantsArea
=
new
ParticipantsArea
();
this
.
add
(
new
JScrollPane
(
participantsArea
),
BorderLayout
.
WEST
);
this
.
add
(
new
JScrollPane
(
participantsArea
),
BorderLayout
.
WEST
);
...
@@ -39,4 +40,11 @@ public class ChatRoomGUI extends JFrame {
...
@@ -39,4 +40,11 @@ public class ChatRoomGUI extends JFrame {
{
{
this
.
participantsArea
.
removeParticipant
(
name
);
this
.
participantsArea
.
removeParticipant
(
name
);
}
}
public
void
addNewMessageListener
(
String
name
,
String
message
)
{
this
.
chatArea
.
addMessage
(
name
,
message
);
}
public
void
addNewParticipantListener
(
String
name
)
{
this
.
participantsArea
.
addNewParticipant
(
name
);}
}
}
\ No newline at end of file
src/Main.java
View file @
11abca71
public
class
Main
{
public
class
Main
{
public
static
void
main
(
String
[]
args
)
{
public
static
void
main
(
String
[]
args
)
{
ChatRoomGUI
chatRoomGUI
=
new
ChatRoomGUI
();
ChatRoomGUI
chatRoomGUI
=
new
ChatRoomGUI
();
NameFrame
nameFrame
=
new
NameFrame
();
NameFrame
nameFrame
=
new
NameFrame
(
chatRoomGUI
);
chatRoomGUI
.
addNewParticipant
(
"ROHAM"
);
chatRoomGUI
.
addNewParticipant
(
"ROHAM"
);
chatRoomGUI
.
addNewMessage
(
"ROHAM"
,
"SALAM"
);
chatRoomGUI
.
addNewMessage
(
"ROHAM"
,
"SALAM"
);
chatRoomGUI
.
addNewParticipant
(
"AMIRHOSSEIN"
);
chatRoomGUI
.
addNewParticipant
(
"AMIRHOSSEIN"
);
chatRoomGUI
.
addNewMessage
(
"AMIRHOSSEIN"
,
"SALAM"
);
chatRoomGUI
.
addNewMessage
(
"AMIRHOSSEIN"
,
"SALAM"
);
chatRoomGUI
.
addNewParticipant
(
"GHOLI"
);
chatRoomGUI
.
addNewParticipant
(
"GHOLI"
);
chatRoomGUI
.
removeNewParticipant
(
"GHOLI"
);
chatRoomGUI
.
removeNewParticipant
(
"GHOLI"
);
chatRoomGUI
.
addNewParticipant
(
nameFrame
.
username
);
chatRoomGUI
.
removeNewParticipant
(
nameFrame
.
username
);
}
}
}
}
src/MessageArea.java
View file @
11abca71
import
networking.Network
;
import
javax.swing.*
;
import
javax.swing.*
;
import
java.awt.*
;
import
java.awt.*
;
import
java.awt.event.ActionEvent
;
import
java.awt.event.ActionListener
;
import
java.io.IOException
;
public
class
MessageArea
extends
JPanel
{
public
class
MessageArea
extends
JPanel
{
JTextField
textField
;
JTextField
textField
;
JButton
btn
;
JButton
btn
;
public
MessageArea
()
throws
HeadlessException
{
public
MessageArea
(
ChatRoomGUI
chatRoomGUI
)
throws
HeadlessException
{
super
();
super
();
this
.
setLayout
(
new
BorderLayout
());
this
.
setLayout
(
new
BorderLayout
());
textField
=
new
JTextField
();
textField
=
new
JTextField
();
...
@@ -13,6 +18,18 @@ public class MessageArea extends JPanel{
...
@@ -13,6 +18,18 @@ public class MessageArea extends JPanel{
add
(
textField
,
BorderLayout
.
CENTER
);
add
(
textField
,
BorderLayout
.
CENTER
);
btn
=
new
JButton
(
"SEND"
);
btn
=
new
JButton
(
"SEND"
);
add
(
btn
,
BorderLayout
.
EAST
);
add
(
btn
,
BorderLayout
.
EAST
);
btn
.
addActionListener
(
new
ActionListener
()
{
@Override
public
void
actionPerformed
(
ActionEvent
e
)
{
chatRoomGUI
.
addNewMessage
(
chatRoomGUI
.
username
,
textField
.
getText
());
try
{
Network
n
=
new
Network
(
"localhost"
,
1234
,
textField
.
getText
());
//n.Send(textField.getText());
}
catch
(
IOException
e1
)
{
e1
.
printStackTrace
();
}
}
});
setVisible
(
true
);
setVisible
(
true
);
}
}
}
}
src/NameFrame.java
View file @
11abca71
import
javax.swing.*
;
import
javax.swing.*
;
import
java.awt.*
;
import
java.awt.*
;
import
java.awt.event.ActionEvent
;
import
java.awt.event.ActionListener
;
public
class
NameFrame
extends
JFrame
{
public
class
NameFrame
extends
JFrame
{
private
static
final
String
BTN_TXT
=
" Start Chatting ..."
;
private
static
final
String
BTN_TXT
=
" Start Chatting ..."
;
...
@@ -7,17 +10,32 @@ public class NameFrame extends JFrame {
...
@@ -7,17 +10,32 @@ public class NameFrame extends JFrame {
private
static
final
int
WIDTH
=
300
,
HEIGHT
=
100
;
private
static
final
int
WIDTH
=
300
,
HEIGHT
=
100
;
JTextField
textField
;
JTextField
textField
;
JButton
btn
;
JButton
btn
;
String
username
;
public
NameFrame
()
throws
HeadlessException
{
public
NameFrame
(
ChatRoomGUI
chatRoomGUI
)
throws
HeadlessException
{
super
();
super
();
this
.
setLayout
(
new
BorderLayout
());
this
.
setLayout
(
new
BorderLayout
());
JLabel
label
=
new
JLabel
(
"Choose Your UserName"
);
JLabel
label
=
new
JLabel
(
"Choose Your UserName"
);
add
(
label
,
BorderLayout
.
PAGE_START
);
add
(
label
,
BorderLayout
.
PAGE_START
);
textField
=
new
JTextField
();
textField
=
new
JTextField
();
add
(
textField
,
BorderLayout
.
CENTER
);
add
(
textField
,
BorderLayout
.
CENTER
);
btn
=
new
JButton
(
LABEL
_TXT
);
btn
=
new
JButton
(
BTN
_TXT
);
add
(
btn
,
BorderLayout
.
PAGE_END
);
add
(
btn
,
BorderLayout
.
PAGE_END
);
btn
.
addActionListener
(
new
ActionListener
()
{
@Override
public
void
actionPerformed
(
ActionEvent
e
)
{
doClickAction
();
chatRoomGUI
.
addNewParticipant
(
username
);
chatRoomGUI
.
username
=
username
;
}
});
setSize
(
WIDTH
,
HEIGHT
);
setSize
(
WIDTH
,
HEIGHT
);
setVisible
(
true
);
setVisible
(
true
);
}
}
public
void
doClickAction
()
{
setVisible
(
false
);
username
=
textField
.
getText
();
}
}
}
\ No newline at end of file
src/NewMessageListener.java
0 → 100644
View file @
11abca71
public
interface
NewMessageListener
{
void
addNewMessageListener
(
String
name
,
String
message
);
void
addNewParticipantListener
(
String
name
);
}
src/networking/Network.java
0 → 100644
View file @
11abca71
package
networking
;
import
java.net.*
;
import
java.io.*
;
public
class
Network
{
private
String
serverName
;
private
int
port
;
Socket
client
;
public
Network
(
String
serverName
,
int
port
,
String
message
)
throws
IOException
{
try
{
System
.
out
.
println
(
"Connecting to "
+
serverName
+
" on port "
+
port
);
client
=
new
Socket
(
serverName
,
port
);
System
.
out
.
println
(
"Just connected to "
+
client
.
getRemoteSocketAddress
());
OutputStream
outToServer
=
client
.
getOutputStream
();
DataOutputStream
out
=
new
DataOutputStream
(
outToServer
);
out
.
writeUTF
(
message
);
InputStream
inFromServer
=
client
.
getInputStream
();
DataInputStream
in
=
new
DataInputStream
(
inFromServer
);
System
.
out
.
println
(
"Server says "
+
in
.
readUTF
());
client
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
this
.
serverName
=
serverName
;
this
.
port
=
port
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment