Code Sketch


yoiii iphone 17
By: Mhalsakant School
Category: Programming
import javax.swing._
import java.awt.BorderLayout
import java.awt.CardLayout
import java.awt.Color
import java.awt.Cursor
import java.awt.Dimension
import java.awt.Font
import java.awt.GradientPaint
import java.awt.Graphics
import java.awt.Graphics2D
import java.awt.GridLayout
import java.awt.RenderingHints
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
import java.net.HttpURLConnection
import java.net.URL
import java.text.SimpleDateFormat
import java.util.Date


// ============================================================
// IPHONE 17 PRO SIMULATOR
// FULL SINGLE FILE
// SCALA SWING DIRECT RUN
// ============================================================

class IPhone17ProSimulator extends JPanel {

  val phoneWidth = 430
  val phoneHeight = 850

  val cards = new CardLayout()
  val screens = new JPanel(cards)

  var wallpaperIndex = 0
  var isDarkMode = true

  var noteValue =
    "Welcome to my iPhone 17 Pro Simulator\n\n" +
    "Scala Swing Project\n\n" +
    "Ideas:\n" +
    "Racing Game\n" +
    "Chess Game\n" +
    "Puzzle Game\n" +
    "Adventure Game\n" +
    "AI Game"

  var messageValue =
    "Mom - See you soon\n\n" +
    "Dad - Good morning\n\n" +
    "Aarav - Game tonight?"


  // ==========================================================
  // CONSTRUCTOR SETUP
  // ==========================================================

  setPreferredSize(
    new Dimension(
      phoneWidth,
      phoneHeight
    )
  )

  setLayout(
    new BorderLayout()
  )

  setBackground(
    Color.BLACK
  )


  // ==========================================================
  // ADD ALL SCREENS
  // ==========================================================

  screens.add(
    createLockScreen(),
    "LOCK"
  )

  screens.add(
    createHomeScreen(),
    "HOME"
  )

  screens.add(
    createPhoneScreen(),
    "PHONE"
  )

  screens.add(
    createMessagesScreen(),
    "MESSAGES"
  )

  screens.add(
    createCalculatorScreen(),
    "CALCULATOR"
  )

  screens.add(
    createClockScreen(),
    "CLOCK"
  )

  screens.add(
    createNotesScreen(),
    "NOTES"
  )

  screens.add(
    createCameraScreen(),
    "CAMERA"
  )

  screens.add(
    createPhotosScreen(),
    "PHOTOS"
  )

  screens.add(
    createSettingsScreen(),
    "SETTINGS"
  )

  screens.add(
    createNetworkScreen(),
    "NETWORK"
  )

  screens.add(
    createGamesScreen(),
    "GAMES"
  )

  screens.add(
    createBrowserScreen(),
    "BROWSER"
  )

  screens.add(
    createContactsScreen(),
    "CONTACTS"
  )

  screens.add(
    createMusicScreen(),
    "MUSIC"
  )

  screens.add(
    createWeatherScreen(),
    "WEATHER"
  )

  screens.add(
    createYouTubeScreen(),
    "YOUTUBE"
  )

  screens.add(
    createInstagramScreen(),
    "INSTAGRAM"
  )


  add(
    screens,
    BorderLayout.CENTER
  )


  showScreen(
    "LOCK"
  )


  // ==========================================================
  // LIVE CLOCK REFRESH
  // ==========================================================

  val liveTimer =
    new Timer(
      1000,
      new ActionListener {

        def actionPerformed(
          e: ActionEvent
        ): Unit = {

          screens.repaint()
        }
      }
    )

  liveTimer.start()


  // ==========================================================
  // SCREEN NAVIGATION
  // ==========================================================

  def showScreen(
    name: String
  ): Unit = {

    cards.show(
      screens,
      name
    )

    screens.revalidate()
    screens.repaint()
  }


  // ==========================================================
  // BUTTON FACTORY
  // ==========================================================

  def makeButton(
    text: String,
    action: => Unit
  ): JButton = {

    val b =
      new JButton(
        text
      )

    b.setFont(
      new Font(
        "SansSerif",
        Font.BOLD,
        14
      )
    )

    b.setForeground(
      Color.WHITE
    )

    b.setBackground(
      new Color(
        55,
        75,
        120
      )
    )

    b.setFocusPainted(
      false
    )

    b.setBorderPainted(
      false
    )

    b.setCursor(
      new Cursor(
        Cursor.HAND_CURSOR
      )
    )

    b.setPreferredSize(
      new Dimension(
        155,
        45
      )
    )

    b.addActionListener(
      new ActionListener {

        def actionPerformed(
          e: ActionEvent
        ): Unit = {

          action
        }
      }
    )

    b
  }


  // ==========================================================
  // STATUS BAR
  // ==========================================================

