This repository was archived by the owner on Dec 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCatGPT.java
More file actions
167 lines (151 loc) · 6.33 KB
/
Copy pathCatGPT.java
File metadata and controls
167 lines (151 loc) · 6.33 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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.util.Random;
public class CatGPT extends JFrame implements ActionListener {
private JFrame frame;
private JPanel panel;
private JButton sendButton;
private JTextArea inputTextArea;
private JTextPane chatHistoryTextPane;
public CatGPT() {
frame = new JFrame();
panel = new JPanel(new GridBagLayout());
inputTextArea = new JTextArea(3, 20);
sendButton = new JButton("Send message");
chatHistoryTextPane = new JTextPane();
chatHistoryTextPane.setEditable(false);
sendButton.addActionListener(this);
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(10, 10, 10, 10); // Increased padding for the panel to the frame
// Place the chat history at the top
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 3; // Adjusted to 3 to accommodate the toggle button
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
panel.add(new JScrollPane(chatHistoryTextPane), gbc);
// Place the input text area below the chat history
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 2;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1.0;
gbc.weighty = 0.1;
panel.add(new JScrollPane(inputTextArea), gbc);
// Place the send button next to the input text area
gbc.gridx = 2;
gbc.gridy = 1;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.NONE;
gbc.weightx = 0;
gbc.weighty = 0;
gbc.ipadx = 20; // Increase internal padding for the send button
gbc.ipady = 10; // Increase internal padding for the send button
panel.add(sendButton, gbc);
// Place the toggle button below the input text area and send button
gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridwidth = 3;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 0;
gbc.weighty = 0;
frame.add(panel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("CatGPT (Insider Preview)");
frame.setSize(500, 400);
frame.setVisible(true);
}
public static void main(String[] args) {
final int globalSize = 15;
UIManager.put("Label.font", new Font(Font.DIALOG, Font.PLAIN, globalSize));
UIManager.put("TextPane.font", new Font(Font.DIALOG, Font.PLAIN, globalSize));
new CatGPT();
}
@Override
public void actionPerformed(ActionEvent event) {
sendButton.setEnabled(false);
inputTextArea.setEnabled(false);
String text = inputTextArea.getText();
if (!text.trim().isEmpty()) {
appendToChat("You", text);
inputTextArea.setText("LLM is processing your prompt...");
Timer timer = new Timer(3000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
String[] catMessages = {
"Meow",
"Purr",
"I'm just a cat, I don't know what you're talking about.",
"Meow Meow Meow Meow.",
"Meeeeeeowwww",
"Purrrrrrrr",
"Feed me hooman!",
"I love boxes.",
"Can I sit on your keyboard?",
"I am the ruler of this household.",
"Wake me up when it's dinner time.",
"Play with me!",
"Pet me or suffer the consequences.",
"I'm plotting something mischievous...",
"Chasing my tail again!",
"Bird watching mode activated.",
"Let's nap together.",
"Meowdeling is my passion.",
"Just loafing around.",
};
int randomIndex = new Random().nextInt(catMessages.length);
String randomMessage = catMessages[randomIndex];
appendToChat("CatGPT", randomMessage);
sendButton.setEnabled(true);
inputTextArea.setText("");
inputTextArea.setEnabled(true);
inputTextArea.requestFocusInWindow();
}
});
timer.setRepeats(false);
timer.start();
} else {
sendButton.setEnabled(true);
inputTextArea.setEnabled(true);
}
}
private void appendToChat(String nickname, String text) {
StyledDocument doc = chatHistoryTextPane.getStyledDocument();
SimpleAttributeSet style = new SimpleAttributeSet();
StyleConstants.setBold(style, true);
try {
String emoji = "";
if (nickname.equals("CatGPT")) {
emoji = "\uD83D\uDC31";
} else {
emoji = "\u200D\uD83D\uDCBB";
}
doc.insertString(doc.getLength(), emoji + " " + nickname + "\n", style);
if (nickname.equals("CatGPT")) {
String newText = text;
new Timer(25, new ActionListener() {
int i = 0;
public void actionPerformed(ActionEvent e) {
try {
doc.insertString(doc.getLength(), String.valueOf(newText.charAt(i)), null);
i++;
if (i >= newText.length()) {
((Timer) e.getSource()).stop();
doc.insertString(doc.getLength(), "\n\n", null);
}
} catch (BadLocationException exception) {
exception.printStackTrace();
}
}
}).start();
} else {
doc.insertString(doc.getLength(), text + "\n\n", null);
}
} catch (BadLocationException exception) {
exception.printStackTrace();
}
}
}