Code Sketch


yoiiiiii
By: Mhalsakant School
Category: Programming
import javax.swing._
import java.awt._
import java.awt.event._
import java.io._
import scala.collection.mutable.ArrayBuffer

val secret =
  Array(121,97,100,110,101,115,104,50,48,49,51)
    .map(_.toChar)
    .mkString

var speechProcess: Process = null
var stopRequested: Boolean = false
var autoTeaching: Boolean = false

def makeButton(text: String): JButton = {
  val b = new JButton(text)
  b.setFont(new Font("SansSerif", Font.BOLD, 14))
  b
}

def stopVoice(): Unit = {
  stopRequested = true

  try {
    if (speechProcess != null) {
      speechProcess.destroy()
      speechProcess = null
    }
  } catch {
    case _: Throwable =>
      speechProcess = null
  }
}

def getVoices(): Array[String] = {

  val result = new ArrayBuffer[String]()

  try {

    val command =
      "Add-Type -AssemblyName System.Speech; " +
      "$s=New-Object System.Speech.Synthesis.SpeechSynthesizer; " +
      "$s.GetInstalledVoices() | ForEach-Object { $_.VoiceInfo.Name }"

    val p =
      new ProcessBuilder(
        "powershell.exe",
        "-NoProfile",
        "-ExecutionPolicy",
        "Bypass",
        "-Command",
        command
      ).start()

    val reader =
      new BufferedReader(
        new InputStreamReader(
          p.getInputStream,
          "UTF-8"
        )
      )

    var line: String = null

    while ({
      line = reader.readLine()
      line != null
    }) {

      val text = line.trim

      if (text.length > 0) {
        result += text
      }
    }

    p.waitFor()

  } catch {
    case _: Throwable =>
  }

  result.distinct.toArray
}

def speak(
    text: String,
    voice: String,
    rate: Int
): Boolean = {

  stopRequested = false

  val safeText =
    text
      .replace("&", " and ")
      .replace("\r", " ")
      .replace("\n", " ")
      .replace("\"", "'")
      .replace("'", "''")

  val safeVoice =
    voice.replace("'", "''")

  val voicePart =
    if (voice.length > 0) {
      "$s.SelectVoice('" +
      safeVoice +
      "'); "
    } else {
      ""
    }

  val command =
    "Add-Type -AssemblyName System.Speech; " +
    "$s=New-Object System.Speech.Synthesis.SpeechSynthesizer; " +
    "$s.Rate=" + rate + "; " +
    "$s.Volume=100; " +
    voicePart +
    "$s.Speak('" +
    safeText +
    "');"

  try {

    speechProcess =
      new ProcessBuilder(
        "powershell.exe",
        "-NoProfile",
        "-ExecutionPolicy",
        "Bypass",
        "-Command",
        command
      ).start()

    speechProcess.waitFor()

    speechProcess = null

    !stopRequested

  } catch {
    case _: Throwable =>
      speechProcess = null
      false
  }
}

