Commit df7b8539 authored by 9731050's avatar 9731050

frist

parent fdb8a22a
Pipeline #682 failed with stages
<?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$/lab11.iml" filepath="$PROJECT_DIR$/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
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.setVisible(true);
}
public void addMessage(String username, String message)
{
this.setText(this.getText() + username + ":\n" + message + "\n");
}
}
import javax.swing.*;
import java.awt.*;
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();
this.setLayout(new BorderLayout());
this.setTitle(WINDOWS_TITLE);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(WIDTH, HEIGHT);
this.setLocation(X, Y);
chatArea = new ChatArea();
this.add(new JScrollPane(chatArea), BorderLayout.CENTER);
messageArea = new MessageArea(this);
this.add(messageArea, BorderLayout.PAGE_END);
participantsArea = new ParticipantsArea();
this.add(new JScrollPane(participantsArea), BorderLayout.WEST);
this.setVisible(true);
}
public void addNewMessage(String name, String message)
{
this.chatArea.addMessage(name, message);
}
public void addNewParticipant(String name)
{
this.participantsArea.addNewParticipant(name);
}
public void removeNewParticipant(String 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);}
}
public class Main {
public static void main(String[] args) {
ChatRoomGUI chatRoomGUI = new ChatRoomGUI();
NameFrame nameFrame = new NameFrame(chatRoomGUI);
chatRoomGUI.addNewParticipant(nameFrame.username);
chatRoomGUI.removeNewParticipant(nameFrame.username);
}
}
import network.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(ChatRoomGUI chatRoomGUI) throws HeadlessException {
super();
this.setLayout(new BorderLayout());
textField = new JTextField();
textField.setEditable(true);
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());
} 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 ...";
private static final String LABEL_TXT = " Choose Your UserName ";
private static final int WIDTH = 300, HEIGHT = 100;
JTextField textField;
JButton btn;
String username;
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(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();
}
}
public interface NewMessageListener {
void addNewMessageListener(String name, String message);
void addNewParticipantListener(String name);
}
import javax.swing.*;
import java.awt.*;
public class ParticipantsArea extends JPanel {
private final int ROW = 20, COLUMN = 1;
DefaultListModel<String> model;
JList<String> list;
public ParticipantsArea() throws HeadlessException {
super();
GridLayout g = new GridLayout(20, 1);
this.setLayout(g);
JLabel label = new JLabel("Online Users");
this.add(label);
this.model= new DefaultListModel();
this.list = new JList(model);
this.add(list);
this.setVisible(true);
}
public void addNewParticipant(String name)
{
if (!this.model.contains(name)) {
this.model.addElement(name);
}
}
public void removeParticipant(String name)
{
if (this.model.contains(name)) {
this.model.removeElement(name);
}
}
}
package network;
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