-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatBotGui.java
More file actions
204 lines (170 loc) · 6.83 KB
/
ChatBotGui.java
File metadata and controls
204 lines (170 loc) · 6.83 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ChatBotGui extends JFrame {
private JTextPane chatPane;
private JTextField chatField;
private JButton sendButton;
private simpleNLP bot;
private StyledDocument doc;
private Style userStyle, botStyle;
private SimpleDateFormat timeFormat;
public ChatBotGui() {
bot = new simpleNLP();
timeFormat = new SimpleDateFormat("HH:mm:ss");
setTitle("Java AI Chatbot");
setSize(500, 600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
chatPane = new JTextPane();
chatPane.setEditable(false);
chatPane.setFont(new Font("Arial", Font.PLAIN, 16));
doc = chatPane.getStyledDocument();
userStyle = chatPane.addStyle("UserStyle", null);
StyleConstants.setAlignment(userStyle, StyleConstants.ALIGN_RIGHT);
StyleConstants.setForeground(userStyle, Color.BLUE);
botStyle = chatPane.addStyle("BotStyle", null);
StyleConstants.setAlignment(botStyle, StyleConstants.ALIGN_LEFT);
StyleConstants.setForeground(botStyle, Color.DARK_GRAY);
JScrollPane scrollPane = new JScrollPane(chatPane);
JPanel inputPanel = new JPanel(new BorderLayout());
chatField = new JTextField() {
@Override
protected void paintComponent(Graphics g) {
if (!isOpaque() && getBorder() instanceof RoundedBorder) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setColor(getBackground());
g2.fillRoundRect(0, 0, getWidth() - 1, getHeight() - 1, 20, 20);
g2.dispose();
}
super.paintComponent(g);
}
@Override
protected void paintBorder(Graphics g) {
if (getBorder() instanceof RoundedBorder) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setColor(Color.GRAY);
g2.drawRoundRect(0, 0, getWidth() - 1, getHeight() - 1, 20, 20);
g2.dispose();
} else {
super.paintBorder(g);
}
}
};
chatField.setOpaque(false);
chatField.setBorder(new RoundedBorder(15));
chatField.setBackground(new Color(245, 245, 245));
chatField.setFont(new Font("Arial", Font.PLAIN, 16));
chatField.setMargin(new Insets(10, 15, 10, 15));
chatField.addFocusListener(new FocusAdapter() {
public void focusGained(FocusEvent e) {
chatField.setBackground(new Color(255, 255, 255));
}
public void focusLost(FocusEvent e) {
chatField.setBackground(new Color(245, 245, 245));
}
});
sendButton = new JButton("Send");
chatField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sendMessage();
}
});
sendButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sendMessage();
}
});
inputPanel.add(chatField, BorderLayout.CENTER);
inputPanel.add(sendButton, BorderLayout.EAST);
add(scrollPane, BorderLayout.CENTER);
add(inputPanel, BorderLayout.SOUTH);
}
private void sendMessage() {
final String input = chatField.getText().trim();
if (!input.isEmpty()) {
String timeStamp = getCurrentTimeStamp();
appendMessage("You (" + timeStamp + "): " + input + "\n", userStyle);
chatField.setText("");
autoScroll();
new Thread(new Runnable() {
public void run() {
try {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
appendMessage("Bot is typing...\n", botStyle);
}
});
Thread.sleep(1000);
final String response = bot.getResponse(input);
final String responseTime = getCurrentTimeStamp();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
removeLastLine();
appendMessage("Bot (" + responseTime + "): " + response + "\n", botStyle);
autoScroll();
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}
private String getCurrentTimeStamp() {
return timeFormat.format(new Date());
}
private void appendMessage(String message, Style style) {
try {
int length = doc.getLength();
doc.insertString(length, message, style);
doc.setParagraphAttributes(length, message.length(), style, false);
} catch (BadLocationException e) {
e.printStackTrace();
}
}
private void removeLastLine() {
try {
int length = doc.getLength();
int start = Utilities.getRowStart(chatPane, length - 1);
doc.remove(start, length - start);
} catch (Exception e) {
e.printStackTrace();
}
}
private void autoScroll() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JScrollBar vertical = ((JScrollPane) chatPane.getParent().getParent()).getVerticalScrollBar();
vertical.setValue(vertical.getMaximum());
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ChatBotGui().setVisible(true);
}
});
}
}
class RoundedBorder implements Border {
private int radius;
RoundedBorder(int radius) {
this.radius = radius;
}
public Insets getBorderInsets(Component c) {
return new Insets(radius + 1, radius + 1, radius + 1, radius + 1);
}
public boolean isBorderOpaque() {
return false;
}
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
g.drawRoundRect(x, y, width - 1, height - 1, radius, radius);
}
}