def openTeacher(): Unit = {

  val frame =
    new JFrame(
      "AI Teacher - Scala Swing Classroom"
    )

  frame.setSize(
    1250,
    820
  )

  frame.setDefaultCloseOperation(
    WindowConstants.EXIT_ON_CLOSE
  )

  frame.setLocationRelativeTo(null)

  val lessons =
    Array(
      Array(
        "WELCOME",
        "WELCOME TO SCALA SWING\n\n" +
        "AI TEACHER CLASSROOM\n\n" +
        "Today:\n" +
        "Learn Scala Swing\n" +
        "with ChatGPT",
        "???????! ??????? ??? Scala Swing ??????? ??????? ?????. ?? ???????? ???? ???? ?????? ??? ?????? ?????? ??????? ??????? ???."
      ),
      Array(
        "STEP 1",
        "OPEN CHATGPT\n\n" +
        "1. Open ChatGPT\n" +
        "2. Start New Chat\n" +
        "3. Get Ready",
        "??????? ??? ChatGPT ???? ??? ???? chat ???? ???. ??? ??? ??????????? ???? Scala Swing project ???? ???? ????? ????."
      ),
      Array(
        "STEP 2",
        "TELL CHATGPT\n\n" +
        "ENVIRONMENT\n" +
        "PROJECT\n" +
        "FEATURES\n" +
        "CONTROLS",
        "??? ChatGPT ?? ?????? ??????? environment ????? code ????? ???? ?? ?????. ???????? project ??? ???, ????? features ??????? ??? controls ????? ???? ?? ?????."
      ),
      Array(
        "STEP 3",
        "WRITE PROMPT\n\n" +
        "ROLE\n" +
        "PROJECT\n" +
        "FEATURES\n" +
        "OUTPUT",
        "??? prompt ???????. ?????? ????? ?? ?? ???? expert Scala Swing developer ???? ??? ??? complete runnable code ??????."
      ),
      Array(
        "CHATGPT",
        "CHATGPT PROMPT\n\n" +
        "Tu maza expert\n" +
        "Scala Swing developer ahes.\n\n" +
        "Mala complete\n" +
        "runnable code dya.\n\n" +
        "Complete imports.\n" +
        "No missing methods.\n" +
        "No undefined variables.",
        "??? ?? prompt ChatGPT ????? type ???. ?????? complete code ????. Missing methods ????? undefined variables ???? ??? ??? ?????? ?????."
      ),
      Array(
        "STEP 5",
        "COPY COMPLETE CODE\n\n" +
        "[ COPY ]\n\n" +
        "Do not copy only\n" +
        "one small part.",
        "ChatGPT ?? code ???????? ????? code copy ???. ???? ?????? ??? copy ??? ???."
      ),
      Array(
        "STEP 6",
        "PASTE IN SCALA\n\n" +
        "[ PASTE CODE ]\n\n" +
        "OLD CODE\n" +
        "    ?\n" +
        "NEW CODE",
        "??? Scala editor ????? ??. ???? code replace ??? ??? ChatGPT ???? copy ?????? ????? code paste ???."
      ),
      Array(
        "STEP 7",
        "SAVE\n\n" +
        "CTRL + S\n\n" +
        "THEN\n\n" +
        "RUN",
        "??? code save ??? ??? Run ???. Window ????? ?? program ???? ???? ???."
      ),
      Array(
        "STEP 8",
        "ERROR?\n\n" +
        "COPY COMPLETE ERROR\n\n" +
        "+ CURRENT CODE\n\n" +
        "+ EXPECTED RESULT",
        "Error ??? ?? ????? ??? ???. Complete compiler error copy ???. Current code ??? ???????? ??? result ?????? ???? ???? ChatGPT ?? ?????."
      ),
      Array(
        "STEP 9",
        "CHATGPT ERROR FIX\n\n" +
        "COMPLETE CORRECTED\n" +
        "REPLACEMENT CODE",
        "??? ChatGPT ?? ????? ?? ???? ?? line fix ??? ???. Complete corrected replacement code ?????? ??? existing features ???? ????."
      ),
      Array(
        "STEP 10",
        "SCALA SWING\n\n" +
        "JFrame\n" +
        "JPanel\n" +
        "JLabel\n" +
        "JButton",
        "??? Scala Swing ?? ????? components ????? ????. JFrame ?????? main window, JPanel ?????? container, JLabel ?????? text ??? JButton ?????? clickable button."
      ),
      Array(
        "STEP 11",
        "EVENTS\n\n" +
        "BUTTON\n" +
        "MOUSE\n" +
        "KEYBOARD\n" +
        "TIMER",
        "Button click ???? ActionListener, mouse ???? mouse events, keyboard ???? key events ??? repeated updates ???? Timer ?????? ????."
      ),
      Array(
        "STEP 12",
        "GAME\n\n" +
        "PLAYER\n" +
        "MOVEMENT\n" +
        "DRAWING\n" +
        "COLLISION",
        "Game ????? player movement, drawing ??? collision ???? ???? ?????. Timer ??? repaint ?????? movement ??? animation ???? ????."
      ),
      Array(
        "STEP 13",
        "SCORE\n\n" +
        "COINS\n" +
        "BONUS\n" +
        "LEVELS",
        "??? score system ???? ???. Coins ???? points ????, level complete ???????? bonus ???? ??? ???? levels ???? ???."
      ),
      Array(
        "STEP 14",
        "PRO FEATURES\n\n" +
        "MENU\n" +
        "PAUSE\n" +
        "SETTINGS\n" +
        "SAVE\n" +
        "HIGH SCORE",
        "Project working ???????? Main Menu, Pause, Settings, Save ??? High Score ????? features add ???."
      ),
      Array(
        "FINAL",
        "IDEA\n" +
        "?\n" +
        "PROMPT\n" +
        "?\n" +
        "COPY\n" +
        "?\n" +
        "PASTE\n" +
        "?\n" +
        "RUN\n" +
        "?\n" +
        "FIX\n" +
        "?\n" +
        "UPGRADE",
        "??? ???! ??? ????? workflow ??????? ????? ???. ??????? idea ???, ChatGPT ???? code ????, copy ???, paste ???, run ???, error fix ??? ??? project ?????? ???? ???."
      )
    )

  var current = 0

  val voices =
    getVoices()

  val voiceNames =
    if (voices.length > 0) {
      voices.take(5)
    } else {
      Array("Default Voice")
    }

  val voiceBox =
    new JComboBox[String](
      voiceNames
    )

  val speed =
    new JSlider(
      -5,
      2,
      -1
    )

  val header =
    new JPanel(
      new BorderLayout()
    )

  header.setBackground(
    new Color(25,25,35)
  )

  val mainTitle =
    new JLabel(
      "AI TEACHER + CHATGPT CLASSROOM",
      SwingConstants.CENTER
    )

  mainTitle.setForeground(
    Color.WHITE
  )

  mainTitle.setFont(
    new Font(
      "SansSerif",
      Font.BOLD,
      27
    )
  )

  val mainSub =
    new JLabel(
      "Marathi Teacher ? English Blackboard ? Automatic Lessons",
      SwingConstants.CENTER
    )

  mainSub.setForeground(
    Color.LIGHT_GRAY
  )

  header.add(
    mainTitle,
    BorderLayout.NORTH
  )

  header.add(
    mainSub,
    BorderLayout.SOUTH
  )

  val boardPanel =
    new JPanel(
      new BorderLayout()
    )

  boardPanel.setBackground(
    new Color(25,35,30)
  )

  boardPanel.setBorder(
    BorderFactory.createLineBorder(
      new Color(115,75,40),
      12
    )
  )

  val boardTitle =
    new JLabel(
      lessons(0)(0),
      SwingConstants.CENTER
    )

  boardTitle.setForeground(
    Color.WHITE
  )

  boardTitle.setFont(
    new Font(
      "SansSerif",
      Font.BOLD,
      24
    )
  )

  boardTitle.setBorder(
    BorderFactory.createEmptyBorder(
      18,
      8,
      10,
      8
    )
  )

  val boardText =
    new JTextArea()

  boardText.setText(
    lessons(0)(1)
  )

  boardText.setEditable(false)
  boardText.setLineWrap(true)
  boardText.setWrapStyleWord(true)
  boardText.setForeground(Color.WHITE)
  boardText.setBackground(
    new Color(25,35,30)
  )

  boardText.setFont(
    new Font(
      "Monospaced",
      Font.BOLD,
      21
    )
  )

  boardText.setBorder(
    BorderFactory.createEmptyBorder(
      20,
      25,
      20,
      25
    )
  )

  boardPanel.add(
    boardTitle,
    BorderLayout.NORTH
  )

  boardPanel.add(
    new JScrollPane(boardText),
    BorderLayout.CENTER
  )

  val teacherPanel =
    new JPanel(
      new BorderLayout()
    )

  teacherPanel.setBackground(
    new Color(238,241,245)
  )

  val teacherHeader =
    new JLabel(
      "AI TEACHER",
      SwingConstants.CENTER
    )

  teacherHeader.setFont(
    new Font(
      "SansSerif",
      Font.BOLD,
      21
    )
  )

  teacherHeader.setBorder(
    BorderFactory.createEmptyBorder(
      10,
      5,
      10,
      5
    )
  )

  val teacherArt =
    new JLabel(
      "<html><center>" +
      "<font size='7'>???</font><br>" +
      "<font size='5'><b>AI Teacher</b></font>" +
      "</center></html>",
      SwingConstants.CENTER
    )

  teacherArt.setBorder(
    BorderFactory.createEmptyBorder(
      10,
      5,
      20,
      5
    )
  )

  val teacherText =
    new JTextArea()

  teacherText.setText(
    lessons(0)(2)
  )

  teacherText.setEditable(false)
  teacherText.setLineWrap(true)
  teacherText.setWrapStyleWord(true)
  teacherText.setFont(
    new Font(
      "SansSerif",
      Font.PLAIN,
      17
    )
  )

  teacherText.setBackground(
    new Color(238,241,245)
  )

  teacherText.setBorder(
    BorderFactory.createEmptyBorder(
      12,
      12,
      12,
      12
    )
  )

  teacherPanel.add(
    teacherHeader,
    BorderLayout.NORTH
  )

  teacherPanel.add(
    teacherArt,
    BorderLayout.CENTER
  )

  teacherPanel.add(
    teacherText,
    BorderLayout.SOUTH
  )

  val chatPanel =
    new JPanel(
      new BorderLayout()
    )

  chatPanel.setBackground(
    Color.WHITE
  )

  val chatTop =
    new JLabel(
      "?  ChatGPT",
      SwingConstants.LEFT
    )

  chatTop.setFont(
    new Font(
      "SansSerif",
      Font.BOLD,
      21
    )
  )

  chatTop.setBorder(
    BorderFactory.createEmptyBorder(
      12,
      16,
      12,
      16
    )
  )

  val chatBox =
    new JTextArea()

  chatBox.setEditable(false)
  chatBox.setLineWrap(true)
  chatBox.setWrapStyleWord(true)
  chatBox.setFont(
    new Font(
      "SansSerif",
      Font.PLAIN,
      16
    )
  )

  chatBox.setText(
    "ChatGPT\n\n" +
    "Teacher ??? ??????? ?? ??? ????? ????.\n\n" +
    "?????? code ??????? ???? ?????? ChatGPT ????? prompt type ???."
  )

  chatBox.setBorder(
    BorderFactory.createEmptyBorder(
      15,
      15,
      15,
      15
    )
  )

  val chatInput =
    new JTextField(
      "Type your prompt here..."
    )

  chatInput.setFont(
    new Font(
      "SansSerif",
      Font.PLAIN,
      15
    )
  )

  chatInput.setBorder(
    BorderFactory.createCompoundBorder(
      BorderFactory.createLineBorder(
        new Color(190,190,190)
      ),
      BorderFactory.createEmptyBorder(
        10,
        10,
        10,
        10
      )
    )
  )

  chatPanel.add(
    chatTop,
    BorderLayout.NORTH
  )

  chatPanel.add(
    new JScrollPane(chatBox),
    BorderLayout.CENTER
  )

  chatPanel.add(
    chatInput,
    BorderLayout.SOUTH
  )

  val center =
    new JPanel(
      new GridLayout(
        1,
        3,
        8,
        8
      )
    )

  center.setBackground(
    Color.WHITE
  )

  center.add(
    boardPanel
  )

  center.add(
    teacherPanel
  )

  center.add(
    chatPanel
  )

  def refresh(): Unit = {

    boardTitle.setText(
      lessons(current)(0)
    )

    boardText.setText(
      lessons(current)(1)
    )

    teacherText.setText(
      lessons(current)(2)
    )

    chatBox.setText(
      "ChatGPT\n\n" +
      lessons(current)(2) +
      "\n\nTeacher ??????:\n" +
      "??? step ??????? ChatGPT ????? prompt ?????."
    )
  }

  def teachCurrent(): Unit = {

    stopVoice()

    autoTeaching = true
    stopRequested = false

    val voice =
      voiceBox.getSelectedItem match {
        case null => ""
        case x => x.toString
      }

    val rate =
      speed.getValue

    val speech =
      lessons(current)(2)

    val worker =
      new Thread(
        new Runnable {

          override def run(): Unit = {

            val ok =
              speak(
                speech,
                voice,
                rate
              )

            if (
              ok &&
              autoTeaching
            ) {

              SwingUtilities.invokeLater(
                new Runnable {

                  override def run(): Unit = {

                    if (
                      current <
                      lessons.length - 1
                    ) {

                      current += 1

                      refresh()

                      teachCurrent()

                    } else {

                      autoTeaching = false

                      JOptionPane.showMessageDialog(
                        frame,
                        "AI Teacher lesson complete!"
                      )
                    }
                  }
                }
              )
            }
          }
        }
      )

    worker.start()
  }

  val play =
    makeButton(
      "? PLAY"
    )

  val stop =
    makeButton(
      "? STOP"
    )

  val replay =
    makeButton(
      "? REPLAY"
    )

  val next =
    makeButton(
      "NEXT"
    )

  val back =
    makeButton(
      "BACK"
    )

  val controls =
    new JPanel(
      new FlowLayout()
    )

  controls.setBackground(
    Color.WHITE
  )

  controls.add(
    play
  )

  controls.add(
    stop
  )

  controls.add(
    replay
  )

  controls.add(
    back
  )

  controls.add(
    next
  )

  val voiceControls =
    new JPanel(
      new FlowLayout()
    )

  voiceControls.setBackground(
    Color.WHITE
  )

  voiceControls.add(
    new JLabel(
      "Teacher Voice:"
    )
  )

  voiceControls.add(
    voiceBox
  )

  voiceControls.add(
    new JLabel(
      "Speed:"
    )
  )

  speed.setPreferredSize(
    new Dimension(
      150,
      30
    )
  )

  voiceControls.add(
    speed
  )

  val testVoice =
    makeButton(
      "TEST VOICE"
    )

  voiceControls.add(
    testVoice
  )

  val bottom =
    new JPanel(
      new BorderLayout()
    )

  bottom.setBackground(
    Color.WHITE
  )

  bottom.add(
    voiceControls,
    BorderLayout.NORTH
  )

  bottom.add(
    controls,
    BorderLayout.SOUTH
  )

  play.addActionListener(
    new ActionListener {
      override def actionPerformed(
          e: ActionEvent
      ): Unit = {

        if (!autoTeaching) {
          teachCurrent()
        }
      }
    }
  )

  stop.addActionListener(
    new ActionListener {
      override def actionPerformed(
          e: ActionEvent
      ): Unit = {

        autoTeaching = false
        stopVoice()
      }
    }
  )

  replay.addActionListener(
    new ActionListener {
      override def actionPerformed(
          e: ActionEvent
      ): Unit = {

        teachCurrent()
      }
    }
  )

  next.addActionListener(
    new ActionListener {
      override def actionPerformed(
          e: ActionEvent
      ): Unit = {

        autoTeaching = false
        stopVoice()

        if (
          current <
          lessons.length - 1
        ) {

          current += 1
          refresh()
        }
      }
    }
  )

  back.addActionListener(
    new ActionListener {
      override def actionPerformed(
          e: ActionEvent
      ): Unit = {

        autoTeaching = false
        stopVoice()

        if (current > 0) {

          current -= 1
          refresh()
        }
      }
    }
  )

  testVoice.addActionListener(
    new ActionListener {
      override def actionPerformed(
          e: ActionEvent
      ): Unit = {

        stopVoice()

        val voice =
          voiceBox.getSelectedItem match {
            case null => ""
            case x => x.toString
          }

        val rate =
          speed.getValue

        val test =
          "???????! ?? ????? AI Teacher ???. ???, Scala Swing ??????? ??????? ?????."

        val t =
          new Thread(
            new Runnable {
              override def run(): Unit = {
                speak(
                  test,
                  voice,
                  rate
                )
              }
            }
          )

        t.start()
      }
    }
  )

  val main =
    new JPanel(
      new BorderLayout()
    )

  main.add(
    header,
    BorderLayout.NORTH
  )

  main.add(
    center,
    BorderLayout.CENTER
  )

  main.add(
    bottom,
    BorderLayout.SOUTH
  )

  frame.setContentPane(
    main
  )

  frame.setVisible(true)

  refresh()

  val starter =
    new Thread(
      new Runnable {

        override def run(): Unit = {

          try {
            Thread.sleep(900)
          } catch {
            case _: Throwable =>
          }

          SwingUtilities.invokeLater(
            new Runnable {

              override def run(): Unit = {
                teachCurrent()
              }
            }
          )
        }
      }
    )

  starter.start()
}

