Datasets:
ArXiv:
License:
File size: 3,205 Bytes
c574d3a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
//Csaba
package battleship.gui;
import battleship.Events.ChatGUIEvent;
import battleship.Resources.Resources;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.ScrollPaneConstants;
/**
*
* @author nycs0
*/
public class ChatGUI extends JPanel {
private List<ChatGUIEvent> listeners;
JTextPane chatTextBox;
JTextField sendTextBox;
int prefferedHeight;
int prefferedWidth;
List<String> chat;
public ChatGUI() {
prefferedHeight = 15;
chat = new ArrayList<>();
listeners = new ArrayList<>();
setLayout(null);
chatTextBox = new JTextPane();
chatTextBox.setLayout(new FlowLayout(FlowLayout.CENTER));
chatTextBox.setBackground(Resources.BackgroundColor);
chatTextBox.setForeground(Color.WHITE);
chatTextBox.setEditable(false);
//chatTextBox.setAutoscrolls(true);
chatTextBox.setSize(700, 150);
chatTextBox.setPreferredSize(new Dimension(700, prefferedHeight));
chatTextBox.setLocation(0, 0);
this.add(chatTextBox);
JScrollPane scroll = new JScrollPane(chatTextBox);
scroll.setBounds(0, 0, chatTextBox.size().width, chatTextBox.size().height);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
this.add(scroll);
sendTextBox = new JTextField();
sendTextBox.setLayout(new FlowLayout(FlowLayout.LEADING));
sendTextBox.setSize(700, 35);
sendTextBox.setLocation(0, 150);
sendTextBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
if (sendTextBox.getText().equals("")) {
return;
}
for (ChatGUIEvent listener : listeners) {
listener.onSendMessage(sendTextBox.getText());
}
sendTextBox.setText("");
}
});
this.add(sendTextBox);
}
public void addMessage(String sender, String message) {
chat.add("[" + LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm")) + "] [" + sender + "] : " + message + "\n");
if (chat.size() > 30) {
chat.remove(0);
}
String chatMessage = "";
for (String string : chat) {
chatMessage += string;
}
chatTextBox.setText(chatMessage);
//chatTextBox.append("[" + LocalTime.now() + "] [" + sender + "] : " + message + "\n");
prefferedHeight += prefferedHeight;
chatTextBox.setPreferredSize(new Dimension(700, prefferedHeight));
chatTextBox.setCaretPosition(chatTextBox.getDocument().getLength());
}
public void addSendMessageListener(ChatGUIEvent listener) {
listeners.add(listener);
}
}
|