  def createStatusBar(): JPanel = {

    val panel =
      new JPanel(
        new BorderLayout()
      )

    panel.setOpaque(
      false
    )

    val timeLabel =
      new JLabel(
        new SimpleDateFormat(
          "HH:mm"
        ).format(
          new Date()
        )
      )

    timeLabel.setForeground(
      Color.WHITE
    )

    timeLabel.setFont(
      new Font(
        "SansSerif",
        Font.BOLD,
        13
      )
    )

    val networkLabel =
      new JLabel(
        "SIGNAL   WIFI   BATTERY"
      )

    networkLabel.setForeground(
      Color.WHITE
    )

    networkLabel.setFont(
      new Font(
        "SansSerif",
        Font.BOLD,
        11
      )
    )

    panel.add(
      timeLabel,
      BorderLayout.WEST
    )

    panel.add(
      networkLabel,
      BorderLayout.EAST
    )

    panel.setBorder(
      BorderFactory.createEmptyBorder(
        8,
        15,
        8,
        15
      )
    )

    panel
  }


  // ==========================================================
  // WALLPAPER
  // ==========================================================

  class WallpaperPanel
      extends JPanel {

    override def paintComponent(
      g: Graphics
    ): Unit = {

      super.paintComponent(
        g
      )

      val g2 =
        g.asInstanceOf[
          Graphics2D
        ]

      g2.setRenderingHint(
        RenderingHints.KEY_ANTIALIASING,
        RenderingHints.VALUE_ANTIALIAS_ON
      )

      var top =
        new Color(
          10,
          25,
          100
        )

      var bottom =
        new Color(
          100,
          20,
          160
        )

      if (
        wallpaperIndex == 1
      ) {

        top =
          new Color(
            125,
            35,
            82
          )

        bottom =
          new Color(
            20,
            105,
            180
          )
      }

      if (
        wallpaperIndex == 2
      ) {

        top =
          new Color(
            4,
            8,
            25
          )

        bottom =
          new Color(
            80,
            5,
            125
          )
      }

      g2.setPaint(
        new GradientPaint(
          0,
          0,
          top,
          0,
          getHeight,
          bottom
        )
      )

      g2.fillRect(
        0,
        0,
        getWidth,
        getHeight
      )

      val random =
        new scala.util.Random(
          777
        )

      var i = 0

      while (
        i < 100
      ) {

        val x =
          random.nextInt(
            math.max(
              1,
              getWidth
            )
          )

        val y =
          random.nextInt(
            math.max(
              1,
              getHeight
            )
          )

        val size =
          1 +
          random.nextInt(
            4
          )

        g2.setColor(
          new Color(
            255,
            255,
            255,
            100 +
            random.nextInt(
              120
            )
          )
        )

        g2.fillOval(
          x,
          y,
          size,
          size
        )

        i += 1
      }
    }
  }


  // ==========================================================
  // LOCK SCREEN
  // ==========================================================

  def createLockScreen(): JPanel = {

    val panel =
      new WallpaperPanel()

    panel.setLayout(
      new BorderLayout()
    )

    panel.add(
      createStatusBar(),
      BorderLayout.NORTH
    )

    val center =
      new JPanel()

    center.setOpaque(
      false
    )

    center.setLayout(
      new BoxLayout(
        center,
        BoxLayout.Y_AXIS
      )
    )

    val date =
      new JLabel(
        new SimpleDateFormat(
          "EEEE, MMMM d"
        ).format(
          new Date()
        )
      )

    date.setForeground(
      Color.WHITE
    )

    date.setFont(
      new Font(
        "SansSerif",
        Font.BOLD,
        18
      )
    )

    date.setAlignmentX(
      0.5f
    )

    val time =
      new JLabel(
        new SimpleDateFormat(
          "HH:mm"
        ).format(
          new Date()
        )
      )

    time.setForeground(
      Color.WHITE
    )

    time.setFont(
      new Font(
        "SansSerif",
        Font.BOLD,
        76
      )
    )

    time.setAlignmentX(
      0.5f
    )

    val unlock =
      makeButton(
        "UNLOCK",
        showScreen(
          "HOME"
        )
      )

    unlock.setAlignmentX(
      0.5f
    )

    center.add(
      Box.createVerticalStrut(
        100
      )
    )

    center.add(
      date
    )

    center.add(
      time
    )

    center.add(
      Box.createVerticalStrut(
        35
      )
    )

    center.add(
      unlock
    )

    panel.add(
      center,
      BorderLayout.CENTER
    )

    panel
  }


  // ==========================================================
  // HOME SCREEN
  // ==========================================================

