Code Sketch
ai science
Category: Art
import javax.swing._
import java.awt._
import java.awt.event._
// ============================================================
// AI SCIENCE BUDDY
// KOJO / SCALA LEARNING APP
// ============================================================
// ============================================================
// COLORS
// ============================================================
val BLUE = new Color(0, 105, 180)
val DARK_BLUE = new Color(0, 70, 130)
val LIGHT_BLUE = new Color(235, 247, 255)
val GREEN = new Color(30, 140, 80)
val LIGHT_GREEN = new Color(235, 250, 240)
val RED = new Color(210, 70, 60)
val DARK_TEXT = new Color(35, 35, 35)
// ============================================================
// MAIN WINDOW
// ============================================================
val frame = new JFrame("AI Science Buddy")
frame.setSize(900, 720)
frame.setDefaultCloseOperation(
WindowConstants.EXIT_ON_CLOSE
)
frame.setLayout(
new BorderLayout(10, 10)
)
// ============================================================
// TITLE
// ============================================================
val titlePanel = new JPanel()
titlePanel.setLayout(
new BorderLayout()
)
titlePanel.setBackground(
new Color(225, 242, 255)
)
val title = new JLabel(
"AI SCIENCE BUDDY",
SwingConstants.CENTER
)
title.setFont(
new Font("Arial", Font.BOLD, 30)
)
title.setForeground(
DARK_BLUE
)
titlePanel.add(
title,
BorderLayout.CENTER
)
val subtitle = new JLabel(
"Learn Science in Simple Words - Step by Step",
SwingConstants.CENTER
)
subtitle.setFont(
new Font("Arial", Font.PLAIN, 16)
)
subtitle.setForeground(
DARK_TEXT
)
titlePanel.add(
subtitle,
BorderLayout.SOUTH
)
frame.add(
titlePanel,
BorderLayout.NORTH
)
// ============================================================
// MAIN PANEL
// ============================================================
val mainPanel = new JPanel()
mainPanel.setLayout(
new BoxLayout(
mainPanel,
BoxLayout.Y_AXIS
)
)
mainPanel.setBorder(
BorderFactory.createEmptyBorder(
15,
20,
15,
20
)
)
// ============================================================
// LANGUAGE
// ============================================================
val languagePanel = new JPanel(
new FlowLayout(
FlowLayout.LEFT
)
)
val languageLabel = new JLabel(
"Choose Language:"
)
languageLabel.setFont(
new Font("Arial", Font.BOLD, 16)
)
languagePanel.add(
languageLabel
)
val languageBox = new JComboBox(
Array(
"English",
"Hindi",
"Marathi"
)
)
languageBox.setFont(
new Font("Arial", Font.PLAIN, 16)
)
languagePanel.add(
languageBox
)
mainPanel.add(
languagePanel
)
// ============================================================
// QUESTION
// ============================================================
val questionLabel = new JLabel(
"Ask your science question:"
)
questionLabel.setFont(
new Font("Arial", Font.BOLD, 18)
)
mainPanel.add(
questionLabel
)
// ============================================================
// QUESTION BOX
// ============================================================
val questionBox = new JTextArea(
4,
60
)
questionBox.setFont(
new Font("Arial", Font.PLAIN, 17)
)
questionBox.setLineWrap(true)
questionBox.setWrapStyleWord(true)
questionBox.setBorder(
BorderFactory.createLineBorder(
new Color(160, 190, 210)
)
)
val questionScroll = new JScrollPane(
questionBox
)
mainPanel.add(
questionScroll
)
// ============================================================
// QUESTION EXAMPLES
// ============================================================
val exampleLabel = new JLabel(
"<html>" +
"<b>Examples:</b> What is gravity? " +
"What is photosynthesis? " +
"What is meant by infectious diseases?</html>"
)
exampleLabel.setFont(
new Font("Arial", Font.PLAIN, 13)
)
exampleLabel.setForeground(
new Color(70, 90, 110)
)
mainPanel.add(
exampleLabel
)
// ============================================================
// BUTTON PANEL
// ============================================================
val buttonPanel = new JPanel(
new FlowLayout(
FlowLayout.CENTER,
10,
10
)
)
// ASK BUTTON
val askButton = new JButton(
"ASK SCIENCE BUDDY"
)
askButton.setFont(
new Font("Arial", Font.BOLD, 16)
)
askButton.setBackground(
BLUE
)
askButton.setForeground(
Color.WHITE
)
buttonPanel.add(
askButton
)
// SPEAK BUTTON
val speakButton = new JButton(
"SPEAK ANSWER"
)
speakButton.setFont(
new Font("Arial", Font.BOLD, 16)
)
speakButton.setBackground(
GREEN
)
speakButton.setForeground(
Color.WHITE
)
buttonPanel.add(
speakButton
)
// CLEAR BUTTON
val clearButton = new JButton(
"CLEAR"
)
clearButton.setFont(
new Font("Arial", Font.BOLD, 16)
)
clearButton.setBackground(
RED
)
clearButton.setForeground(
Color.WHITE
)
buttonPanel.add(
clearButton
)
mainPanel.add(
buttonPanel
)
// ============================================================
// STATUS
// ============================================================
val statusLabel = new JLabel(
"Ready! Ask me a science question."
)
statusLabel.setFont(
new Font("Arial", Font.BOLD, 14)
)
statusLabel.setForeground(
BLUE
)
mainPanel.add(
statusLabel
)
// ============================================================
// ANSWER LABEL
// ============================================================
val answerLabel = new JLabel(
"Science Buddy Answer:"
)
answerLabel.setFont(
new Font("Arial", Font.BOLD, 19)
)
answerLabel.setForeground(
GREEN
)
mainPanel.add(
answerLabel
)
// ============================================================
// ANSWER BOX
// ============================================================
val answerBox = new JTextArea(
15,
60
)
answerBox.setFont(
new Font("Arial", Font.PLAIN, 17)
)
answerBox.setLineWrap(true)
answerBox.setWrapStyleWord(true)
answerBox.setEditable(false)
answerBox.setBackground(
LIGHT_BLUE
)
answerBox.setForeground(
DARK_TEXT
)
answerBox.setBorder(
BorderFactory.createLineBorder(
new Color(160, 200, 220)
)
)
val answerScroll = new JScrollPane(
answerBox
)
mainPanel.add(
answerScroll
)
// ============================================================
// HOW TO ASK
// ============================================================
val howToLabel = new JLabel(
"<html>" +
"<b>How to ask:</b><br>" +
"Type a science question in simple words.<br>" +
"Example: What is meant by infectious diseases?<br>" +
"Example: Why is the sky blue?<br>" +
"Example: How do plants make food?</html>"
)
howToLabel.setFont(
new Font("Arial", Font.PLAIN, 14)
)
howToLabel.setOpaque(true)
howToLabel.setBackground(
LIGHT_GREEN
)
howToLabel.setBorder(
BorderFactory.createEmptyBorder(
8,
8,
8,
8
)
)
mainPanel.add(
howToLabel
)
// ============================================================
// ADD MAIN PANEL
// ============================================================
frame.add(
mainPanel,
BorderLayout.CENTER
)
// ============================================================
// SCIENCE ANSWER ENGINE
// ============================================================
def getScienceAnswer(
question: String,
language: String
): String = {
val q = question.toLowerCase.trim
// ==========================================================
// INFECTIOUS DISEASES
// ==========================================================
if (
q.contains("infectious") ||
q.contains("contagious")
) {
if (language == "Hindi") {
return """
???????? ??? ???? ????
??? 1: ????
???????? ??? ??? ??? ??? ?? ??? ???? ???? ???? ???????????? ?? ???? ???? ????
??? 2: ??? ?? ???????????
????? ?????, ??????????, ???? ?? ??? ?????? ????? ?? ???? ????
??? 3: ?? ???? ??? ???? ????
??? ???????? ??? ???, ????? ???? ?? ????, ???? ?????? ?? ??? ?????? ?? ?????? ?? ??? ???? ????
??? 4: ??????
???? ?? ???? ???????? ????? ?? ?????? ????
??? ?????? ???:
???????? ??? ???????? ?? ???? ???? ???? ??? ???? ??? ???????? ??? ?? ??????? ?? ????? ??????? ?? ??? ???? ????
"""
}
if (language == "Marathi") {
return """
?????????? ??? ?????? ????
????? 1: ????
?????????? ??? ?? ??? ??????? ???????? ???????????????? ?????? ??? ????.
????? 2: ????? ???????????
??????? ??????, ??????????, ????? ??? ???? ?????? ????? ?????? ??? ????.
????? 3: ?? ??? ???? ??????
???? ?????????? ??? ???, ????? ???? ????? ????, ??? ?????? ????? ???? ???????????? ???? ?????.
????? 4: ???????
???? ??? ???? ?? ?????????? ??????? ??????? ????.
?????? ???????:
?????????? ??? ?? ???????????? ?????. ???? ?????????? ??? ??? ??????????? ??????? ?????????? ???? ?????.
"""
}
return """
WHAT ARE INFECTIOUS DISEASES?
Step 1: Meaning
Infectious diseases are illnesses caused by harmful microorganisms.
Step 2: What can cause them?
They can be caused by viruses, bacteria, fungi, or some parasites.
Step 3: How can they spread?
Some infectious diseases can spread through the air, contaminated food or water, direct contact, or certain insects.
Step 4: Examples
Flu and measles are examples of infectious diseases.
In simple words:
An infectious disease is an illness caused by germs. Some infectious diseases can spread from one person to another.
"""
}
// ==========================================================
// GRAVITY
// ==========================================================
else if (q.contains("gravity")) {
if (language == "Hindi") {
return """
????????????? ???? ???
??? 1:
????????????? ?? ?? ???
??? 2:
?? ??????? ?? ??-????? ?? ?? ?????? ???
??? 3:
?????? ?? ????????????? ??????? ?? ?????? ?? ?? ?????? ???
??????:
?? ?? ???? ?? ??? ?? ?????? ???, ?? ?? ???? ????? ???
??? ?????? ???:
????????????? ?? ?? ?? ?? ??????? ?? ?????? ?? ?? ?????? ???
"""
}
if (language == "Marathi") {
return """
????????????? ?????? ????
????? 1:
????????????? ?? ?? ?? ???.
????? 2:
?? ???????? ?????????? ?????.
????? 3:
???????? ????????????? ???????? ?????????? ?????? ?????.
??????:
??? ????? ????? ?? ?? ???? ????.
?????? ???????:
????????????? ?? ???????? ?????????? ?????? ??????? ?? ???.
"""
}
return """
WHAT IS GRAVITY?
Step 1:
Gravity is a force.
Step 2:
It pulls objects toward each other.
Step 3:
Earth's gravity pulls objects toward Earth.
Example:
When you drop a ball, it falls toward the ground because of gravity.
In simple words:
Gravity is the force that pulls objects toward Earth.
"""
}
// ==========================================================
// PHOTOSYNTHESIS
// ==========================================================
else if (
q.contains("photosynthesis") ||
(
q.contains("plant") &&
q.contains("food")
)
) {
if (language == "Hindi") {
return """
?????? ???????? ???? ???
??? 1:
???? ????? ?? ?????? ??????? ???? ????
??? 2:
???? ?????? ?? ???? ???? ????
??? 3:
???? ??? ?? ?????? ?????????? ???? ????
??? 4:
???? ????? ?? ?????? ?? ??? ?? ???? ???? ????? ????
??? 5:
?? ????????? ??? ??????? ?? ?????? ???
??? ?????? ???:
?????? ???????? ?? ????????? ?? ?????? ??? ???? ????? ?? ?????? ?? ??? ?? ???? ???? ????? ????
"""
}
if (language == "Marathi") {
return """
?????????????? ?????? ????
????? 1:
??????? ??????????? ?????.
????? 2:
??????? ???????? ???? ?????.
????? 3:
??????? ?????? ?????? ?????????? ?????.
????? 4:
???????????????? ?????? ??????? ??????? ???? ???? ?????.
????? 5:
?? ?????????? ??????? ????? ????? ????.
?????? ???????:
?????????????? ?????? ???????????????? ?????? ??????? ?????????? ??????? ???? ???? ???????? ?????????.
"""
}
return """
WHAT IS PHOTOSYNTHESIS?
Step 1:
Plants receive sunlight.
Step 2:
Plants take water from the soil.
Step 3:
Plants take carbon dioxide from the air.
Step 4:
Plants use sunlight to make their food.
Step 5:
Oxygen is released during this process.
In simple words:
Photosynthesis is the process in which green plants make their own food using sunlight, water, and carbon dioxide.
"""
}
// ==========================================================
// ATOM
// ==========================================================
else if (q.contains("atom")) {
if (language == "Hindi") {
return """
?????? ???? ???
??? 1:
?????? ?????? ?? ???? ???? ???? ???
??? 2:
?????? ?? ??? ??? ????? ???? ???
??? 3:
????? ??? ??????? ?? ????????? ???? ????
??? 4:
?????????? ????? ?? ????? ??? ???? ????
??? ?????? ???:
?????? ?????? ?? ?? ???? ???? ???????-??? ???
"""
}
if (language == "Marathi") {
return """
??? ?????? ????
????? 1:
??? ?? ????????? ????? ???? ??? ???.
????? 2:
??????? ???????? ??????? ????.
????? 3:
????????????? ??????? ??? ????????? ?????.
????? 4:
?????????? ????????????? ?????.
?????? ???????:
??? ?? ????????? ????? ???? ??????? ??? ???.
"""
}
return """
WHAT IS AN ATOM?
Step 1:
An atom is a very small unit of matter.
Step 2:
It has a center called the nucleus.
Step 3:
The nucleus contains protons and neutrons.
Step 4:
Electrons are found around the nucleus.
In simple words:
An atom is a tiny building block of matter.
"""
}
// ==========================================================
// OXYGEN
// ==========================================================
else if (q.contains("oxygen")) {
if (language == "Hindi") {
return """
??????? ???? ???
??? 1:
??????? ?? ???????? ???? ???
??? 2:
???? ???????? ????? O ???
??? 3:
?????? ?? ?? ??? ????? ?? ??? ??????? ?? ????? ???? ????
??? 4:
??????? ????? ???? ??? ????? ??????? ???? ?? ????????? ??? ?????????? ???
??? ?????? ???:
??????? ?? ?????????? ??? ?? ????? ???????? ?????? ?? ?? ????? ?? ????? ?? ??? ???? ???
"""
}
if (language == "Marathi") {
return """
??????? ?????? ????
????? 1:
??????? ?? ?? ???????? ????????? ???.
????? 2:
?????? ???????? ????? O ???.
????? 3:
????? ??? ???? ???? ?????????? ????????? ???? ?????.
????? 4:
??????? ????? ???????????? ??????? ?????????? ???.
?????? ???????:
??????? ?? ?????????? ?????? ?????? ?????????? ???? ???.
"""
}
return """
WHAT IS OXYGEN?
Step 1:
Oxygen is a chemical element.
Step 2:
Its chemical symbol is O.
Step 3:
Humans and many organisms use oxygen for respiration.
Step 4:
Oxygen is important for releasing energy from food in our cells.
In simple words:
Oxygen is an important gas that humans and many organisms need for respiration.
"""
}
// ==========================================================
// CELL
// ==========================================================
else if (q.contains("cell")) {
if (language == "Hindi") {
return """
?????? ???? ???
??? 1:
?????? ????? ????? ?? ??? ???? ???
??? 2:
????? ???? ???? ???? ???????? ?? ??? ???
??? 3:
??? ???? ??? ???? ?? ?????? ?? ??? ???? ????
??? 4:
???????? ???? ??? ???-??? ??? ???? ????
??? ?????? ???:
?????? ????? ???? ?? ?? ???? ???????-??? ???
"""
}
if (language == "Marathi") {
return """
???? ?????? ????
????? 1:
???? ?? ???????? ?????? ??? ???.
????? 2:
???? ???? ???? ??????? ?????? ???.
????? 3:
???? ??????? ???? ???? ??? ?????? ?????? ?????.
????? 4:
???? ?????????? ???????? ???? ?????.
?????? ???????:
???? ?? ???? ??????? ?? ???? ??????? ??? ???.
"""
}
return """
WHAT IS A CELL?
Step 1:
A cell is the basic unit of living things.
Step 2:
Our body is made of many cells.
Step 3:
Some tiny organisms have only one cell.
Step 4:
Cells perform different jobs in living organisms.
In simple words:
A cell is a small building block of a living organism.
"""
}
// ==========================================================
// FORCE
// ==========================================================
else if (q.contains("force")) {
if (language == "Hindi") {
return """
?? ???? ???
??? 1:
?? ?? ????? ?? ?????? ???
??? 2:
?? ???? ????? ?? ??? ??? ???? ???
??? 3:
?? ???? ????? ?? ?????, ????? ?? ???? ???? ????? ?? ?? ???? ???
??? 4:
?? ?? ?????? (N) ??? ???? ???? ???
??????:
???? ?? ????? ???? ?? ???? ??? ???? ???
??? ?????? ???:
?? ???? ????? ?? ????? ??? ????? ?? ?????? ???
"""
}
if (language == "Marathi") {
return """
?? ?????? ????
????? 1:
?? ?????? ????? ????? ????.
????? 2:
??????? ??????? ??? ???? ????.
????? 3:
??????? ????? ???? ??? ????, ????? ???? ????? ???? ???? ????.
????? 4:
?? ?????? (N) ????? ????? ????.
??????:
??????? ????????? ?? ???? ????.
?????? ???????:
?? ?????? ??????? ??????? ????? ????? ????.
"""
}
return """
WHAT IS FORCE?
Step 1:
Force is a push or a pull.
Step 2:
A force can change the motion of an object.
Step 3:
A force can start an object, stop it, or change its direction.
Step 4:
Force is measured in newtons (N).
Example:
When you push a ball, the ball moves.
In simple words:
Force is a push or pull acting on an object.
"""
}
// ==========================================================
// ENERGY
// ==========================================================
else if (q.contains("energy")) {
if (language == "Hindi") {
return """
????? ???? ???
??? 1:
????? ??? ???? ?? ???????? ???? ?? ?????? ???
??? 2:
????? ?? ?? ??? ???? ????
??? 3:
?????? ??? ?????? ?????, ?????? ?????, ???????? ????? ?? ??????? ??????
??? 4:
????? ???? ?? ???? ?? ???????? ????? ????? ???
??? ?????? ???:
????? ?? ?????? ?? ????? ??? ???? ?? ???? ?? ?? ???????? ?? ???? ???
"""
}
if (language == "Marathi") {
return """
????? ?????? ????
????? 1:
????? ?????? ??? ???????? ????? ??? ????????? ??????.
????? 2:
??????? ???? ?????? ????.
????? 3:
??????? ?????? ?????? ?????, ?????? ?????, ???????? ????? ??? ??????? ?????.
????? 4:
?????? ??????? ???????? ???????? ????? ?????.
?????? ???????:
????? ?????? ??? ???????? ????? ??? ????????? ??????.
"""
}
return """
WHAT IS ENERGY?
Step 1:
Energy is the ability to do work or cause change.
Step 2:
Energy has many forms.
Step 3:
Examples include light energy, thermal energy, chemical energy, and electrical energy.
Step 4:
Our bodies get chemical energy from food.
In simple words:
Energy is the ability to do work or make a change.
"""
}
// ==========================================================
// SOLAR SYSTEM
// ==========================================================
else if (
q.contains("solar system") ||
q.contains("planets")
) {
if (language == "Hindi") {
return """
??? ???? ???? ???
??? 1:
????? ????? ??? ???? ?? ?????? ??? ???
??? 2:
?? ???? ????? ?? ????? ?? ???????? ???? ????
??? 3:
??? ???? ??? ???????, ??????????? ?? ??????? ?? ????
??? 4:
?????? ??? ???? ?? ?? ???? ???
??? ?????? ???:
????? ?? ???? ????? ?? ????? ???? ??????? ?? ???? ?? ??? ???? ???? ????
"""
}
if (language == "Marathi") {
return """
??????? ?????? ????
????? 1:
????? ?? ?????? ??????????? ???????????? ???.
????? 2:
?? ???? ??????????? ??????.
????? 3:
???????????? ?????, ??????? ??? ??????? ??????? ?????? ????.
????? 4:
?????? ?? ?????????? ?? ???? ???.
?????? ???????:
????? ??? ????????????? ????????? ?????????? ??????? ??????? ???????.
"""
}
return """
WHAT IS THE SOLAR SYSTEM?
Step 1:
The Sun is at the center of our Solar System.
Step 2:
Eight planets orbit the Sun.
Step 3:
The Solar System also contains moons, asteroids, and comets.
Step 4:
Earth is one of the planets in our Solar System.
In simple words:
The Solar System is the Sun and the objects that orbit it.
"""
}
// ==========================================================
// MOON
// ==========================================================
else if (q.contains("moon")) {
if (language == "Hindi") {
return """
??????? ???? ???
??? 1:
??????? ?????? ?? ????????? ?????? ???
??? 2:
??????? ?????? ?? ????? ?? ????? ???
??? 3:
??????? ???? ?????? ??? ???? ??????
??? 4:
??????? ????? ?? ?????? ?? ????????? ???? ???
??? ?????? ???:
??????? ?????? ?? ????? ?? ????? ???? ????? ????????? ?????? ???
"""
}
if (language == "Marathi") {
return """
????? ?????? ????
????? 1:
????? ?? ???????? ???????? ?????? ???.
????? 2:
????? ??????????? ?????.
????? 3:
????? ??????? ?????? ??????? ??? ????.
????? 4:
????? ??????????? ????????? ????.
?????? ???????:
????? ?? ??????????? ??????? ???????? ?????? ???.
"""
}
return """
WHAT IS THE MOON?
Step 1:
The Moon is Earth's natural satellite.
Step 2:
The Moon travels around Earth.
Step 3:
The Moon does not make its own visible light.
Step 4:
It reflects sunlight.
In simple words:
The Moon is Earth's natural satellite that travels around our planet.
"""
}
// ==========================================================
// WATER BOILING
// ==========================================================
else if (
q.contains("water") &&
(
q.contains("boil") ||
q.contains("boiling")
)
) {
if (language == "Hindi") {
return """
???? ????? ????? ???
??? 1:
???? ?? ???? ???? ?? ???? ?? ???? ?? ??? ???? ???? ????
??? 2:
?????? ????? ?? ???? ?? ?? ???? ????? ??????? ???? ????
??? 3:
??????? ?????????? ???? ?? ???? ???? 100�C ?? ????? ???
??? 4:
????? ??? ???? ??? ?? ??? ??? ????? ???
??? ?????? ???:
????? ????? ?? ???? ?? ?? ???? ?? ??? ???? ??? ?? ???????? ???? ???? ?? ???? ??? ??? ??? ???? ???
"""
}
if (language == "Marathi") {
return """
???? ?? ??????
????? 1:
???? ??? ???????? ?????? ?? ?????? ???? ??????.
????? 2:
?????? ????????? ?????????? ?????? ???? ????? ?????.
????? 3:
??????? ????????? ?????? ???? ?????? 100�C ?? ?????.
????? 4:
??????? ???? ?????????? ????????? ?????.
?????? ???????:
?????? ?????????? ???????? ?? ?????? ?????? ??? ?????? ??? ???????? ???? ????????? ?????.
"""
}
return """
WHY DOES WATER BOIL?
Step 1:
When water is heated, its particles move faster.
Step 2:
As the temperature increases, the particles gain more energy.
Step 3:
At normal atmospheric pressure, water boils at about 100�C.
Step 4:
During boiling, water changes from a liquid into a gas.
In simple words:
Heat makes water particles move faster. When water gets hot enough, it changes into gas.
"""
}
// ==========================================================
// SKY BLUE
// ==========================================================
else if (
q.contains("sky") &&
q.contains("blue")
) {
if (language == "Hindi") {
return """
????? ???? ????? ????? ???? ???
??? 1:
????? ?? ?????? ?? ????? ?? ??? ???? ???
??? 2:
????? ?? ?????? ?????? ?? ???????? ??? ?????? ???? ???
??? 3:
???????? ??? ???? ????? ???? ?????? ???
??? 4:
?? ????? ??? ???? ????? ????? ????? ?? ??????? ???
??? ?????? ???:
???????? ??? ???? ????? ???? ?????? ?? ???? ????? ???? ???? ????? ???? ???
"""
}
if (language == "Marathi") {
return """
???? ???? ?? ??????
????? 1:
??????????? ???? ??????? ?????? ????.
????? 2:
??????????? ?????????? ????????? ?????? ????.
????? 3:
????????? ???? ?????? ????? ???????? ??????? ????.
????? 4:
?? ????????? ???? ?????? ?????? ????????????? ???????.
?????? ???????:
????????? ???? ?????? ????? ????????????? ???? ???????? ???? ?????.
"""
}
return """
WHY IS THE SKY BLUE?
Step 1:
Sunlight contains many colors.
Step 2:
Sunlight enters Earth's atmosphere.
Step 3:
Blue light is scattered strongly by gases in the atmosphere.
Step 4:
The scattered blue light reaches our eyes.
In simple words:
The sky looks blue because blue light is scattered strongly in Earth's atmosphere.
"""
}
// ==========================================================
// HUMAN BODY
// ==========================================================
else if (
q.contains("human body") ||
q.contains("body system")
) {
if (language == "Hindi") {
return """
???? ???? ???? ???
??? 1:
???? ???? ???? ???? ???????? ?? ??? ???
??? 2:
???????? ????? ??? ?? ??? ????? ????
??? 3:
???-??? ??? ????? ???? ?? ?????????? ????? ????
??? 4:
?????? ??? ????? ?????, ???? ?????, ???? ???????? ????? ?? ???????? ??????
??? ?????? ???:
????? ???? ?? ???-??? ??? ????? ???? ?? ??? ???? ??? ??? ???? ????
"""
}
if (language == "Marathi") {
return """
????? ???? ?????? ????
????? 1:
????? ???? ???? ??????? ?????? ???.
????? 2:
???? ????? ???? ??? ??? ???? ???? ?????.
????? 3:
???????? ???? ????? ????????? ??????? ???? ?????.
????? 4:
??????? ?????? ???????????, ?????????, ?????????? ?????? ??? ???????????.
?????? ???????:
??????? ???????? ??? ????? ??? ???? ??????? ????? ??????? ??? ?????.
"""
}
return """
WHAT IS THE HUMAN BODY?
Step 1:
The human body is made of many cells.
Step 2:
Cells form tissues and organs.
Step 3:
Different organs work together in body systems.
Step 4:
Examples include the respiratory, digestive, circulatory, and nervous systems.
In simple words:
Different parts of the body work together to keep the body alive and working.
"""
}
// ==========================================================
// DEFAULT
// ==========================================================
else {
if (language == "Hindi") {
return """
???? ?? ?????? ?? ????? ????? ??? ???? ?????
?? ?? ??? ?????? ??? ???? ???:
1. ????????????? ???? ???
2. ?????? ???????? ???? ???
3. ?????? ???? ???
4. ???????? ??? ???? ????
5. ??????? ???? ???
6. ?????? ???? ???
7. ?? ???? ???
8. ????? ???? ???
9. ???? ????? ????? ???
10. ????? ???? ????? ????? ???? ???
??? ??????? ?????? ????? ?? ??? ?????? ???? ?? ?????? ???-??-??? ????? ??????
"""
}
if (language == "Marathi") {
return """
??? ?? ????????? ???? ????? ????? ?????? ????.
?????? ??? ?????? ?????? ????:
1. ????????????? ?????? ????
2. ?????????????? ?????? ????
3. ??? ?????? ????
4. ?????????? ??? ?????? ????
5. ??????? ?????? ????
6. ???? ?????? ????
7. ?? ?????? ????
8. ????? ?????? ????
9. ???? ?? ??????
10. ???? ???? ?? ??????
???? ??????? ?????? ?????? ??? ?????? ?????????? ?? ???-??-??? ????? ????.
"""
}
return """
I DO NOT HAVE A PREPARED ANSWER FOR THIS QUESTION YET.
You can ask questions like:
1. What is gravity?
2. What is photosynthesis?
3. What is an atom?
4. What are infectious diseases?
5. What is oxygen?
6. What is a cell?
7. What is force?
8. What is energy?
9. Why does water boil?
10. Why is the sky blue?
Ask your question in simple words and I will give a step-by-step answer when the topic is available.
"""
}
}
// ============================================================
// SPEAK FUNCTION
// ============================================================
def speakAnswer(
text: String
): Unit = {
try {
val cleanText =
text
.replace("\n", " ")
.replace("\"", "")
.replace("'", "")
val operatingSystem =
System.getProperty(
"os.name"
).toLowerCase
if (
operatingSystem.contains("win")
) {
val command =
"PowerShell -Command \"Add-Type -AssemblyName System.Speech; " +
"$s = New-Object System.Speech.Synthesis.SpeechSynthesizer; " +
"$s.Speak('" +
cleanText +
"')\""
Runtime.getRuntime.exec(
command
)
statusLabel.setText(
"Speaking answer..."
)
}
else if (
operatingSystem.contains("mac")
) {
Runtime.getRuntime.exec(
Array(
"say",
cleanText
)
)
statusLabel.setText(
"Speaking answer..."
)
}
else {
Runtime.getRuntime.exec(
Array(
"espeak",
cleanText
)
)
statusLabel.setText(
"Speaking answer..."
)
}
}
catch {
case error: Exception =>
JOptionPane.showMessageDialog(
frame,
"Voice is not available on this computer.\n\n" +
"The written answer is still available.",
"Voice Information",
JOptionPane.INFORMATION_MESSAGE
)
}
}
// ============================================================
// ASK BUTTON
// ============================================================
askButton.addActionListener(
new ActionListener {
def actionPerformed(
event: ActionEvent
): Unit = {
val question =
questionBox.getText.trim
if (
question.length == 0
) {
JOptionPane.showMessageDialog(
frame,
"Please type a science question.",
"Science Buddy",
JOptionPane.WARNING_MESSAGE
)
}
else {
statusLabel.setText(
"Science Buddy is thinking..."
)
val language =
languageBox
.getSelectedItem
.toString
val answer =
getScienceAnswer(
question,
language
)
answerBox.setText(
answer
)
answerBox.setCaretPosition(
0
)
statusLabel.setText(
"Answer ready!"
)
}
}
}
)
// ============================================================
// SPEAK BUTTON
// ============================================================
speakButton.addActionListener(
new ActionListener {
def actionPerformed(
event: ActionEvent
): Unit = {
val answer =
answerBox.getText.trim
if (
answer.length == 0
) {
JOptionPane.showMessageDialog(
frame,
"Please ask a question first.",
"Science Buddy",
JOptionPane.WARNING_MESSAGE
)
}
else {
speakAnswer(
answer
)
}
}
}
)
// ============================================================
// CLEAR BUTTON
// ============================================================
clearButton.addActionListener(
new ActionListener {
def actionPerformed(
event: ActionEvent
): Unit = {
questionBox.setText(
""
)
answerBox.setText(
""
)
statusLabel.setText(
"Ready! Ask me a science question."
)
questionBox.requestFocus()
}
}
)
// ============================================================
// CTRL + ENTER = ASK
// ============================================================
questionBox.addKeyListener(
new KeyAdapter {
override def keyPressed(
event: KeyEvent
): Unit = {
if (
event.getKeyCode == KeyEvent.VK_ENTER &&
event.isControlDown
) {
askButton.doClick()
}
}
}
)
// ============================================================
// SHOW WINDOW
// ============================================================
frame.setLocationRelativeTo(
null
)
frame.setVisible(
true
)
questionBox.requestFocus()