Browse Source

Merge branch 'server' into 'main'

Server

See merge request fdai7332/java-chat!10
remotes/origin/server
fdai7599 11 months ago
parent
commit
b47cbca014
  1. 144
      src/main/java/ChatGUI.java
  2. 6
      src/main/java/ClientHandler.java
  3. 80
      src/test/java/ChatGUITest.java
  4. 10
      src/test/java/TestActionEvent.java

144
src/main/java/ChatGUI.java

@ -0,0 +1,144 @@
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class ChatGUI implements ActionListener {
// Menu items for font sizes
JMenuItem small = new JMenuItem("small-font");
JMenuItem medium = new JMenuItem("medium-font");
JMenuItem large = new JMenuItem("large-font");
// Menu items for font colors
JMenuItem black = new JMenuItem("black");
JMenuItem red = new JMenuItem("red");
JMenuItem blue = new JMenuItem("blue");
JMenuItem green = new JMenuItem("green");
JMenuItem exit = new JMenuItem("Exit");
JTextField inputTextField = new JTextField();
JTextArea outputTextArea = new JTextArea();
JButton sendButton = new JButton("Senden");
JFrame gui;
public ChatGUI() {
gui = new JFrame();
// Set up the main frame
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setTitle("java-chat");
gui.setLayout(new BorderLayout());
// Set up the menu bar
JMenuBar bar = new JMenuBar();
gui.setJMenuBar(bar);
// Create menu items and menus
JMenu options = new JMenu("options");
JMenu colors = new JMenu("font-colors");
JMenu size = new JMenu("font-size");
JMenu menu = new JMenu("File");
menu.add(exit);
menu.add(options);
bar.add(menu);
options.add(colors);
options.add(size);
colors.add(black);
colors.add(red);
colors.add(blue);
colors.add(green);
size.add(small);
size.add(medium);
size.add(large);
// Register action listeners for menu items
red.addActionListener(this);
blue.addActionListener(this);
black.addActionListener(this);
green.addActionListener(this);
small.addActionListener(this);
medium.addActionListener(this);
large.addActionListener(this);
exit.addActionListener(this);
bar.add(menu);
// Set up the output text area with scrolling
JScrollPane outputScrollPane = new JScrollPane(outputTextArea);
gui.add(outputScrollPane, BorderLayout.CENTER);
// Set up the input panel with text field and send button
JPanel inputPanel = new JPanel(new BorderLayout());
inputPanel.add(inputTextField, BorderLayout.CENTER);
inputPanel.add(sendButton, BorderLayout.EAST);
gui.add(inputPanel, BorderLayout.SOUTH);
inputTextField.addActionListener(this);
sendButton.addActionListener(this);
// Set the size of the GUI to be a quarter of the screen size
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int quarterWidth = screenSize.width / 2;
int quarterHeight = screenSize.height / 2;
gui.setSize(quarterWidth, quarterHeight);
gui.setVisible(true);
}
// main methode zum Testen, müsste bei Implementation entfernt werden
public static void main(String[] args) {
new ChatGUI();
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == exit) {
System.exit(0);
}
if (e.getSource() == inputTextField || e.getSource() == sendButton) {
String inputText = inputTextField.getText();
outputTextArea.append(inputText + "\n");
inputTextField.setText("");
}
if (e.getSource() == red) {
outputTextArea.setForeground(Color.RED);
} else if (e.getSource() == blue) {
outputTextArea.setForeground(Color.BLUE);
} else if (e.getSource() == black) {
outputTextArea.setForeground(Color.BLACK);
} else if (e.getSource() == green) {
outputTextArea.setForeground(Color.GREEN);
}
if (e.getSource() == small) {
outputTextArea.setFont(outputTextArea.getFont().deriveFont(Font.PLAIN, 12));
}
if (e.getSource() == medium) {
outputTextArea.setFont(outputTextArea.getFont().deriveFont(Font.PLAIN, 16));
}
if (e.getSource() == large) {
outputTextArea.setFont(outputTextArea.getFont().deriveFont(Font.PLAIN, 20));
}
}
}

6
src/main/java/ClientHandler.java