  def createHomeScreen(): JPanel = {

    val panel =
      new WallpaperPanel()

    panel.setLayout(
      new BorderLayout()
    )

    panel.add(
      createStatusBar(),
      BorderLayout.NORTH
    )

    val grid =
      new JPanel(
        new GridLayout(
          5,
          4,
          8,
          10
        )
      )

    grid.setOpaque(
      false
    )

    grid.setBorder(
      BorderFactory.createEmptyBorder(
        15,
        10,
        10,
        10
      )
    )

    addHomeApp(
      grid,
      "PHONE",
      "Phone"
    )

    addHomeApp(
      grid,
      "MESSAGES",
      "Messages"
    )

    addHomeApp(
      grid,
      "CALCULATOR",
      "Calculator"
    )

    addHomeApp(
      grid,
      "CLOCK",
      "Clock"
    )

    addHomeApp(
      grid,
      "CAMERA",
      "Camera"
    )

    addHomeApp(
      grid,
      "PHOTOS",
      "Photos"
    )

    addHomeApp(
      grid,
      "NOTES",
      "Notes"
    )

    addHomeApp(
      grid,
      "SETTINGS",
      "Settings"
    )

    addHomeApp(
      grid,
      "NETWORK",
      "Network"
    )

    addHomeApp(
      grid,
      "GAMES",
      "Games"
    )

    addHomeApp(
      grid,
      "BROWSER",
      "Safari"
    )

    addHomeApp(
      grid,
      "CONTACTS",
      "Contacts"
    )

    addHomeApp(
      grid,
      "MUSIC",
      "Music"
    )

    addHomeApp(
      grid,
      "WEATHER",
      "Weather"
    )

    addHomeApp(
      grid,
      "YOUTUBE",
      "YouTube"
    )

    addHomeApp(
      grid,
      "INSTAGRAM",
      "Instagram"
    )

    addHomeApp(
      grid,
      "PHONE",
      "Call"
    )

    addHomeApp(
      grid,
      "NOTES",
      "Files"
    )

    addHomeApp(
      grid,
      "BROWSER",
      "Search"
    )

    addHomeApp(
      grid,
      "GAMES",
      "More"
    )

    panel.add(
      grid,
      BorderLayout.CENTER
    )

    panel
  }


  // ==========================================================
  // HOME APP BUTTON
  // ==========================================================

  def addHomeApp(
    parent: JPanel,
    page: String,
    name: String
  ): Unit = {

    val holder =
      new JPanel(
        new BorderLayout()
      )

    holder.setOpaque(
      false
    )

    val button =
      new JButton(
        name.toUpperCase
      )

    button.setFont(
      new Font(
        "SansSerif",
        Font.BOLD,
        9
      )
    )

    button.setForeground(
      Color.WHITE
    )

    button.setBackground(
      new Color(
        60,
        85,
        145
      )
    )

    button.setFocusPainted(
      false
    )

    button.setBorderPainted(
      false
    )

    button.addActionListener(
      new ActionListener {

        def actionPerformed(
          e: ActionEvent
        ): Unit = {

          showScreen(
            page
          )
        }
      }
    )

    val label =
      new JLabel(
        name,
        SwingConstants.CENTER
      )

    label.setForeground(
      Color.WHITE
    )

    label.setFont(
      new Font(
        "SansSerif",
        Font.PLAIN,
        10
      )
    )

    holder.add(
      button,
      BorderLayout.CENTER
    )

    holder.add(
      label,
      BorderLayout.SOUTH
    )

    parent.add(
      holder
    )
  }


  // ==========================================================
  // APP BASE
  // ==========================================================

  def appBase(
    title: String
  ): JPanel = {

    val panel =
      new JPanel(
        new BorderLayout()
      )

    if (
      isDarkMode
    ) {

      panel.setBackground(
        new Color(
          18,
          20,
          27
        )
      )

    } else {

      panel.setBackground(
        new Color(
          240,
          241,
          245
        )
      )
    }

    val top =
      new JPanel(
        new BorderLayout()
      )

    top.setOpaque(
      false
    )

    val back =
      makeButton(
        "BACK",
        showScreen(
          "HOME"
        )
      )

    back.setPreferredSize(
      new Dimension(
        85,
        40
      )
    )

    val label =
      new JLabel(
        title,
        SwingConstants.CENTER
      )

    if (
      isDarkMode
    ) {

      label.setForeground(
        Color.WHITE
      )

    } else {

      label.setForeground(
        Color.BLACK
      )
    }

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

    top.add(
      back,
      BorderLayout.WEST
    )

    top.add(
      label,
      BorderLayout.CENTER
    )

    panel.add(
      top,
      BorderLayout.NORTH
    )

    panel
  }


  // ==========================================================
  // PHONE
  // ==========================================================

