Code Sketch
ai teqnlogia yoiiiii
Category: Programming
import javax.swing._
import javax.swing.border._
import java.awt._
import java.awt.event._
import java.awt.image.BufferedImage
import java.awt.datatransfer._
import javax.imageio.ImageIO
import java.io._
// ============================================================
// SCALA SWING MEGA LEARNING STUDIO
// CORRECTED VERSION
// ============================================================
val passwordSecret =
Array(121,97,100,110,101,115,104,50,48,49,51)
.map(_.toChar)
.mkString
var speechProcess: Process = null
var savedImage: BufferedImage = null
val BG = new Color(18, 20, 28)
val PANEL = new Color(28, 31, 42)
val PANEL2 = new Color(38, 42, 56)
val TEXT = new Color(240, 243, 250)
val SUBTEXT = new Color(175, 182, 198)
val ACCENT = new Color(90, 150, 255)
val GREEN = new Color(70, 210, 145)
val RED = new Color(255, 90, 100)
val YELLOW = new Color(255, 200, 70)
// ============================================================
// COMMON HELPERS
// ============================================================
def makeButton(t: String): JButton = {
val b = new JButton(t)
b.setFocusPainted(false)
b.setForeground(TEXT)
b.setBackground(PANEL2)
b.setFont(new Font("Segoe UI", Font.BOLD, 13))
b.setBorder(
new CompoundBorder(
new LineBorder(new Color(65, 70, 90)),
new EmptyBorder(8, 14, 8, 14)
)
)
b
}
def makeLabel(t: String, size: Int, bold: Boolean): JLabel = {
val l = new JLabel(t)
l.setForeground(TEXT)
l.setFont(
new Font(
"Segoe UI",
if (bold) Font.BOLD else Font.PLAIN,
size
)
)
l
}
def makeArea(): JTextArea = {
val a = new JTextArea()
a.setBackground(new Color(12, 15, 21))
a.setForeground(TEXT)
a.setCaretColor(TEXT)
a.setFont(new Font("Segoe UI", Font.PLAIN, 16))
a.setLineWrap(true)
a.setWrapStyleWord(true)
a.setBorder(new EmptyBorder(12, 12, 12, 12))
a
}
def scroll(c: Component): JScrollPane = {
val s = new JScrollPane(c)
s.setBorder(null)
s.getVerticalScrollBar.setUnitIncrement(14)
s
}
def info(title: String, msg: String): Unit = {
JOptionPane.showMessageDialog(
null,
msg,
title,
JOptionPane.INFORMATION_MESSAGE
)
}
// ============================================================
// CORRECTED CLIPBOARD TEXT FUNCTION
// ============================================================
def copyText(text: String): Unit = {
try {
val clipboard =
Toolkit.getDefaultToolkit.getSystemClipboard
val transferable =
new StringSelection(text)
clipboard.setContents(
transferable,
null
)
} catch {
case _: Throwable =>
info("Clipboard", "Text copy failed.")
}
}
def pasteText(): String = {
try {
val clipboard =
Toolkit.getDefaultToolkit.getSystemClipboard
val transferable =
clipboard.getContents(null)
if (
transferable != null &&
transferable.isDataFlavorSupported(
DataFlavor.stringFlavor
)
) {
transferable
.getTransferData(DataFlavor.stringFlavor)
.toString
} else {
""
}
} catch {
case _: Throwable =>
""
}
}
// ============================================================
// CORRECTED CLIPBOARD IMAGE FUNCTION
// ============================================================
def copyImage(): Unit = {
try {
if (savedImage == null) {
info("Image", "There is no image to copy.")
} else {
val clipboard =
Toolkit.getDefaultToolkit.getSystemClipboard
val transferable =
new Transferable {
def getTransferDataFlavors:
Array[DataFlavor] =
Array(DataFlavor.imageFlavor)
def isDataFlavorSupported(
flavor: DataFlavor
): Boolean =
flavor == DataFlavor.imageFlavor
def getTransferData(
flavor: DataFlavor
): Object = {
if (
flavor == DataFlavor.imageFlavor
) {
savedImage
} else {
throw new UnsupportedFlavorException(flavor)
}
}
}
clipboard.setContents(
transferable,
null
)
}
} catch {
case _: Throwable =>
info("Image", "Image copy failed.")
}
}
def pasteImage(): Boolean = {
try {
val clipboard =
Toolkit.getDefaultToolkit.getSystemClipboard
val transferable =
clipboard.getContents(null)
if (
transferable != null &&
transferable.isDataFlavorSupported(
DataFlavor.imageFlavor
)
) {
savedImage =
transferable
.getTransferData(
DataFlavor.imageFlavor
)
.asInstanceOf[BufferedImage]
true
} else {
false
}
} catch {
case _: Throwable =>
false
}
}
// ============================================================
// FILE FUNCTIONS
// ============================================================
def saveText(text: String, defaultName: String): Unit = {
val chooser = new JFileChooser()
chooser.setSelectedFile(
new File(defaultName)
)
if (
chooser.showSaveDialog(null) ==
JFileChooser.APPROVE_OPTION
) {
val writer =
new FileWriter(
chooser.getSelectedFile
)
try {
writer.write(text)
} finally {
writer.close()
}
}
}
def loadText(area: JTextArea): Unit = {
val chooser = new JFileChooser()
if (
chooser.showOpenDialog(null) ==
JFileChooser.APPROVE_OPTION
) {
val source =
scala.io.Source.fromFile(
chooser.getSelectedFile,
"UTF-8"
)
try {
area.setText(source.mkString)
} finally {
source.close()
}
}
}
// ============================================================
// SPEECH
// ============================================================
def stopSpeech(): Unit = {
try {
if (speechProcess != null) {
speechProcess.destroy()
speechProcess = null
}
} catch {
case _: Throwable =>
}
}
def speak(text: String): Unit = {
stopSpeech()
try {
val safeText =
text
.replace("'", "''")
.replace("\n", " ")
val command =
"Add-Type -AssemblyName System.Speech; " +
"$s=New-Object System.Speech.Synthesis.SpeechSynthesizer; " +
"$s.Speak('" + safeText + "');"
speechProcess =
new ProcessBuilder(
"powershell",
"-NoProfile",
"-Command",
command
).start()
} catch {
case _: Throwable =>
}
}
// ============================================================
// TEACHER
// ============================================================
def teacherAnswer(q0: String): String = {
val q =
q0.toLowerCase.trim
if (q.contains("button")) {
"JButton ?????? clickable button ???? ???? ????. " +
"addActionListener ????? click ???????? ??????? code ????."
} else if (q.contains("jframe")) {
"JFrame ?? ????? window ???. " +
"Scala ????? WindowConstants.EXIT_ON_CLOSE ?????? ????? ???."
} else if (q.contains("jpanel")) {
"JPanel ?? components ??????????? container ?????? ?????? ????."
} else if (q.contains("label")) {
"JLabel ????????? title ????? ?????? ???????????? ???????."
} else if (q.contains("textarea")) {
"JTextArea ???? text ????? code editor ??????????? ?????? ???."
} else if (q.contains("textfield")) {
"JTextField ????? single-line user input ????? ????."
} else if (q.contains("mouse")) {
"MouseAdapter ?????? mouse click ????? mouse movement handle ???? ????."
} else if (q.contains("keyboard")) {
"KeyAdapter ?????? keyboard events handle ???? ?????."
} else if (q.contains("timer")) {
"javax.swing.Timer animation ??? repeated UI updates ???? ?????? ????."
} else if (
q.contains("error") ||
q.contains("compile")
) {
"Compiler error ????? line number ???. " +
"???? line ????????? ??????? lines ???? brackets, commas ??? syntax ?????? ?????."
} else if (q.contains("clipboard")) {
"Clipboard ???? data ?????????? getContents(null) ?????? Transferable ????? ??? ?????? isDataFlavorSupported ?????."
} else if (q.contains("image")) {
"BufferedImage image memory ????? ?????. ImageIO ?????? PNG ????? JPG save ???? ????."
} else if (q.contains("game")) {
"Game ???? state, input, timer, collision, score ??? repaint ?? ????? ??? ????."
} else {
"?? ?????? ?????? offline teacher library ????? ?????? ????. " +
"??????: JButton click ??? ???????"
}
}
// ============================================================
// LESSONS
// ============================================================
val lessonTitles = Array(
"Scala Swing Basics",
"JFrame",
"JPanel",
"JLabel",
"JButton",
"JTextField",
"JTextArea",
"Layouts",
"Mouse Events",
"Keyboard Events",
"Timers",
"Colors",
"Fonts",
"Menus",
"Dialogs",
"Files",
"Clipboard",
"Images",
"Animation",
"Mini Games"
)
val lessonMarathi = Array(
"?? ??? Scala Swing ?? basic ???? ?????? ????.",
"JFrame ?????? application ?? ????? window ????.",
"JPanel ????? ??? ???????? components ?????.",
"JLabel ?????? screen ?? text ?????? ????.",
"JButton ?? click ???????? action ?????? ????.",
"JTextField ????? user ???? input ???? ????.",
"JTextArea ???? text ????? code ???????????? ?????? ???.",
"Layout manager components ????????? arrange ????.",
"Mouse events ?????? click ??? movement handle ???? ?????.",
"Keyboard events ???? controls ???? ???? ?????.",
"Timer ?????? animation ??? countdown ???? ???? ?????.",
"Colors ???? interface ????? ????? ????.",
"Fonts ?????? text ?? style ????? ????.",
"Menus ????? ???? commands ????????? ????? ?????.",
"Dialog box ?????? user ?? ???? messages ??? inputs ???? ?????.",
"Files ?????? code ??? notes save ???? ?????.",
"Clipboard ?????? text ??? image copy paste ???? ?????.",
"BufferedImage ?????? image ???? ??? save ???? ????.",
"Timer ??? repaint ?????? moving animation ???? ????.",
"?? concepts ????? ???? ??? games ???? ????."
)
var currentLesson = 0
var autoNext = true
// ============================================================
// MAIN APPLICATION
// ============================================================
def openStudio(): Unit = {
val frame =
new JFrame(
"Scala Swing AI Learning Studio"
)
frame.setSize(1400, 850)
frame.setLocationRelativeTo(null)
frame.setDefaultCloseOperation(
WindowConstants.EXIT_ON_CLOSE
)
val root =
new JPanel(new BorderLayout())
root.setBackground(BG)
// ----------------------------------------------------------
// TOP BAR
// ----------------------------------------------------------
val top =
new JPanel(new BorderLayout())
top.setBackground(
new Color(10, 12, 18)
)
top.setBorder(
new EmptyBorder(12, 18, 12, 18)
)
val heading =
makeLabel(
"? SCALA SWING AI STUDIO",
23,
true
)
val topButtons =
new JPanel(
new FlowLayout(
FlowLayout.RIGHT,
8,
0
)
)
topButtons.setOpaque(false)
val stopButton =
makeButton("STOP VOICE")
stopButton.addActionListener(
new ActionListener {
def actionPerformed(
e: ActionEvent
): Unit = {
stopSpeech()
}
}
)
val about =
makeButton("ABOUT")
about.addActionListener(
new ActionListener {
def actionPerformed(
e: ActionEvent
): Unit = {
info(
"About",
"Scala Swing AI Learning Studio\n\n" +
"Learning ? Code ? Errors ? Images ? Quiz ? Projects"
)
}
}
)
topButtons.add(stopButton)
topButtons.add(about)
top.add(
heading,
BorderLayout.WEST
)
top.add(
topButtons,
BorderLayout.EAST
)
root.add(
top,
BorderLayout.NORTH
)
// ----------------------------------------------------------
// TABS
// ----------------------------------------------------------
val tabs =
new JTabbedPane()
// ==========================================================
// CLASSROOM TAB
// ==========================================================
val classroom =
new JPanel(
new BorderLayout(10, 10)
)
classroom.setBackground(BG)
classroom.setBorder(
new EmptyBorder(12, 12, 12, 12)
)
val board =
new JTextArea()
board.setEditable(false)
board.setBackground(
new Color(20, 50, 38)
)
board.setForeground(
new Color(240, 255, 220)
)
board.setFont(
new Font(
"Consolas",
Font.BOLD,
21
)
)
board.setBorder(
new CompoundBorder(
new LineBorder(
new Color(135, 95, 50),
8
),
new EmptyBorder(
15,
15,
15,
15
)
)
)
val teacher =
makeLabel(
"???",
90,
false
)
teacher.setHorizontalAlignment(
SwingConstants.CENTER
)
val speech =
makeArea()
speech.setRows(7)
speech.setFont(
new Font(
"Segoe UI",
Font.PLAIN,
18
)
)
speech.setEditable(false)
val english =
makeArea()
english.setRows(9)
english.setEditable(false)
english.setFont(
new Font(
"Consolas",
Font.PLAIN,
16
)
)
val teacherPanel =
new JPanel(
new BorderLayout(8, 8)
)
teacherPanel.setBackground(PANEL)
teacherPanel.setBorder(
new EmptyBorder(12, 12, 12, 12)
)
teacherPanel.add(
teacher,
BorderLayout.NORTH
)
teacherPanel.add(
scroll(speech),
BorderLayout.CENTER
)
teacherPanel.add(
scroll(english),
BorderLayout.SOUTH
)
val lessonControls =
new JPanel(
new FlowLayout(
FlowLayout.CENTER,
8,
8
)
)
lessonControls.setBackground(BG)
val back =
makeButton("? BACK")
val play =
makeButton("? PLAY")
val replay =
makeButton("? REPLAY")
val next =
makeButton("NEXT ?")
val auto =
new JCheckBox("AUTO NEXT")
auto.setSelected(true)
auto.setBackground(BG)
auto.setForeground(TEXT)
val progress =
makeLabel(
"",
14,
true
)
lessonControls.add(back)
lessonControls.add(play)
lessonControls.add(replay)
lessonControls.add(next)
lessonControls.add(auto)
lessonControls.add(progress)
classroom.add(
board,
BorderLayout.NORTH
)
classroom.add(
teacherPanel,
BorderLayout.CENTER
)
classroom.add(
lessonControls,
BorderLayout.SOUTH
)
def refreshLesson(): Unit = {
progress.setText(
"LESSON " +
(currentLesson + 1) +
" / " +
lessonTitles.length
)
board.setText(
"SCALA SWING\n\n" +
lessonTitles(currentLesson)
)
speech.setText(
lessonMarathi(currentLesson)
)
english.setText(
"ENGLISH ACTIONS\n\n" +
"OPEN CHATGPT\n\n" +
"READ THE QUESTION\n\n" +
"COPY COMPLETE CODE\n\n" +
"PASTE INTO SCALA EDITOR\n\n" +
"SAVE\n\n" +
"RUN\n\n" +
"CHECK RESULT"
)
}
def playLesson(): Unit = {
refreshLesson()
val text =
lessonMarathi(currentLesson)
speak(text)
if (
auto.isSelected &&
currentLesson <
lessonTitles.length - 1
) {
val lessonAtStart =
currentLesson
new Thread(
new Runnable {
def run(): Unit = {
try {
Thread.sleep(4000)
} catch {
case _: Throwable =>
}
SwingUtilities.invokeLater(
new Runnable {
def run(): Unit = {
if (
auto.isSelected &&
currentLesson ==
lessonAtStart
) {
currentLesson += 1
refreshLesson()
}
}
}
)
}
}
).start()
}
}
back.addActionListener(
new ActionListener {
def actionPerformed(
e: ActionEvent
): Unit = {
if (currentLesson > 0)
currentLesson -= 1
refreshLesson()
}
}
)
next.addActionListener(
new ActionListener {
def actionPerformed(
e: ActionEvent
): Unit = {
if (
currentLesson <
lessonTitles.length - 1
)
currentLesson += 1
refreshLesson()
}
}
)
play.addActionListener(
new ActionListener {
def actionPerformed(
e: ActionEvent
): Unit = {
playLesson()
}
}
)
replay.addActionListener(
new ActionListener {
def actionPerformed(
e: ActionEvent
): Unit = {
playLesson()
}
}
)
refreshLesson()
tabs.addTab(
"? CLASSROOM",
classroom
)
// ==========================================================
// ASK TEACHER
// ==========================================================
val ask =
new JPanel(
new BorderLayout(10, 10)
)
ask.setBackground(BG)
ask.setBorder(
new EmptyBorder(12, 12, 12, 12)
)
val question =
makeArea()
question.setRows(7)
val answer =
makeArea()
answer.setRows(10)
answer.setEditable(false)
val askButtons =
new JPanel(
new FlowLayout(
FlowLayout.LEFT
)
)
askButtons.setBackground(BG)
val askNow =
makeButton("ASK")
val sample =
makeButton("SAMPLE")
val copyAns =
makeButton("COPY ANSWER")
val clearAsk =
makeButton("CLEAR")
askButtons.add(askNow)
askButtons.add(sample)
askButtons.add(copyAns)
askButtons.add(clearAsk)
val askCenter =
new JPanel(
new GridLayout(2, 1, 8, 8)
)
askCenter.setBackground(BG)
val qPanel =
new JPanel(
new BorderLayout()
)
qPanel.setBackground(PANEL)
qPanel.add(
makeLabel(
"YOUR QUESTION",
15,
true
),
BorderLayout.NORTH
)
qPanel.add(
scroll(question),
BorderLayout.CENTER
)
val aPanel =
new JPanel(
new BorderLayout()
)
aPanel.setBackground(PANEL)
aPanel.add(
makeLabel(
"TEACHER ANSWER",
15,
true
),
BorderLayout.NORTH
)
aPanel.add(
scroll(answer),
BorderLayout.CENTER
)
askCenter.add(qPanel)
askCenter.add(aPanel)
ask.add(
makeLabel(
"? ASK THE TEACHER",
24,
true
),
BorderLayout.NORTH
)
ask.add(
askCenter,
BorderLayout.CENTER
)
ask.add(
askButtons,
BorderLayout.SOUTH
)
askNow.addActionListener(
new ActionListener {
def actionPerformed(
e: ActionEvent
): Unit = {
val a =
teacherAnswer(
question.getText
)
answer.setText(a)
speak(a)
}
}
)
sample.addActionListener(
new ActionListener {
def actionPerformed(
e: ActionEvent
): Unit = {
question.setText(
"Scala Swing ????? JButton click event ??? ???????"
)
}
}
)
copyAns.addActionListener(
new ActionListener {
def actionPerformed(
e: ActionEvent
): Unit = {
copyText(answer.getText)
}
}
)
clearAsk.addActionListener(
new ActionListener {
def actionPerformed(
e: ActionEvent
): Unit = {
question.setText("")
answer.setText("")
}
}
)
tabs.addTab(
"? ASK TEACHER",
ask
)
// ==========================================================
// CODE LAB
// ==========================================================
val code =
new JPanel(
new BorderLayout(8, 8)
)
code.setBackground(BG)
code.setBorder(
new EmptyBorder(12, 12, 12, 12)
)
val editor =
makeArea()
editor.setFont(
new Font(
"Consolas",
Font.PLAIN,
16
)
)
val codeButtons =
new JPanel(
new FlowLayout(
FlowLayout.LEFT
)
)
codeButtons.setBackground(BG)
val copyCode =
makeButton("COPY")
val pasteCode =
makeButton("PASTE")
val loadCode =
makeButton("LOAD")
val saveCode =
makeButton("SAVE")
val clearCode =
makeButton("CLEAR")
codeButtons.add(copyCode)
codeButtons.add(pasteCode)
codeButtons.add(loadCode)
codeButtons.add(saveCode)
codeButtons.add(clearCode)
code.add(
makeLabel(
"? CODE LAB",
24,
true
),
BorderLayout.NORTH
)
code.add(
scroll(editor),
BorderLayout.CENTER
)
code.add(
codeButtons,
BorderLayout.SOUTH
)
copyCode.addActionListener(
new ActionListener {
def actionPerformed(
e: ActionEvent
): Unit = {
copyText(editor.getText)
}
}
)
pasteCode.addActionListener(
new ActionListener {
def actionPerformed(
e: ActionEvent
): Unit = {
val t =
pasteText()
if (t.nonEmpty)
editor.insert(
t,
editor.getCaretPosition
)
}
}
)
loadCode.addActionListener(
new ActionListener {
def actionPerformed(
e: ActionEvent
): Unit = {
loadText(editor)
}
}
)
saveCode.addActionListener(
new ActionListener {
def actionPerformed(
e: ActionEvent
): Unit = {
saveText(
editor.getText,
"ScalaCode.scala"
)
}
}
)
clearCode.addActionListener(
new ActionListener {
def actionPerformed(
e: ActionEvent
): Unit = {
editor.setText("")
}
}
)
tabs.addTab(
"? CODE LAB",
code
)
// ==========================================================
// ERROR FIX
// ==========================================================
val errorPanel =
new JPanel(
new GridLayout(2, 1, 8, 8)
)
errorPanel.setBackground(BG)
errorPanel.setBorder(
new EmptyBorder(12, 12, 12, 12)
)
val errorInput =
makeArea()
val errorOutput =
makeArea()
errorOutput.setEditable(false)
val errorTop =
new JPanel(
new BorderLayout()
)
errorTop.setBackground(BG)
val analyze =
makeButton(
"ANALYZE ERROR"
)
errorTop.add(
makeLabel(
"? ERROR INPUT",
19,
true
),
BorderLayout.WEST
)
errorTop.add(
analyze,
BorderLayout.EAST
)
val errorBox1 =
new JPanel(
new BorderLayout()
)
errorBox1.setBackground(PANEL)
errorBox1.add(
errorTop,
BorderLayout.NORTH
)
errorBox1.add(
scroll(errorInput),
BorderLayout.CENTER
)
val errorBox2 =
new JPanel(
new BorderLayout()
)
errorBox2.setBackground(PANEL)
errorBox2.add(
makeLabel(
"TEACHER FIX",
19,
true
),
BorderLayout.NORTH
)
errorBox2.add(
scroll(errorOutput),
BorderLayout.CENTER
)
errorPanel.removeAll()
errorPanel.add(errorBox1)
errorPanel.add(errorBox2)
analyze.addActionListener(
new ActionListener {
def actionPerformed(
e: ActionEvent
): Unit = {
val x =
errorInput.getText.toLowerCase
val result =
if (
x.contains(
"illegal start of declaration"
)
) {
"Possible cause:\n\n" +
"1. Code is outside the correct block.\n" +
"2. Missing { or }.\n" +
"3. Missing =.\n" +
"4. Previous line has syntax error."
} else if (
x.contains(
"exit_on_close"
)
) {
"Use:\n\n" +
"WindowConstants.EXIT_ON_CLOSE\n\n" +
"instead of JFrame.EXIT_ON_CLOSE."
} else if (
x.contains(
"forward reference"
)
) {
"A value is being used before safe initialization.\n\n" +
"Move declarations above the code that uses them."
} else if (
x.contains(
"not found"
)
) {
"Check imports, spelling, declaration order and scope."
} else {
"Paste the complete error with its line number.\n\n" +
"Also check the lines immediately before the reported line."
}
errorOutput.setText(result)
speak(result.replace("\n", " "))
}
}
)
tabs.addTab(
"? ERROR FIX",
errorPanel
)
// ==========================================================
// IMAGE STUDIO
// ==========================================================
val image =
new JPanel(
new BorderLayout(8, 8)
)
image.setBackground(BG)
image.setBorder(
new EmptyBorder(12, 12, 12, 12)
)
val imageLabel =
new JLabel(
"IMAGE PREVIEW",
SwingConstants.CENTER
)
imageLabel.setForeground(SUBTEXT)
imageLabel.setFont(
new Font(
"Segoe UI",
Font.BOLD,
22
)
)
imageLabel.setOpaque(true)
imageLabel.setBackground(
new Color(10, 12, 18)
)
val imageButtons =
new JPanel(
new FlowLayout(
FlowLayout.LEFT
)
)
imageButtons.setBackground(BG)
val generate =
makeButton("GENERATE")
val upload =
makeButton("UPLOAD")
val pasteImg =
makeButton("PASTE IMAGE")
val copyImg =
makeButton("COPY IMAGE")
val saveImg =
makeButton("SAVE IMAGE")
val clearImg =
makeButton("CLEAR")
imageButtons.add(generate)
imageButtons.add(upload)
imageButtons.add(pasteImg)
imageButtons.add(copyImg)
imageButtons.add(saveImg)
imageButtons.add(clearImg)
image.add(
makeLabel(
"? IMAGE STUDIO",
24,
true
),
BorderLayout.NORTH
)
image.add(
imageLabel,
BorderLayout.CENTER
)
image.add(
imageButtons,
BorderLayout.SOUTH
)
generate.addActionListener(
new ActionListener {
def actionPerformed(
e: ActionEvent
): Unit = {
val img =
new BufferedImage(
800,
500,
BufferedImage.TYPE_INT_ARGB
)
val g =
img.createGraphics()
g.setColor(
new Color(
20,
30,
80
)
)
g.fillRect(
0,
0,
800,
500
)
g.setColor(
new Color(
255,
220,
80
)
)
g.fillOval(
300,
80,
200,
200
)
g.setColor(TEXT)
g.setFont(
new Font(
"Segoe UI",
Font.BOLD,
35
)
)
g.drawString(
"IMAGE STUDIO",
250,
380
)
g.dispose()
savedImage = img
val scaled =
img.getScaledInstance(
760,
470,
Image.SCALE_SMOOTH
)
imageLabel.setIcon(
new ImageIcon(scaled)
)
imageLabel.setText("")
}
}
)
upload.addActionListener(
new ActionListener {
def actionPerformed(
e: ActionEvent
): Unit = {
val chooser =
new JFileChooser()
if (
chooser.showOpenDialog(null) ==
JFileChooser.APPROVE_OPTION
) {
try {
savedImage =
ImageIO.read(
chooser.getSelectedFile
)
if (savedImage != null) {
val scaled =
savedImage.getScaledInstance(
760,
470,
Image.SCALE_SMOOTH
)
imageLabel.setIcon(
new ImageIcon(scaled)
)
imageLabel.setText("")
} else {
info(
"Image",
"Invalid image file."
)
}
} catch {
case _: Throwable =>
info(
"Image",
"Image loading failed."
)
}
}
}
}
)
pasteImg.addActionListener(
new ActionListener {
def actionPerformed(
e: ActionEvent
): Unit = {
if (pasteImage()) {
val scaled =
savedImage.getScaledInstance(
760,
470,
Image.SCALE_SMOOTH
)
imageLabel.setIcon(
new ImageIcon(scaled)
)
imageLabel.setText("")
} else {
info(
"Clipboard",
"Clipboard ????? image ?????? ????."
)
}
}
}
)
copyImg.addActionListener(
new ActionListener {
def actionPerformed(
e: ActionEvent
): Unit = {
copyImage()
}
}
)
saveImg.addActionListener(
new ActionListener {
def actionPerformed(
e: ActionEvent
): Unit = {
if (savedImage == null) {
info(
"Image",
"Save ?????????? image ????."
)
} else {
val chooser =
new JFileChooser()
chooser.setSelectedFile(
new File("MyImage.png")
)
if (
chooser.showSaveDialog(null) ==
JFileChooser.APPROVE_OPTION
) {
try {
ImageIO.write(
savedImage,
"png",
chooser.getSelectedFile
)
} catch {
case _: Throwable =>
info(
"Image",
"Image save failed."
)
}
}
}
}
}
)
clearImg.addActionListener(
new ActionListener {
def actionPerformed(
e: ActionEvent
): Unit = {
savedImage = null
imageLabel.setIcon(null)
imageLabel.setText(
"IMAGE PREVIEW"
)
}
}
)
tabs.addTab(
"? IMAGE STUDIO",
image
)
// ==========================================================
// NOTES
// ==========================================================
val notesPanel =
new JPanel(
new BorderLayout(8, 8)
)
notesPanel.setBackground(BG)
notesPanel.setBorder(
new EmptyBorder(12, 12, 12, 12)
)
val notes =
makeArea()
val noteButtons =
new JPanel(
new FlowLayout(
FlowLayout.LEFT
)
)
noteButtons.setBackground(BG)
val newNote =
makeButton("NEW")
val saveNote =
makeButton("SAVE")
val loadNote =
makeButton("LOAD")
val copyNote =
makeButton("COPY")
val pasteNote =
makeButton("PASTE")
noteButtons.add(newNote)
noteButtons.add(saveNote)
noteButtons.add(loadNote)
noteButtons.add(copyNote)
noteButtons.add(pasteNote)
notesPanel.add(
makeLabel(
"? NOTES",
24,
true
),
BorderLayout.NORTH
)
notesPanel.add(
scroll(notes),
BorderLayout.CENTER
)
notesPanel.add(
noteButtons,
BorderLayout.SOUTH
)
newNote.addActionListener(
new ActionListener {
def actionPerformed(
e: ActionEvent
): Unit = notes.setText("")
}
)
saveNote.addActionListener(
new ActionListener {
def actionPerformed(
e: ActionEvent
): Unit =
saveText(
notes.getText,
"MyNotes.txt"
)
}
)
loadNote.addActionListener(
new ActionListener {
def actionPerformed(
e: ActionEvent
): Unit =
loadText(notes)
}
)
copyNote.addActionListener(
new ActionListener {
def actionPerformed(
e: ActionEvent
): Unit =
copyText(notes.getText)
}
)
pasteNote.addActionListener(
new ActionListener {
def actionPerformed(
e: ActionEvent
): Unit = {
val t =
pasteText()
if (t.nonEmpty)
notes.insert(
t,
notes.getCaretPosition
)
}
}
)
tabs.addTab(
"? NOTES",
notesPanel
)
// ==========================================================
// FEATURE LIST
// ==========================================================
val features = Array(
"Dashboard",
"AI Classroom",
"Marathi Teacher",
"Voice Teacher",
"Auto Next",
"Replay Lesson",
"Ask Teacher",
"Code Lab",
"Copy Code",
"Paste Code",
"Save Code",
"Load Code",
"Error Fix",
"Error Analysis",
"Notes",
"Save Notes",
"Load Notes",
"Copy Notes",
"Paste Notes",
"Image Studio",
"Generate Image",
"Upload Image",
"Paste Image",
"Copy Image",
"Save Image",
"Clear Image",
"Quiz",
"Project Builder",
"Prompt Lab",
"Feature Search",
"Task Manager",
"Calculator",
"Timer",
"Theme",
"Help Center"
)
val featurePanel =
new JPanel(
new BorderLayout(8, 8)
)
featurePanel.setBackground(BG)
featurePanel.setBorder(
new EmptyBorder(12, 12, 12, 12)
)
val model =
new DefaultListModel[String]()
var i = 0
while (i < features.length) {
model.addElement(features(i))
i += 1
}
val featureList =
new JList[String](model)
featureList.setBackground(
new Color(12, 15, 21)
)
featureList.setForeground(TEXT)
featureList.setFont(
new Font(
"Segoe UI",
Font.PLAIN,
15
)
)
featurePanel.add(
makeLabel(
"? FEATURE LIBRARY ? " +
features.length +
" FEATURES",
24,
true
),
BorderLayout.NORTH
)
featurePanel.add(
scroll(featureList),
BorderLayout.CENTER
)
tabs.addTab(
"? FEATURES",
featurePanel
)
// ==========================================================
// FINAL LAYOUT
// ==========================================================
root.add(
tabs,
BorderLayout.CENTER
)
frame.setContentPane(root)
frame.setVisible(true)
}
// ============================================================
// LOGIN
// ============================================================
def openLogin(): Unit = {
val login =
new JFrame(
"Scala Swing Learning Studio"
)
login.setSize(
520,
330
)
login.setLocationRelativeTo(null)
login.setDefaultCloseOperation(
WindowConstants.EXIT_ON_CLOSE
)
val panel =
new JPanel(
new BorderLayout(10, 10)
)
panel.setBackground(BG)
panel.setBorder(
new EmptyBorder(
25,
25,
25,
25
)
)
val title =
makeLabel(
"SCALA SWING AI STUDIO",
25,
true
)
title.setHorizontalAlignment(
SwingConstants.CENTER
)
val center =
new JPanel()
center.setLayout(
new BoxLayout(
center,
BoxLayout.Y_AXIS
)
)
center.setBackground(BG)
val infoText =
makeLabel(
"PRIVATE LEARNING CLASSROOM",
14,
true
)
infoText.setAlignmentX(
Component.CENTER_ALIGNMENT
)
val password =
new JPasswordField()
password.setMaximumSize(
new Dimension(
320,
44
)
)
password.setFont(
new Font(
"Segoe UI",
Font.PLAIN,
18
)
)
password.setAlignmentX(
Component.CENTER_ALIGNMENT
)
val enter =
makeButton(
"ENTER CLASSROOM"
)
enter.setAlignmentX(
Component.CENTER_ALIGNMENT
)
val status =
makeLabel(
"READY",
13,
false
)
status.setAlignmentX(
Component.CENTER_ALIGNMENT
)
center.add(infoText)
center.add(
Box.createVerticalStrut(18)
)
center.add(password)
center.add(
Box.createVerticalStrut(15)
)
center.add(enter)
center.add(
Box.createVerticalStrut(10)
)
center.add(status)
panel.add(
title,
BorderLayout.NORTH
)
panel.add(
center,
BorderLayout.CENTER
)
def checkLogin(): Unit = {
val entered =
new String(
password.getPassword
)
if (
entered == passwordSecret
) {
login.dispose()
openStudio()
} else {
status.setText(
"ACCESS DENIED"
)
status.setForeground(RED)
password.setText("")
}
}
enter.addActionListener(
new ActionListener {
def actionPerformed(
e: ActionEvent
): Unit = {
checkLogin()
}
}
)
password.addActionListener(
new ActionListener {
def actionPerformed(
e: ActionEvent
): Unit = {
checkLogin()
}
}
)
login.setContentPane(panel)
login.setVisible(true)
}
// ============================================================
// START
// ============================================================
SwingUtilities.invokeLater(
new Runnable {
def run(): Unit = {
openLogin()
}
}
)