-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathg6_ChatBot.java
178 lines (175 loc) · 6.27 KB
/
g6_ChatBot.java
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
package finalproject;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class g6_ChatBot extends JFrame implements KeyListener,ActionListener
{ private JLabel passTitleLabel = new JLabel(" Hi there! how can I help you ?");
private Container container = getContentPane();
private JPanel headerPanel = new JPanel();
private JLabel headerTitleLabel = new JLabel(" BOOTCAMP E-PASS");
private JPanel mainPanel = new JPanel();
private JPanel bodyPanel = new JPanel();
private JTextArea chatBoxTextArea=new JTextArea();
private JTextArea typeTextField=new JTextArea();
private JScrollPane scrollable=new JScrollPane();
private JButton backButton = new JButton("Back");
private JLabel typeLabel = new JLabel("Type Here >");
String[][] chatBot=
{ //standard greetings
{"hi","hello","hola","ola","howdy"},
{"hi","hello","hey"},
//question greetings
{"how are you","how r you","how r u","how are u"},
{"good","doing well"},
//yes
{"yes"},
{"no","NO","NO!!!!!!!"},
//default
{"shut up","you're bad","noob","stop talking",
"I'm is BUSY !! BYE Hehe, due to LOL..Hehe"}
};
g6_ChatBot(){
setLayoutManager();
setHeaderPanel();
setPassLabel();
setBodyPanel();
setLocationAndSize();
setFontAndColor();
setBackButton();
setFrame();
addComponentsToContainer();
setFrame();
}
private void setLayoutManager(){
container.setLayout(null);
}
private void setHeaderPanel(){
headerPanel.setBackground(new Color(251,5,37));
headerPanel.setLayout(new GridLayout(1,1,0,0));
ImageIcon iconLogo = new ImageIcon(new ImageIcon("C:\\Users\\Admin\\Desktop\\icon.png").getImage().getScaledInstance(75,75, Image.SCALE_SMOOTH));
headerTitleLabel.setIcon(iconLogo);
headerTitleLabel.setFont(new Font("Roboto", Font.BOLD,26));
headerTitleLabel.setForeground(Color.white);
headerPanel.add(headerTitleLabel);
}
private void setPassLabel(){
passTitleLabel.setFont(new Font("Roboto", Font.BOLD,23));
passTitleLabel.setBounds(0,85,870,30);
passTitleLabel.setHorizontalAlignment(JLabel.CENTER);
}
private void setBodyPanel(){
chatBoxTextArea.setBounds(35,35,590,300);
typeTextField.setBounds(157,320,469,30);
typeLabel.setBounds(35,320,150,30);
chatBoxTextArea.setLineWrap(true);
chatBoxTextArea.setEditable(false);
typeTextField.setBorder(BorderFactory.createEmptyBorder());
chatBoxTextArea.setBorder(BorderFactory.createEmptyBorder());
scrollable= new JScrollPane(chatBoxTextArea);
scrollable.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollable.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollable.setBounds(35,35,590,270);
chatBoxTextArea.setPreferredSize(new Dimension(760,400));
bodyPanel.add(scrollable);
bodyPanel.add(typeTextField);
bodyPanel.add(typeLabel);
bodyPanel.setLayout(null);
typeTextField.addKeyListener(this);
}
private void setFontAndColor(){
typeTextField.setFont(new Font("Roboto", Font.PLAIN,18));
typeLabel.setFont(new Font("Roboto", Font.BOLD,20));
chatBoxTextArea.setFont(new Font("Roboto", Font.PLAIN,18));
typeTextField.setBackground(Color.WHITE);
bodyPanel.setBackground(new Color(113,113,113));
typeLabel.setForeground(Color.WHITE);
chatBoxTextArea.setOpaque(true);
typeTextField.setOpaque(true);
}
private void setBackButton(){
backButton.setFont(new Font("Roboto", Font.BOLD,17));
backButton.setBounds(10,532,90,40);
backButton.addActionListener(this);
backButton.setBackground(Color.BLACK);
backButton.setForeground(Color.WHITE);
container.add(backButton);
}
private void setLocationAndSize(){
headerPanel.setBounds(0, 0, 870, 70);
bodyPanel.setBounds(100,135,660,378);
}
private void addComponentsToContainer(){
container.add(headerPanel);
container.add(bodyPanel);
container.add(passTitleLabel);
}
private void setFrame(){
Image icon = Toolkit.getDefaultToolkit().getImage("C:\\Users\\Admin\\Desktop\\icon.png");
setIconImage(icon);
setVisible(true);
setSize(870, 610);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
}
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_ENTER)
{ typeTextField.setEditable(false);
String quote=typeTextField.getText();
typeTextField.setText("");
addText("-->You :\t"+quote);
quote.trim();
while(
quote.charAt(quote.length()-1)=='!' ||
quote.charAt(quote.length()-1)=='.' ||
quote.charAt(quote.length()-1)=='?'
)
{ quote=quote.substring(0,quote.length()-1);
}
quote.trim();
byte response=0;
int j=0;
while(response==0)
{ if(inArray(quote.toLowerCase(),chatBot[j*2]))
{ response=2;
int r=(int)Math.floor(Math.random()*chatBot[(j*2)+1].length);
addText("\n-->Bot :\t"+chatBot[(j*2)+1][r]);
}
j++;
if(j*2==chatBot.length-1 && response==0)
{ response=1;
}
}
if(response==1)
{ int r=(int)Math.floor(Math.random()*chatBot[chatBot.length-1].length);
addText("\n-->Bot :\t"+chatBot[chatBot.length-1][r]);
}
addText("\n");
}
}
public void keyReleased(KeyEvent e)
{ if(e.getKeyCode()==KeyEvent.VK_ENTER)
{ typeTextField.setEditable(true);
}
}
public void keyTyped(KeyEvent e)
{}
public void addText(String str)
{ chatBoxTextArea.setText(chatBoxTextArea.getText()+str);
}
public boolean inArray(String in,String[] str)
{ boolean match=false;
for(int i=0;i<str.length;i++)
{ if(str[i].equals(in))
{ match=true;
}
}
return match;
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==backButton)
{
this.toBack();
}
}
}