  def createPhoneScreen(): JPanel = {

    val panel =
      appBase(
        "Phone"
      )

    val area =
      new JTextArea(
        "RECENTS\n\n" +
        "Mom       Incoming Call\n\n" +
        "Dad       Outgoing Call\n\n" +
        "Aarav     Missed Call\n\n" +
        "School    Incoming Call"
      )

    area.setFont(
      new Font(
        "SansSerif",
        Font.PLAIN,
        17
      )
    )

    area.setEditable(
      false
    )

    panel.add(
      new JScrollPane(
        area
      ),
      BorderLayout.CENTER
    )

    val callButton =
      makeButton(
        "CALL",
        {

          val name =
            JOptionPane.showInputDialog(
              panel,
              "Enter contact name:"
            )

          if (
            name != null &&
            name.trim.length > 0
          ) {

            JOptionPane.showMessageDialog(
              panel,
              "Calling " +
              name +
              "...\n\n" +
              "Simulator call connected."
            )
          }
        }
      )

    panel.add(
      callButton,
      BorderLayout.SOUTH
    )

    panel
  }


  // ==========================================================
  // MESSAGES
  // ==========================================================

  def createMessagesScreen(): JPanel = {

    val panel =
      appBase(
        "Messages"
      )

    val area =
      new JTextArea(
        messageValue
      )

    area.setFont(
      new Font(
        "SansSerif",
        Font.PLAIN,
        17
      )
    )

    panel.add(
      new JScrollPane(
        area
      ),
      BorderLayout.CENTER
    )

    val saveButton =
      makeButton(
        "SAVE",
        {

          messageValue =
            area.getText

          JOptionPane.showMessageDialog(
            panel,
            "Messages saved."
          )
        }
      )

    panel.add(
      saveButton,
      BorderLayout.SOUTH
    )

    panel
  }


  // ==========================================================
  // CALCULATOR
  // ==========================================================

  def createCalculatorScreen(): JPanel = {

    val panel =
      appBase(
        "Calculator"
      )

    val display =
      new JTextField(
        "0"
      )

    display.setHorizontalAlignment(
      SwingConstants.RIGHT
    )

    display.setFont(
      new Font(
        "Monospaced",
        Font.BOLD,
        27
      )
    )

    display.setEditable(
      false
    )

    panel.add(
      display,
      BorderLayout.NORTH
    )

    val grid =
      new JPanel(
        new GridLayout(
          5,
          4,
          5,
          5
        )
      )

    val values =
      Array(
        "7",
        "8",
        "9",
        "/",
        "4",
        "5",
        "6",
        "*",
        "1",
        "2",
        "3",
        "-",
        "0",
        ".",
        "=",
        "+",
        "C",
        "(",
        ")",
        "%"
      )

    var expression =
      ""

    values.foreach(
      key => {

        val button =
          makeButton(
            key,
            {

              if (
                key == "C"
              ) {

                expression =
                  ""

                display.setText(
                  "0"
                )

              } else if (
                key == "="
              ) {

                try {

                  val result =
                    calculate(
                      expression
                    )

                  expression =
                    result.toString

                  display.setText(
                    expression
                  )

                } catch {

                  case _: Throwable =>

                    display.setText(
                      "ERROR"
                    )
                }

              } else {

                expression +=
                  key

                display.setText(
                  expression
                )
              }
            }
          )

        grid.add(
          button
        )
      }
    )

    panel.add(
      grid,
      BorderLayout.CENTER
    )

    panel
  }


  // ==========================================================
  // CALCULATOR ENGINE
  // ==========================================================

  def calculate(
    text: String
  ): Double = {

    val plus =
      text.indexOf(
        "+"
      )

    if (
      plus > 0
    ) {

      val left =
        text.substring(
          0,
          plus
        ).toDouble

      val right =
        text.substring(
          plus + 1
        ).toDouble

      left + right

    } else {

      val minus =
        text.lastIndexOf(
          "-"
        )

      if (
        minus > 0
      ) {

        val left =
          text.substring(
            0,
            minus
          ).toDouble

        val right =
          text.substring(
            minus + 1
          ).toDouble

        left - right

      } else {

        val multiply =
          text.indexOf(
            "*"
          )

        if (
          multiply > 0
        ) {

          val left =
            text.substring(
              0,
              multiply
            ).toDouble

          val right =
            text.substring(
              multiply + 1
            ).toDouble

          left * right

        } else {

          val divide =
            text.indexOf(
              "/"
            )

          if (
            divide > 0
          ) {

            val left =
              text.substring(
                0,
                divide
              ).toDouble

            val right =
              text.substring(
                divide + 1
              ).toDouble

            left / right

          } else {

            text.toDouble
          }
        }
      }
    }
  }


  // ==========================================================
  // CLOCK
  // ==========================================================

