Commit f430da74 authored by 9731044's avatar 9731044

initial commit

parents
Pipeline #693 failed with stages
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class ChatArea extends JTextArea {
private static final int ROWS = 10, COLUMNS = 30;
private String name;
private String text;
public ChatArea() {
super(ROWS, COLUMNS);
this.setEditable(false);
this.setLineWrap(true);
this.setVisible(true);
}
public void addMessage(String name, String text) {
setText(getText()+ "\n"+ name + ":"+text);
}
}
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class ChatRoomGUI extends JFrame{
private final String WINDOWS_TITLE = "AUT Chat Room";
private final int WIDTH = 500, HEIGHT = 500;
private final int X = 100, Y = 100;
public ChatArea chatBox;
private ParticipantArea participant;
private MessageArea msg;
public Network network;
public ChatRoomGUI() {
super();
this.setTitle(WINDOWS_TITLE);
this.setLayout(new BorderLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(WIDTH, HEIGHT);
this.setLocation(X, Y);
this.setVisible(true);
chatBox = new ChatArea();
msg = new MessageArea(this);
this.add(new JScrollPane(chatBox), BorderLayout.CENTER);
this.add(new JScrollPane(msg), BorderLayout.PAGE_END);
participant = new ParticipantArea();
this.add(new JScrollPane(participant), BorderLayout.WEST);
network = new Network(this);
}
public void addNewMessage(String name , String text) {
chatBox.addMessage(name,text);
}
public void addNewParticipant(String name) {
participant.addOnlinePerson(name);
}
public void removeParticipant(String name) {
participant.removeOnlinePerson(name);
}
}
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class ClientHandler implements Runnable {
private Socket socket;
private Server server;
private PrintWriter dataOutputStream;
private Scanner dataInputStream;
public ClientHandler(Socket client, Server server) throws Exception {
if (client == null) {
System.out.println("client can't be null");
}
this.socket = client;
this.server = server;
dataInputStream = new Scanner(client.getInputStream());
dataOutputStream = new PrintWriter(client.getOutputStream(),true);
}
public void handleIncomingMessages(String message) {
for(ClientHandler clientHandler : server.clients)
clientHandler.dataOutputStream.println(message);
}
@Override
public void run() {
while(true) {
try {
String input = dataInputStream.nextLine();
if(input == "exit") {
break;
}
handleIncomingMessages(input);
}
catch(Exception e) {
e.printStackTrace();
}
}
}
}
public class Main {
public static void main(String[] args) {
//ChatRoomGUI chatRoom = new ChatRoomGUI();
UsernameFrame username = new UsernameFrame();
/*chatRoom.addNewMessage("User 0", "Hello");
for(dint i=0 ;i<10;i++) {
chatRoom.addNewParticipant("User "+ i);
}
chatRoom.removeParticipant("User 1");*/
}
}
import java.awt.BorderLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class MessageArea extends JPanel {
JTextField t;
JButton b;
public final static String TEXT = "send message:";
ChatRoomGUI chat;
public MessageArea(ChatRoomGUI chat) {
this.chat = chat;
this.setLayout(new BorderLayout());
t = new JTextField();
this.add(t,BorderLayout.CENTER);
b = new JButton(TEXT);
add(b, BorderLayout.EAST);
this.setVisible(true);
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
chat.network.send(t.getText());
}
});
}
public String getText() {
return t.getText();
}
}
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class Network {
private ChatRoomGUI myChat;
private PrintWriter dataOutputStream;
private Scanner dataInputStream;
private Socket socket;
public Network(ChatRoomGUI myChat) {
this.myChat = myChat;
try {
socket = new Socket("localhost",1234);
dataInputStream = new Scanner(socket.getInputStream());
dataOutputStream = new PrintWriter(socket.getOutputStream(),true);
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
public void send(String s) {
try {
dataOutputStream.println(s);
}
catch(Exception e){
e.printStackTrace();
}
}
public void run() {
while(true) {
try {
String input = dataInputStream.nextLine();
myChat.chatBox.addMessage("Server", input);
}
catch(Exception e) {
e.printStackTrace();
}
}
}
}
import java.awt.BorderLayout;
import java.awt.Label;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JPanel;
public class ParticipantArea extends JPanel{
DefaultListModel<String> onlinelist;
public final static String TEXT = "online people:";
public ParticipantArea() {
super();
this.setLayout(new BorderLayout());
Label l1 = new Label(TEXT);
this.add(l1,BorderLayout.PAGE_START );
onlinelist = new DefaultListModel<>();
JList <String> list = new JList<>(onlinelist);
list.setSize(50,50);
this.add(list, BorderLayout.WEST);
this.setVisible(true);
}
public void addOnlinePerson(String username) {
onlinelist.addElement(username);
}
public void removeOnlinePerson(String username) {
onlinelist.removeElement(username);
}
}
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
public class Server implements Runnable {
private static final int PORT = 1234;
private ServerSocket serverSocket;
DataOutputStream dos;
DataInputStream dis;
ArrayList<ClientHandler> clients;
public Server() throws IOException {
serverSocket = new ServerSocket(PORT);
}
@Override
public void run() {
while(true) {
try {
Socket client = serverSocket.accept();
ClientHandler cur = new ClientHandler(client , this);
clients.add(cur);
Thread thread = new Thread(cur);
thread.start();
} catch (Exception e) {
System.out.println("error!");
}
}
}
public static void main(String args[]) {
try {
Server server = new Server();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.awt.BorderLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class UsernameFrame extends JFrame {
private static final String BTN_TXT = " Start Chatting ...";
private static final String LABEL_TEXT = " Choose Your UserName ";
private static final int WIDTH = 300, HEIGHT = 300;
JTextField textField;
JButton btn;
ChatRoomGUI c;
public UsernameFrame() throws HeadlessException {
super();
this.setLayout(new BorderLayout());
JLabel label = new JLabel(LABEL_TEXT);
add(label, BorderLayout.PAGE_START);
textField = new JTextField();
add(textField, BorderLayout.CENTER);
btn = new JButton(BTN_TXT);
add(btn, BorderLayout.PAGE_END);
setSize(WIDTH, HEIGHT);
setVisible(true);
btn.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
String name = textField.getText();
// System.out.println(name);
// System.out.println("");
c = new ChatRoomGUI();
c.addNewParticipant(name);
}
});
}
public String getName() {
return textField.getText();
}
}
\ No newline at end of file
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