@ -9,6 +9,7 @@ public class ClientHandler implements Runnable {
private String name;
private BufferedReader fromClientReader;
private PrintWriter toClientWriter;
private String lastMessage;
// Constructor for ClientHandler
public ClientHandler(ChatServer chatServer, Socket connectionToClient) {
@ -33,6 +34,7 @@ public class ClientHandler implements Runnable {
while (message!=null) {
// Broadcast the client's message to all connected clients
chatServer.broadcastMessage(name + ": " + message);
lastMessage = message;
message = fromClientReader.readLine();
}
}
@ -64,4 +66,8 @@ public class ClientHandler implements Runnable {
toClientWriter.println(message); // Send the message to the client
toClientWriter.flush(); // Flush the stream
}
//Test method
public String getLastMessage() {
return lastMessage;
}
}

80
src/test/java/ChatGUITest.java

@ -0,0 +1,80 @@
import static org.junit.Assert.assertEquals;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import org.junit.Before;
import org.junit.Test;
public class ChatGUITest {
private ChatGUI chatGUI;
@Before
public void setUp() {
chatGUI = new ChatGUI();
}
@Test
public void testSetOutputTextColorRed() {
chatGUI.actionPerformed(new TestActionEvent(chatGUI.red));
assertEquals(Color.RED, chatGUI.outputTextArea.getForeground());
}
@Test
public void testSetOutputTextColorBlue() {
chatGUI.actionPerformed(new TestActionEvent(chatGUI.blue));
assertEquals(Color.BLUE, chatGUI.outputTextArea.getForeground());
}
@Test
public void testSetOutputTextColorBlack() {
chatGUI.actionPerformed(new TestActionEvent(chatGUI.black));
assertEquals(Color.BLACK, chatGUI.outputTextArea.getForeground());
}
@Test
public void testSetOutputTextColorGreen() {
chatGUI.actionPerformed(new TestActionEvent(chatGUI.green));
assertEquals(Color.GREEN, chatGUI.outputTextArea.getForeground());
}
@Test
public void testSetOutputTextFontSizeSmall() {
chatGUI.actionPerformed(new TestActionEvent(chatGUI.small));
Font expectedFont = chatGUI.outputTextArea.getFont().deriveFont(Font.PLAIN, 12);
assertEquals(expectedFont, chatGUI.outputTextArea.getFont());
}
@Test
public void testSetOutputTextFontSizeMedium() {
chatGUI.actionPerformed(new TestActionEvent(chatGUI.medium));
Font expectedFont = chatGUI.outputTextArea.getFont().deriveFont(Font.PLAIN, 16);
assertEquals(expectedFont, chatGUI.outputTextArea.getFont());
}
@Test
public void testSetOutputTextFontSizeLarge() {
chatGUI.actionPerformed(new TestActionEvent(chatGUI.large));
Font expectedFont = chatGUI.outputTextArea.getFont().deriveFont(Font.PLAIN, 20);
assertEquals(expectedFont, chatGUI.outputTextArea.getFont());
}
@Test
public void testSendButtonActionPerformed() {
chatGUI.inputTextField.setText("Testnachricht");
chatGUI.sendButton.doClick();
String expectedOutput = "Testnachricht\n";
assertEquals(expectedOutput, chatGUI.outputTextArea.getText());
}
@Test
public void testInputTextFieldActionPerformed() {
chatGUI.inputTextField.setText("Testnachricht");
chatGUI.actionPerformed(new ActionEvent(chatGUI.inputTextField, ActionEvent.ACTION_PERFORMED, ""));
String expectedOutput = "Testnachricht\n";
assertEquals(expectedOutput, chatGUI.outputTextArea.getText());
assertEquals("", chatGUI.inputTextField.getText());
}
}

10
src/test/java/TestActionEvent.java

@ -0,0 +1,10 @@
import java.awt.event.ActionEvent;
public class TestActionEvent extends ActionEvent {
private static final long serialVersionUID = 1L;
public TestActionEvent(Object source) {
super(source, ActionEvent.ACTION_PERFORMED, "Test command");
}
}
Loading…
Cancel
Save