  def createClockScreen(): JPanel = {

    val panel =
      appBase(
        "Clock"
      )

    val box =
      new JPanel()

    box.setOpaque(
      false
    )

    box.setLayout(
      new BoxLayout(
        box,
        BoxLayout.Y_AXIS
      )
    )

    val time =
      new JLabel(
        new SimpleDateFormat(
          "HH:mm:ss"
        ).format(
          new Date()
        )
      )

    time.setFont(
      new Font(
        "Monospaced",
        Font.BOLD,
        50
      )
    )

    time.setAlignmentX(
      0.5f
    )

    val date =
      new JLabel(
        new SimpleDateFormat(
          "EEEE, MMMM d, yyyy"
        ).format(
          new Date()
        )
      )

    date.setFont(
      new Font(
        "SansSerif",
        Font.PLAIN,
        17
      )
    )

    date.setAlignmentX(
      0.5f
    )

    box.add(
      Box.createVerticalStrut(
        90
      )
    )

    box.add(
      time
    )

    box.add(
      Box.createVerticalStrut(
        20
      )
    )

    box.add(
      date
    )

    panel.add(
      box,
      BorderLayout.CENTER
    )

    panel
  }


  // ==========================================================
  // NOTES
  // ==========================================================

  def createNotesScreen(): JPanel = {

    val panel =
      appBase(
        "Notes"
      )

    val area =
      new JTextArea(
        noteValue
      )

    area.setFont(
      new Font(
        "SansSerif",
        Font.PLAIN,
        17
      )
    )

    area.setLineWrap(
      true
    )

    area.setWrapStyleWord(
      true
    )

    panel.add(
      new JScrollPane(
        area
      ),
      BorderLayout.CENTER
    )

    val save =
      makeButton(
        "SAVE NOTE",
        {

          noteValue =
            area.getText

          JOptionPane.showMessageDialog(
            panel,
            "Note saved."
          )
        }
      )

    panel.add(
      save,
      BorderLayout.SOUTH
    )

    panel
  }


  // ==========================================================
  // CAMERA
  // ==========================================================

  def createCameraScreen(): JPanel = {

    val panel =
      appBase(
        "Camera"
      )

    val preview =
      new JPanel() {

        override def paintComponent(
          g: Graphics
        ): Unit = {

          super.paintComponent(
            g
          )

          val g2 =
            g.asInstanceOf[
              Graphics2D
            ]

          g2.setPaint(
            new GradientPaint(
              0,
              0,
              new Color(
                70,
                75,
                90
              ),
              0,
              getHeight,
              Color.BLACK
            )
          )

          g2.fillRect(
            0,
            0,
            getWidth,
            getHeight
          )

          g2.setColor(
            Color.WHITE
          )

          g2.drawString(
            "CAMERA PREVIEW",
            15,
            25
          )

          g2.drawLine(
            getWidth / 2,
            0,
            getWidth / 2,
            getHeight
          )

          g2.drawLine(
            0,
            getHeight / 2,
            getWidth,
            getHeight / 2
          )
        }
      }

    panel.add(
      preview,
      BorderLayout.CENTER
    )

    val capture =
      makeButton(
        "TAKE PHOTO",
        {

          JOptionPane.showMessageDialog(
            panel,
            "Photo captured successfully."
          )
        }
      )

    panel.add(
      capture,
      BorderLayout.SOUTH
    )

    panel
  }


  // ==========================================================
  // PHOTOS
  // ==========================================================

  def createPhotosScreen(): JPanel = {

    val panel =
      appBase(
        "Photos"
      )

    val grid =
      new JPanel(
        new GridLayout(
          3,
          2,
          8,
          8
        )
      )

    var i =
      1

    while (
      i <= 6
    ) {

      val photo =
        new JButton(
          "PHOTO " +
          i
        )

      photo.setFont(
        new Font(
          "SansSerif",
          Font.BOLD,
          17
        )
      )

      grid.add(
        photo
      )

      i += 1
    }

    panel.add(
      grid,
      BorderLayout.CENTER
    )

    panel
  }


  // ==========================================================
  // SETTINGS
  // ==========================================================

  def createSettingsScreen(): JPanel = {

    val panel =
      appBase(
        "Settings"
      )

    val box =
      new JPanel()

    box.setOpaque(
      false
    )

    box.setLayout(
      new BoxLayout(
        box,
        BoxLayout.Y_AXIS
      )
    )

    val dark =
      new JCheckBox(
        "Dark Mode",
        isDarkMode
      )

    dark.setOpaque(
      false
    )

    dark.setFont(
      new Font(
        "SansSerif",
        Font.BOLD,
        16
      )
    )

    dark.addActionListener(
      new ActionListener {

        def actionPerformed(
          e: ActionEvent
        ): Unit = {

          isDarkMode =
            dark.isSelected

          screens.repaint()
        }
      }
    )

    val wallpaperLabel =
      new JLabel(
        "Wallpaper"
      )

    wallpaperLabel.setFont(
      new Font(
        "SansSerif",
        Font.BOLD,
        18
      )
    )

    val cosmic =
      makeButton(
        "COSMIC",
        {

          wallpaperIndex =
            0

          screens.repaint()
        }
      )

    val sunset =
      makeButton(
        "SUNSET",
        {

          wallpaperIndex =
            1

          screens.repaint()
        }
      )

    val neon =
      makeButton(
        "NEON",
        {

          wallpaperIndex =
            2

          screens.repaint()
        }
      )

    box.add(
      dark
    )

    box.add(
      Box.createVerticalStrut(
        20
      )
    )

    box.add(
      wallpaperLabel
    )

    box.add(
      Box.createVerticalStrut(
        10
      )
    )

    box.add(
      cosmic
    )

    box.add(
      Box.createVerticalStrut(
        8
      )
    )

    box.add(
      sunset
    )

    box.add(
      Box.createVerticalStrut(
        8
      )
    )

    box.add(
      neon
    )

    panel.add(
      box,
      BorderLayout.CENTER
    )

    panel
  }


