import javax.swing._
import java.awt._
import java.awt.event._
// =====================================
// AI SCIENCE BUDDY
// Kojo Scala
// =====================================
// Create the main window
val frame = new JFrame("AI Science Buddy")
frame.setSize(700, 550)
frame.setDefaultCloseOperation(
WindowConstants.EXIT_ON_CLOSE
)
frame.setLayout(
new BorderLayout()
)
// =====================================
// TITLE
// =====================================
val title = new JLabel(
"AI SCIENCE BUDDY",
SwingConstants.CENTER
)
title.setFont(
new Font("Arial", Font.BOLD, 28)
)
title.setForeground(
new Color(0, 100, 180)
)
frame.add(
title,
BorderLayout.NORTH
)
// =====================================
// MAIN PANEL
// =====================================
val mainPanel = new JPanel()
mainPanel.setLayout(
new BoxLayout(
mainPanel,
BoxLayout.Y_AXIS
)
)
// =====================================
// QUESTION LABEL
// =====================================
val questionLabel = new JLabel(
"Enter your science question:"
)
questionLabel.setFont(
new Font("Arial", Font.BOLD, 18)
)
mainPanel.add(questionLabel)
// =====================================
// QUESTION BOX
// =====================================
val questionBox = new JTextArea(
4,
50
)
questionBox.setFont(
new Font("Arial", Font.PLAIN, 16)
)
questionBox.setLineWrap(true)
questionBox.setWrapStyleWord(true)
val questionScroll =
new JScrollPane(questionBox)
mainPanel.add(questionScroll)
// =====================================
// ASK BUTTON
// =====================================
val askButton = new JButton(
"ASK SCIENCE BUDDY"
)
askButton.setFont(
new Font("Arial", Font.BOLD, 18)
)
askButton.setForeground(Color.WHITE)
askButton.setBackground(
new Color(0, 120, 200)
)
mainPanel.add(askButton)
// =====================================
// STATUS
// =====================================
val statusLabel = new JLabel(
"Ready! Enter a science question."
)
statusLabel.setFont(
new Font("Arial", Font.PLAIN, 14)
)
statusLabel.setForeground(Color.BLUE)
mainPanel.add(statusLabel)
// =====================================
// ANSWER LABEL
// =====================================
val answerLabel = new JLabel(
"Science Buddy Answer:"
)
answerLabel.setFont(
new Font("Arial", Font.BOLD, 18)
)
mainPanel.add(answerLabel)
// =====================================
// ANSWER BOX
// =====================================
val answerBox = new JTextArea(
12,
50
)
answerBox.setFont(
new Font("Arial", Font.PLAIN, 16)
)
answerBox.setLineWrap(true)
answerBox.setWrapStyleWord(true)
answerBox.setEditable(false)
answerBox.setBackground(
new Color(240, 248, 255)
)
val answerScroll =
new JScrollPane(answerBox)
mainPanel.add(answerScroll)
// Add main panel
frame.add(
mainPanel,
BorderLayout.CENTER
)
// =====================================
// SCIENCE ANSWER FUNCTION
// =====================================
def getScienceAnswer(
question: String
): String = {
val q =
question.toLowerCase
if (
q.contains("gravity")
) {
"Gravity is a force that attracts " +
"objects toward each other. Earth's " +
"gravity pulls objects toward the " +
"center of Earth."
}
else if (
q.contains("sky") &&
q.contains("blue")
) {
"The sky looks blue because sunlight " +
"is scattered by Earth's atmosphere. " +
"Blue light is scattered more strongly " +
"than many other colors."
}
else if (
q.contains("photosynthesis")
) {
"Photosynthesis is the process by which " +
"green plants make food using sunlight, " +
"water, and carbon dioxide. Oxygen is " +
"released during this process."
}
else if (
q.contains("water") &&
q.contains("boil")
) {
"At normal atmospheric pressure, water " +
"boils at about 100 degrees Celsius. " +
"Boiling happens when water changes " +
"from liquid into gas."
}
else if (
q.contains("atom")
) {
"An atom is the basic unit of ordinary " +
"matter. It contains a nucleus made of " +
"protons and neutrons, with electrons " +
"around the nucleus."
}
else if (
q.contains("solar system")
) {
"The Solar System consists of the Sun " +
"and the objects that orbit it, including " +
"eight planets, dwarf planets, moons, " +
"asteroids, and comets."
}
else if (
q.contains("plant") &&
q.contains("food")
) {
"Plants make their own food mainly " +
"through photosynthesis. They use " +
"sunlight, water, and carbon dioxide " +
"to produce glucose and oxygen."
}
else if (
q.contains("earth") &&
q.contains("moon")
) {
"The Moon is Earth's natural satellite. " +
"It orbits Earth and takes about 27.3 " +
"days to complete one orbit relative " +
"to the stars."
}
else if (
q.contains("oxygen")
) {
"Oxygen is a chemical element with the " +
"symbol O. It is important for respiration " +
"in humans and many other organisms."
}
else if (
q.contains("cell")
) {
"A cell is the basic structural and " +
"functional unit of living organisms. " +
"Some organisms consist of only one cell, " +
"while others contain many cells."
}
else if (
q.contains("force")
) {
"A force is a push or pull that can change " +
"an object's motion. Force is measured " +
"in newtons (N)."
}
else if (
q.contains("energy")
) {
"Energy is the ability to do work or cause " +
"change. Common forms include kinetic, " +
"potential, thermal, chemical, electrical, " +
"and light energy."
}
else if (
q.contains("human body")
) {
"The human body is made of many systems " +
"that work together, including the nervous, " +
"circulatory, respiratory, digestive, and " +
"musculoskeletal systems."
}
else {
"I don't have a prepared answer for that " +
"question yet.\n\nTry asking about topics " +
"such as gravity, atoms, photosynthesis, " +
"plants, cells, energy, forces, oxygen, " +
"the Solar System, or the human body."
}
}
// =====================================
// ASK BUTTON
// =====================================
askButton.addActionListener(
new ActionListener {
def actionPerformed(
event: ActionEvent
): Unit = {
val question =
questionBox.getText.trim
if (
question.length == 0
) {
JOptionPane.showMessageDialog(
frame,
"Please enter a science question.",
"Science Buddy",
JOptionPane.WARNING_MESSAGE
)
}
else {
statusLabel.setText(
"Thinking..."
)
val answer =
getScienceAnswer(question)
answerBox.setText(
answer
)
statusLabel.setText(
"Answer ready!"
)
}
}
}
)
// =====================================
// SHOW WINDOW
// =====================================
frame.setLocationRelativeTo(null)
frame.setVisible(true)