Commit ac02d338 authored by Amirhosein Rajabpour's avatar Amirhosein Rajabpour

first commit

parent 66e2ce40
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" addBOMForNewFiles="with NO BOM" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="11" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/project-lab11.iml" filepath="$PROJECT_DIR$/project-lab11.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
package chatRoom.client;
import chatRoom.client.view.ChatRoomGUI;
import chatRoom.client.view.UsernameFrame;
public class Main {
public static void main(String[] args){
// ChatRoomGUI chat1 = new ChatRoomGUI();
UsernameFrame chat2 = new UsernameFrame();
}
}
package chatRoom.client;
import javax.sound.sampled.Port;
import java.net.*;
import java.io.*;
import java.util.Formatter;
import java.util.Scanner;
public class Network {
public static void main(String [] args) {
String serverName = args[0];
int port = Integer.parseInt(args[1]);
try(Socket client = new Socket(serverName, port);
Scanner socketin = new Scanner(client.getInputStream());
Formatter socketOut = new Formatter(client.getOutputStream());)
{
String text;
socketOut.format(text);
socketOut.flush();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package chatRoom.client.model;
import chatRoom.client.view.NewMessageListener;
package chatRoom.client.view;
import javax.swing.*;
import java.awt.*;
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;
private chatArea chatBox;
private MessageArea messageArea;
private ParticipantsArea participantsArea;
private String userName;
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);
chatBox = new chatArea();
this.add(new JScrollPane(chatBox), BorderLayout.CENTER);
messageArea = new MessageArea(this);
this.add(messageArea,BorderLayout.SOUTH);
participantsArea = new ParticipantsArea();
this.add(new JScrollPane(participantsArea),BorderLayout.WEST);
this.setVisible(true);
}
public void addNewMessage(String pm) {
chatBox.addMessage(userName, pm);
}
public void resetMessageText() {
messageArea.reset();
}
public void addNewParticipant(String userName){
participantsArea.addNewParticipant(userName);
this.userName = userName;
}
}
\ No newline at end of file
package chatRoom.client.view;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MessageArea extends JPanel {
JTextField textField;
JButton btn;
ChatRoomGUI chatRoomGUI;
public MessageArea(ChatRoomGUI chatRoomGUI){
super();
this.chatRoomGUI = chatRoomGUI;
this.setLayout(new BorderLayout());
textField = new JTextField();
textField.setEditable(true);
add(textField, BorderLayout.CENTER);
btn = new JButton("send");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
doClickAction();
} });
add(btn, BorderLayout.EAST);
this.setVisible(true);
}
void reset() {
textField.setText("");
}
void doClickAction(){
chatRoomGUI.addNewMessage(textField.getText());
reset();
}
}
package chatRoom.client.view;
public interface NewMessageListener {
}
package chatRoom.client.view;
import javax.swing.*;
import java.awt.*;
import javax.swing.JList;
public class ParticipantsArea extends JPanel {
DefaultListModel model;
JList userslist;
public ParticipantsArea(){
super();
JLabel label = new JLabel("online users");
add(label, BorderLayout.PAGE_START);
this.setLayout(new GridLayout(10,1));
model = new DefaultListModel();
userslist = new JList(model);
this.add(userslist,BorderLayout.CENTER);
}
public void addNewParticipant(String userName){
if(!model.contains(userName))
model.addElement(userName);
// System.out.println(model);
}
public void removeParticipant(String userName){
if(model.contains(userName))
model.removeElement(userName);
}
}
package chatRoom.client.view;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class UsernameFrame extends JFrame {
private static final String BTN_TXT = " Start Chatting ...";
private static final String LABEL_TXT = " Choose Your UserName ";
private static final int WIDTH = 300, HEIGHT = 100;
private JTextField textField;
private JButton btn;
public UsernameFrame() throws HeadlessException {
super();
this.setLayout(new BorderLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel(LABEL_TXT);
add(label, BorderLayout.PAGE_START);
textField = new JTextField();
add(textField, BorderLayout.CENTER);
btn = new JButton(BTN_TXT);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
doClickAction();
} });
add(btn, BorderLayout.PAGE_END);
setSize(WIDTH, HEIGHT);
setVisible(true);
}
public void doClickAction(){
ChatRoomGUI chat1 = new ChatRoomGUI();
chat1.addNewParticipant(textField.getText());
this.setVisible(false);
}
}
\ No newline at end of file
package chatRoom.client.view;
import javax.swing.*;
public class chatArea extends JTextArea {
private static final int ROWS = 10, COLUMNS = 30;
public chatArea(){
super(ROWS,COLUMNS);
this.setEditable(false);
this.setLineWrap(true);
setVisible(true);
}
public void addMessage(String name, String pm){
String str = name+ ":" + pm + "\n";
append(str);
}
}
package chatRoom.server;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
public class ClientHandler {
private Socket socket;
private DataOutputStream dataOutputStream;
private DataInputStream dataInputStream;
public ClientHandler(Socket client) throws Exception {
if (client == null)
throw new Exception("client can't be null");
this.socket = client;
dataOutputStream = new DataOutputStream(client.getOutputStream());
dataInputStream = new DataInputStream(client.getInputStream());
run();
}
private void handleIncomingMessages(String message){
System.out.println(message);
}
public void run() {
while(true){
try {
String input = dataInputStream.readUTF();
handleIncomingMessages(input);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
\ No newline at end of file
package chatRoom.server;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
public class Server {
private static final int PORT = 4321;
private ServerSocket serverSocket;
ArrayList<Socket> users = new ArrayList<>();
public Server() throws IOException {
serverSocket = new ServerSocket(PORT);
while (true) {
Socket client = serverSocket.accept();
try {
ClientHandler clientHandler = new ClientHandler(client);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
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