  // ==========================================================
  // NETWORK
  // ==========================================================

  def createNetworkScreen(): JPanel = {

    val panel =
      appBase(
        "Network"
      )

    val box =
      new JPanel()

    box.setOpaque(
      false
    )

    box.setLayout(
      new BoxLayout(
        box,
        BoxLayout.Y_AXIS
      )
    )

    val status =
      new JLabel(
        "READY"
      )

    status.setFont(
      new Font(
        "SansSerif",
        Font.BOLD,
        22
      )
    )

    status.setAlignmentX(
      0.5f
    )

    val info =
      new JLabel(
        "Wi-Fi Available"
      )

    info.setAlignmentX(
      0.5f
    )

    val check =
      makeButton(
        "CHECK INTERNET",
        {

          status.setText(
            "CHECKING..."
          )

          val worker =
            new Thread(
              new Runnable {

                def run(): Unit = {

                  try {

                    val connection =
                      new URL(
                        "https://www.google.com"
                      )
                        .openConnection()
                        .asInstanceOf[
                          HttpURLConnection
                        ]

                    connection.setConnectTimeout(
                      5000
                    )

                    connection.setReadTimeout(
                      5000
                    )

                    connection.setRequestMethod(
                      "HEAD"
                    )

                    val code =
                      connection.getResponseCode

                    SwingUtilities.invokeLater(
                      new Runnable {

                        def run(): Unit = {

                          status.setText(
                            "INTERNET CONNECTED"
                          )

                          info.setText(
                            "HTTP STATUS: " +
                            code
                          )
                        }
                      }
                    )

                    connection.disconnect()

                  } catch {

                    case _: Throwable =>

                      SwingUtilities.invokeLater(
                        new Runnable {

                          def run(): Unit = {

                            status.setText(
                              "OFFLINE"
                            )

                            info.setText(
                              "Internet unavailable"
                            )
                          }
                        }
                      )
                  }
                }
              }
            )

          worker.start()
        }
      )

    check.setAlignmentX(
      0.5f
    )

    box.add(
      Box.createVerticalStrut(
        70
      )
    )

    box.add(
      status
    )

    box.add(
      Box.createVerticalStrut(
        20
      )
    )

    box.add(
      info
    )

    box.add(
      Box.createVerticalStrut(
        25
      )
    )

    box.add(
      check
    )

    panel.add(
      box,
      BorderLayout.CENTER
    )

    panel
  }


  // ==========================================================
  // GAMES
  // ==========================================================

  def createGamesScreen(): JPanel = {

    val panel =
      appBase(
        "Game Center"
      )

    val box =
      new JPanel()

    box.setOpaque(
      false
    )

    box.setLayout(
      new BoxLayout(
        box,
        BoxLayout.Y_AXIS
      )
    )

    val tic =
      makeButton(
        "TIC TAC TOE",
        startTicTacToe()
      )

    tic.setAlignmentX(
      0.5f
    )

    val number =
      makeButton(
        "NUMBER GUESS",
        startNumberGuess()
      )

    number.setAlignmentX(
      0.5f
    )

    box.add(
      Box.createVerticalStrut(
        70
      )
    )

    box.add(
      tic
    )

    box.add(
      Box.createVerticalStrut(
        20
      )
    )

    box.add(
      number
    )

    panel.add(
      box,
      BorderLayout.CENTER
    )

    panel
  }


  // ==========================================================
  // TIC TAC TOE
  // ==========================================================

  def startTicTacToe(): Unit = {

    val board =
      new JPanel(
        new GridLayout(
          3,
          3,
          5,
          5
        )
      )

    val state =
      Array.fill[String](
        9
      )(
        ""
      )

    val buttons =
      new Array[JButton](
        9
      )

    var turn =
      "X"

    var i =
      0

    while (
      i < 9
    ) {

      val index =
        i

      val b =
        new JButton(
          ""
        )

      b.setFont(
        new Font(
          "SansSerif",
          Font.BOLD,
          32
        )
      )

      b.addActionListener(
        new ActionListener {

          def actionPerformed(
            e: ActionEvent
          ): Unit = {

            if (
              state(index) == ""
            ) {

              state(index) =
                turn

              buttons(index).setText(
                turn
              )

              if (
                checkWinner(
                  state
                )
              ) {

                JOptionPane.showMessageDialog(
                  board,
                  turn +
                  " WINS!"
                )

              } else if (
                state.forall(
                  x =>
                    x != ""
                )
              ) {

                JOptionPane.showMessageDialog(
                  board,
                  "DRAW!"
                )

              } else {

                if (
                  turn == "X"
                ) {

                  turn =
                    "O"

                } else {

                  turn =
                    "X"
                }
              }
            }
          }
        }
      )

      buttons(i) =
        b

      board.add(
        b
      )

      i += 1
    }

    JOptionPane.showMessageDialog(
      this,
      board,
      "Tic Tac Toe",
      JOptionPane.PLAIN_MESSAGE
    )
  }


  // ==========================================================
  // WINNER
  // ==========================================================

  def checkWinner(
    state: Array[String]
  ): Boolean = {

    val lines =
      Array(
        Array(0,1,2),
        Array(3,4,5),
        Array(6,7,8),
        Array(0,3,6),
        Array(1,4,7),
        Array(2,5,8),
        Array(0,4,8),
        Array(2,4,6)
      )

    lines.exists(
      line =>
        state(line(0)) != "" &&
        state(line(0)) ==
          state(line(1)) &&
        state(line(1)) ==
          state(line(2))
    )
  }


  // ==========================================================
  // NUMBER GUESS
  // ==========================================================

  def startNumberGuess(): Unit = {

    val answer =
      new scala.util.Random()
        .nextInt(
          100
        ) +
        1

    var attempts =
      0

    val field =
      new JTextField()

    val label =
      new JLabel(
        "Guess a number from 1 to 100"
      )

    val panel =
      new JPanel(
        new BorderLayout(
          8,
          8
        )
      )

    val guess =
      new JButton(
        "GUESS"
      )

    guess.addActionListener(
      new ActionListener {

        def actionPerformed(
          e: ActionEvent
        ): Unit = {

          try {

            val number =
              field.getText
                .trim
                .toInt

            attempts +=
              1

            if (
              number < answer
            ) {

              label.setText(
                "TOO LOW"
              )

            } else if (
              number > answer
            ) {

              label.setText(
                "TOO HIGH"
              )

            } else {

              label.setText(
                "CORRECT!"
              )

              JOptionPane.showMessageDialog(
                panel,
                "Correct!\nAttempts: " +
                attempts
              )
            }

          } catch {

            case _: Throwable =>
              label.setText(
                "ENTER A NUMBER"
              )
          }
        }
      }
    )

    panel.add(
      label,
      BorderLayout.NORTH
    )

    panel.add(
      field,
      BorderLayout.CENTER
    )

    panel.add(
      guess,
      BorderLayout.SOUTH
    )

    JOptionPane.showMessageDialog(
      this,
      panel,
      "Number Guess",
      JOptionPane.PLAIN_MESSAGE
    )
  }


  // ==========================================================
  // BROWSER
  // ==========================================================

  def createBrowserScreen(): JPanel = {

    val panel =
      appBase(
        "Safari"
      )

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

    val url =
      new JTextField(
        "https://www.google.com"
      )

    val open =
      makeButton(
        "OPEN WEBSITE",
        {

          try {

            java.awt.Desktop
              .getDesktop
              .browse(
                new java.net.URI(
                  url.getText.trim
                )
              )

          } catch {

            case _: Throwable =>

              JOptionPane.showMessageDialog(
                panel,
                "Could not open website."
              )
          }
        }
      )

    box.add(
      new JLabel(
        "Website URL:"
      )
    )

    box.add(
      url
    )

    box.add(
      open
    )

    panel.add(
      box,
      BorderLayout.CENTER
    )

    panel
  }


  // ==========================================================
  // CONTACTS
  // ==========================================================

  def createContactsScreen(): JPanel = {

    val panel =
      appBase(
        "Contacts"
      )

    val model =
      new DefaultListModel[String]()

    model.addElement(
      "Mom       +91 90000 00001"
    )

    model.addElement(
      "Dad       +91 90000 00002"
    )

    model.addElement(
      "Aarav     +91 90000 00003"
    )

    model.addElement(
      "School    +91 90000 00004"
    )

    val list =
      new JList[String](
        model
      )

    list.setFont(
      new Font(
        "SansSerif",
        Font.PLAIN,
        16
      )
    )

    panel.add(
      new JScrollPane(
        list
      ),
      BorderLayout.CENTER
    )

    val addContact =
      makeButton(
        "ADD CONTACT",
        {

          val name =
            JOptionPane.showInputDialog(
              panel,
              "Contact name:"
            )

          if (
            name != null &&
            name.trim.length > 0
          ) {

            val number =
              JOptionPane.showInputDialog(
                panel,
                "Phone number:"
              )

            model.addElement(
              name +
              "    " +
              number
            )
          }
        }
      )

    panel.add(
      addContact,
      BorderLayout.SOUTH
    )

    panel
  }


