Commit 11abca71 authored by Roham's avatar Roham

lab11

parent 6e4d319e
import javax.swing.*;
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 int WIDTH = 500, HEIGHT = 500;
private final int X = 100, Y = 100;
ChatArea chatArea;
MessageArea messageArea;
ParticipantsArea participantsArea;
String username;
public ChatRoomGUI() {
super();
......@@ -18,7 +19,7 @@ public class ChatRoomGUI extends JFrame {
this.setLocation(X, Y);
chatArea = new ChatArea();
this.add(new JScrollPane(chatArea), BorderLayout.CENTER);
messageArea = new MessageArea();
messageArea = new MessageArea(this);
this.add(messageArea, BorderLayout.PAGE_END);
participantsArea = new ParticipantsArea();
this.add(new JScrollPane(participantsArea), BorderLayout.WEST);
......@@ -39,4 +40,11 @@ public class ChatRoomGUI extends JFrame {
{
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 static void main(String[] args) {
ChatRoomGUI chatRoomGUI = new ChatRoomGUI();
NameFrame nameFrame = new NameFrame();
NameFrame nameFrame = new NameFrame(chatRoomGUI);
chatRoomGUI.addNewParticipant("ROHAM");
chatRoomGUI.addNewMessage("ROHAM", "SALAM");
chatRoomGUI.addNewParticipant("AMIRHOSSEIN");
chatRoomGUI.addNewMessage("AMIRHOSSEIN", "SALAM");
chatRoomGUI.addNewParticipant("GHOLI");
chatRoomGUI.removeNewParticipant("GHOLI");
chatRoomGUI.addNewParticipant(nameFrame.username);
chatRoomGUI.removeNewParticipant(nameFrame.username);
}
}
import networking.Network;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
public class MessageArea extends JPanel{
JTextField textField;
JButton btn;
public MessageArea() throws HeadlessException {
public MessageArea(ChatRoomGUI chatRoomGUI) throws HeadlessException {
super();
this.setLayout(new BorderLayout());
textField = new JTextField();
......@@ -13,6 +18,18 @@ public class MessageArea extends JPanel{
add(textField, BorderLayout.CENTER);
btn = new JButton("SEND");
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);
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class NameFrame extends JFrame {
private static final String BTN_TXT = " Start Chatting ...";
......@@ -7,17 +10,32 @@ public class NameFrame extends JFrame {
private static final int WIDTH = 300, HEIGHT = 100;
JTextField textField;
JButton btn;
String username;
public NameFrame() throws HeadlessException {
public NameFrame(ChatRoomGUI chatRoomGUI) throws HeadlessException {
super();
this.setLayout(new BorderLayout());
JLabel label = new JLabel("Choose Your UserName");
add(label, BorderLayout.PAGE_START);
textField = new JTextField();
add(textField, BorderLayout.CENTER);
btn = new JButton(LABEL_TXT);
btn = new JButton(BTN_TXT);
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);
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