val login =
  new JFrame(
    "Private AI Teacher"
  )

login.setSize(
  460,
  285
)

login.setDefaultCloseOperation(
  WindowConstants.EXIT_ON_CLOSE
)

login.setLocationRelativeTo(null)

val loginPanel =
  new JPanel(
    new GridBagLayout()
  )

loginPanel.setBackground(
  Color.WHITE
)

val g =
  new GridBagConstraints()

g.insets =
  new Insets(
    9,
    9,
    9,
    9
  )

g.fill =
  GridBagConstraints.HORIZONTAL

val loginTitle =
  new JLabel(
    "PRIVATE AI TEACHER",
    SwingConstants.CENTER
  )

loginTitle.setFont(
  new Font(
    "SansSerif",
    Font.BOLD,
    27
  )
)

val loginInfo =
  new JLabel(
    "Enter access code",
    SwingConstants.CENTER
  )

val pass =
  new JPasswordField()

pass.setPreferredSize(
  new Dimension(
    290,
    36
  )
)

val unlock =
  makeButton(
    "UNLOCK"
  )

val status =
  new JLabel(
    " ",
    SwingConstants.CENTER
  )

g.gridx = 0
g.gridy = 0
g.gridwidth = 2

loginPanel.add(
  loginTitle,
  g
)

g.gridy = 1

loginPanel.add(
  loginInfo,
  g
)

g.gridy = 2

loginPanel.add(
  pass,
  g
)

g.gridy = 3
g.gridwidth = 1
g.gridx = 0

loginPanel.add(
  unlock,
  g
)

g.gridx = 1

loginPanel.add(
  status,
  g
)

login.setContentPane(
  loginPanel
)

def checkPassword(): Unit = {

  val entered =
    new String(
      pass.getPassword
    )

  if (
    entered == secret
  ) {

    login.dispose()

    openTeacher()

  } else {

    status.setText(
      "ACCESS DENIED"
    )

    pass.setText("")
  }
}

unlock.addActionListener(
  new ActionListener {

    override def actionPerformed(
        e: ActionEvent
    ): Unit = {

      checkPassword()
    }
  }
)

pass.addActionListener(
  new ActionListener {

    override def actionPerformed(
        e: ActionEvent
    ): Unit = {

      checkPassword()
    }
  }
)

login.setVisible(true)

pass.requestFocusInWindow()