  // ==========================================================
  // MUSIC
  // ==========================================================

  def createMusicScreen(): JPanel = {

    val panel =
      appBase(
        "Music"
      )

    val box =
      new JPanel()

    box.setOpaque(
      false
    )

    box.setLayout(
      new BoxLayout(
        box,
        BoxLayout.Y_AXIS
      )
    )

    val musicTitle =
      new JLabel(
        "MUSIC"
      )

    musicTitle.setFont(
      new Font(
        "SansSerif",
        Font.BOLD,
        42
      )
    )

    musicTitle.setAlignmentX(
      0.5f
    )

    val song =
      new JLabel(
        "My Favorite Song"
      )

    song.setAlignmentX(
      0.5f
    )

    val play =
      makeButton(
        "PLAY / PAUSE",
        JOptionPane.showMessageDialog(
          panel,
          "Music player toggled."
        )
      )

    play.setAlignmentX(
      0.5f
    )

    box.add(
      Box.createVerticalStrut(
        80
      )
    )

    box.add(
      musicTitle
    )

    box.add(
      Box.createVerticalStrut(
        20
      )
    )

    box.add(
      song
    )

    box.add(
      Box.createVerticalStrut(
        25
      )
    )

    box.add(
      play
    )

    panel.add(
      box,
      BorderLayout.CENTER
    )

    panel
  }


  // ==========================================================
  // WEATHER
  // ==========================================================

  def createWeatherScreen(): JPanel = {

    val panel =
      appBase(
        "Weather"
      )

    val box =
      new JPanel()

    box.setOpaque(
      false
    )

    box.setLayout(
      new BoxLayout(
        box,
        BoxLayout.Y_AXIS
      )
    )

    val city =
      new JLabel(
        "New Delhi"
      )

    city.setFont(
      new Font(
        "SansSerif",
        Font.BOLD,
        26
      )
    )

    city.setAlignmentX(
      0.5f
    )

    val temperature =
      new JLabel(
        "28 C"
      )

    temperature.setFont(
      new Font(
        "SansSerif",
        Font.BOLD,
        68
      )
    )

    temperature.setAlignmentX(
      0.5f
    )

    val condition =
      new JLabel(
        "Sunny"
      )

    condition.setFont(
      new Font(
        "SansSerif",
        Font.PLAIN,
        20
      )
    )

    condition.setAlignmentX(
      0.5f
    )

    box.add(
      Box.createVerticalStrut(
        70
      )
    )

    box.add(
      city
    )

    box.add(
      temperature
    )

    box.add(
      condition
    )

    panel.add(
      box,
      BorderLayout.CENTER
    )

    panel
  }


  // ==========================================================
  // YOUTUBE
  // ==========================================================

  def createYouTubeScreen(): JPanel = {

    createSocialScreen(
      "YouTube",
      "https://www.youtube.com"
    )
  }


  // ==========================================================
  // INSTAGRAM
  // ==========================================================

  def createInstagramScreen(): JPanel = {

    createSocialScreen(
      "Instagram",
      "https://www.instagram.com"
    )
  }


  // ==========================================================
  // SOCIAL SCREEN
  // ==========================================================

  def createSocialScreen(
    title: String,
    url: String
  ): JPanel = {

    val panel =
      appBase(
        title
      )

    val box =
      new JPanel()

    box.setOpaque(
      false
    )

    box.setLayout(
      new BoxLayout(
        box,
        BoxLayout.Y_AXIS
      )
    )

    val label =
      new JLabel(
        title
      )

    label.setFont(
      new Font(
        "SansSerif",
        Font.BOLD,
        32
      )
    )

    label.setAlignmentX(
      0.5f
    )

    val open =
      makeButton(
        "OPEN " + title,
        {

          try {

            java.awt.Desktop
              .getDesktop
              .browse(
                new java.net.URI(
                  url
                )
              )

          } catch {

            case _: Throwable =>

              JOptionPane.showMessageDialog(
                panel,
                "Could not open " +
                title
              )
          }
        }
      )

    open.setAlignmentX(
      0.5f
    )

    box.add(
      Box.createVerticalStrut(
        100
      )
    )

    box.add(
      label
    )

    box.add(
      Box.createVerticalStrut(
        25
      )
    )

    box.add(
      open
    )

    panel.add(
      box,
      BorderLayout.CENTER
    )

    panel
  }
}


// ============================================================
// START APPLICATION
// ============================================================

val iphoneFrame =
  new JFrame(
    "iPhone 17 Pro Simulator"
  )

iphoneFrame.setDefaultCloseOperation(
  WindowConstants.EXIT_ON_CLOSE
)

iphoneFrame.setResizable(
  false
)

iphoneFrame.setContentPane(
  new IPhone17ProSimulator()
)

iphoneFrame.pack()

iphoneFrame.setLocationRelativeTo(
  null
)

iphoneFrame.setVisible(
  true
)