Commit 11abca71 authored by Roham's avatar Roham

lab11

parent 6e4d319e
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
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);
} }
} }
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);
} }
} }
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
public interface NewMessageListener {
void addNewMessageListener(String name, String message);
void addNewParticipantListener(String name);
}
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;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment