Code Sketch


yoii nemm karoli baba
By: Mhalsakant School
Category: Programming

import java.awt._
import java.awt.geom._
import java.awt.image.BufferedImage
import javax.swing._
import javax.imageio.ImageIO
import java.net.URL

// ================================================
// NEEM KAROLI BABA - COMPLETE SCALA SWING APP
// ================================================

// THEME

val GOLD = new Color(255, 205, 95)
val CREAM = new Color(255, 239, 205)
val BACKGROUND = new Color(25, 10, 4)
val PANEL = new Color(51, 27, 12)
val HEADER = new Color(65, 30, 8)

// PHOTO URL

val PHOTO_URL =
  "https://www.maharajji.love/files/images/Maharajji_Solo_BandW/images/large/Maharajji-518.jpg"

// ================================================
// INFORMATION PAGE
// ================================================

def informationPage(
  pageTitle: String,
  information: String
): JPanel = {

  val page = new JPanel(new BorderLayout(10, 10))

  page.setBackground(BACKGROUND)

  val heading = new JLabel(pageTitle)

  heading.setForeground(GOLD)

  heading.setFont(
    new Font("Serif", Font.BOLD, 28)
  )

  heading.setHorizontalAlignment(
    SwingConstants.CENTER
  )

  heading.setBorder(
    BorderFactory.createEmptyBorder(25, 10, 15, 10)
  )

  val textArea = new JTextArea(information)

  textArea.setEditable(false)
  textArea.setLineWrap(true)
  textArea.setWrapStyleWord(true)

  textArea.setFont(
    new Font("Serif", Font.PLAIN, 19)
  )

  textArea.setForeground(CREAM)
  textArea.setBackground(PANEL)

  textArea.setCaretPosition(0)

  textArea.setBorder(
    BorderFactory.createEmptyBorder(25, 30, 25, 30)
  )

  val scroll = new JScrollPane(textArea)

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

  scroll.getViewport.setBackground(PANEL)

  page.add(heading, BorderLayout.NORTH)
  page.add(scroll, BorderLayout.CENTER)

  page
}

// ================================================
// REAL NEEM KAROLI BABA PHOTO PANEL
// ================================================

class PortraitPanel extends JPanel {

  setPreferredSize(new Dimension(820, 620))
  setBackground(BACKGROUND)

  @volatile private var babaPhoto: BufferedImage = null

  @volatile private var loadingMessage =
    "Loading Maharaj Ji's original photograph..."

  // LOAD PHOTO IN BACKGROUND

  private val photoLoader = new Thread(
    new Runnable {

      override def run(): Unit = {

        try {

          val downloadedPhoto =
            ImageIO.read(new URL(PHOTO_URL))

          SwingUtilities.invokeLater(
            new Runnable {

              override def run(): Unit = {

                if (downloadedPhoto != null) {

                  babaPhoto = downloadedPhoto

                  loadingMessage =
                    "JAI MAHARAJ JI"

                } else {

                  loadingMessage =
                    "Image format could not be loaded."

                }

                repaint()
              }
            }
          )

        } catch {

          case ex: Exception =>

            SwingUtilities.invokeLater(
              new Runnable {

                override def run(): Unit = {

                  loadingMessage =
                    "Photo unavailable. Check internet connection."

                  repaint()
                }
              }
            )
        }
      }
    }
  )

  photoLoader.setDaemon(true)
  photoLoader.start()

  // ==============================================
  // DRAWING
  // ==============================================

  override protected def paintComponent(
    graphics: Graphics
  ): Unit = {

    super.paintComponent(graphics)

    val g = graphics.create().asInstanceOf[Graphics2D]

    try {

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

      g.setRenderingHint(
        RenderingHints.KEY_TEXT_ANTIALIASING,
        RenderingHints.VALUE_TEXT_ANTIALIAS_ON
      )

      // RESPONSIVE SIZE

      val scale = math.min(
        getWidth.toDouble / 820.0,
        getHeight.toDouble / 620.0
      )

      g.translate(
        (getWidth - 820 * scale) / 2.0,
        (getHeight - 620 * scale) / 2.0
      )

      g.scale(scale, scale)

      // BACKGROUND

      g.setPaint(
        new GradientPaint(
          0, 0,
          new Color(25, 8, 2),
          0, 620,
          new Color(105, 48, 9)
        )
      )

      g.fillRect(0, 0, 820, 620)

      // GOLDEN HALO

      g.setPaint(
        new RadialGradientPaint(
          new Point2D.Float(410f, 285f),
          290f,
          Array[Float](0f, 0.70f, 1f),
          Array[Color](
            new Color(255, 220, 120, 100),
            new Color(190, 100, 25, 60),
            new Color(90, 35, 5, 0)
          )
        )
      )

      g.fillOval(120, -5, 580, 580)

      // HALO OUTLINE

      g.setColor(
        new Color(255, 207, 104, 180)
      )

      g.setStroke(new BasicStroke(3f))

      g.drawOval(145, 20, 530, 530)

      g.drawOval(160, 35, 500, 500)

      // STARS

      for (i <- 0 until 80) {

        val x = (i * 137 + 31) % 820
        val y = (i * 71 + 19) % 620

        g.setColor(
          new Color(255, 225, 145, 180)
        )

        g.fillOval(x, y, 3, 3)
      }

      // ==========================================
      // ORIGINAL PHOTOGRAPH
      // ==========================================

      val photo = babaPhoto

      if (photo != null) {

        val iw = photo.getWidth
        val ih = photo.getHeight

        if (iw > 0 && ih > 0) {

          // FIT IMAGE

          val imageScale = math.min(
            470.0 / iw,
            470.0 / ih
          )

          val w = (iw * imageScale).toInt
          val h = (ih * imageScale).toInt

          val x = (820 - w) / 2
          val y = (555 - h) / 2 + 5

          // GOLDEN FRAME

          g.setColor(
            new Color(255, 209, 111)
          )

          g.setStroke(new BasicStroke(9f))

          g.drawRoundRect(
            x - 9,
            y - 9,
            w + 18,
            h + 18,
            15,
            15
          )

          // DARK INNER BORDER

          g.setColor(
            new Color(75, 35, 8)
          )

          g.setStroke(new BasicStroke(3f))

          g.drawRect(
            x - 3,
            y - 3,
            w + 6,
            h + 6
          )

          // PHOTO

          g.setRenderingHint(
            RenderingHints.KEY_INTERPOLATION,
            RenderingHints.VALUE_INTERPOLATION_BICUBIC
          )

          g.drawImage(
            photo,
            x,
            y,
            w,
            h,
            this
          )

          // NAME

          g.setColor(GOLD)

          val font = new Font(
            "Serif",
            Font.BOLD,
            26
          )

          g.setFont(font)

          val name = "NEEM KAROLI BABA"

          val metrics = g.getFontMetrics(font)

          g.drawString(
            name,
            (820 - metrics.stringWidth(name)) / 2,
            600
          )

        }

      } else {

        // LOADING / ERROR MESSAGE

        g.setColor(GOLD)

        g.setFont(
          new Font("Serif", Font.BOLD, 21)
        )

        val messageWidth =
          g.getFontMetrics.stringWidth(loadingMessage)

        g.drawString(
          loadingMessage,
          (820 - messageWidth) / 2,
          315
        )

      }

    } finally {

      g.dispose()

    }
  }
}

// ================================================
// MAIN WINDOW
// ================================================

val frame = new JFrame(
  "NEEM KAROLI BABA | MAHARAJ JI"
)

frame.setDefaultCloseOperation(
  WindowConstants.EXIT_ON_CLOSE
)

frame.setSize(1100, 780)

frame.setMinimumSize(
  new Dimension(750, 550)
)

frame.setLocationRelativeTo(null)

// ================================================
// HEADER
// ================================================

val header = new JPanel(
  new GridLayout(2, 1)
)

header.setBackground(HEADER)

header.setPreferredSize(
  new Dimension(1000, 110)
)

val title = new JLabel(
  "NEEM KAROLI BABA"
)

title.setForeground(GOLD)

title.setFont(
  new Font("Serif", Font.BOLD, 35)
)

title.setHorizontalAlignment(
  SwingConstants.CENTER
)

val subtitle = new JLabel(
  "MAHARAJ JI  |  LOVE  |  SERVICE  |  DEVOTION"
)

subtitle.setForeground(CREAM)

subtitle.setFont(
  new Font("Serif", Font.BOLD, 16)
)

subtitle.setHorizontalAlignment(
  SwingConstants.CENTER
)

header.add(title)
header.add(subtitle)

// ================================================
// HOME PAGE
// ================================================

val home = informationPage(
  "WELCOME TO MAHARAJ JI'S WORLD",

  """? WELCOME TO THE SPIRITUAL WORLD ?

Neem Karoli Baba, lovingly known as Maharaj Ji, was a respected Indian spiritual teacher and devotee of Lord Hanuman.

He is remembered for his teachings of love, kindness, compassion and selfless service.

His simple message continues to inspire people around the world.

Explore the life, teachings and spiritual legacy of Maharaj Ji.

DIVINE DEVOTION

LOVE EVERYONE.
SERVE EVERYONE.
REMEMBER GOD.

JAI SHRI RAM!

JAI HANUMAN!

JAI MAHARAJ JI!"""
)

// ================================================
// BIOGRAPHY
// ================================================

val biography = informationPage(
  "BIOGRAPHY OF NEEM KAROLI BABA",

  """NEEM KAROLI BABA - MAHARAJ JI

EARLY LIFE

Neem Karoli Baba is commonly identified as Lakshman Narayan Sharma.

He was born around 1900 in Uttar Pradesh, India.

He later became known as Neem Karoli Baba or Neeb Karori Baba.

SPIRITUAL JOURNEY

Maharaj Ji travelled through different parts of India.

He became known as a spiritual teacher and devotee of Lord Hanuman.

ASHRAMS

Kainchi Dham in Uttarakhand is one of the most famous ashrams associated with him.

He was also associated with the Vrindavan Ashram.

HIS FINAL DAYS

Neem Karoli Baba passed away on 11 September 1973 in Vrindavan.

HIS LEGACY

His teachings continue to inspire devotees in India and across the world.

His life is remembered through his ashrams, temples and the stories of his devotees."""
)

// ================================================
// TEACHINGS
// ================================================

val teachings = informationPage(
  "TEACHINGS OF MAHARAJ JI",

  """THE TEACHINGS OF NEEM KAROLI BABA

1. LOVE EVERYONE

Treat every person with kindness and respect.

2. SERVE EVERYONE

Help people who need support.

3. FEED EVERYONE

Sharing food is an expression of compassion.

4. REMEMBER GOD

Keep faith and devotion in your heart.

5. BE HUMBLE

Always remain kind and humble.

6. HELP OTHERS

Make the world better through good actions.

7. DEVOTION TO HANUMAN

Maharaj Ji was deeply devoted to Lord Hanuman.

LOVE EVERYONE.
SERVE EVERYONE.
REMEMBER GOD.

JAI MAHARAJ JI!"""
)

// ================================================
// KAINCHI DHAM
// ================================================

val kainchi = informationPage(
  "KAINCHI DHAM - UTTARAKHAND",

  """KAINCHI DHAM ASHRAM

LOCATION

Kainchi Dham is situated in Uttarakhand, near Nainital.

HISTORY

The ashram was established in 1964.

MAHARAJ JI

It is closely associated with Neem Karoli Baba.

LORD HANUMAN

Devotion to Lord Hanuman is an important part of the ashram.

NATURE

The ashram is surrounded by beautiful hills, trees and peaceful scenery.

DEVOTEES

People from India and many other countries visit Kainchi Dham.

ANNUAL CELEBRATION

The annual Bhandara is an important event associated with the ashram.

JAI BAJRANG BALI!

JAI MAHARAJ JI!"""
)

// ================================================
// DRAWING / REAL PHOTO PAGE
// ================================================

val drawingPage = new JPanel(
  new BorderLayout()
)

drawingPage.setBackground(BACKGROUND)

val photoHeading = new JLabel(
  "?  ORIGINAL MAHARAJ JI PHOTOGRAPH  ?"
)

photoHeading.setForeground(GOLD)

photoHeading.setFont(
  new Font("Serif", Font.BOLD, 25)
)

photoHeading.setHorizontalAlignment(
  SwingConstants.CENTER
)

photoHeading.setBorder(
  BorderFactory.createEmptyBorder(12, 5, 12, 5)
)

val portrait = new PortraitPanel()

val drawingScroll = new JScrollPane(portrait)

drawingScroll.setBorder(
  BorderFactory.createEmptyBorder()
)

drawingScroll.getViewport.setBackground(BACKGROUND)

drawingPage.add(
  photoHeading,
  BorderLayout.NORTH
)

drawingPage.add(
  drawingScroll,
  BorderLayout.CENTER
)

// ================================================
// NAVIGATION TABS
// ================================================

val tabs = new JTabbedPane()

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

tabs.setBackground(HEADER)
tabs.setForeground(GOLD)

tabs.addTab("HOME", home)
tabs.addTab("REAL PHOTO", drawingPage)
tabs.addTab("BIOGRAPHY", biography)
tabs.addTab("TEACHINGS", teachings)
tabs.addTab("KAINCHI DHAM", kainchi)

// ================================================
// FOOTER
// ================================================

val footer = new JLabel(
  "JAI SHRI RAM  |  JAI HANUMAN  |  JAI MAHARAJ JI"
)

footer.setForeground(GOLD)
footer.setBackground(HEADER)
footer.setOpaque(true)

footer.setFont(
  new Font("Serif", Font.BOLD, 16)
)

footer.setHorizontalAlignment(
  SwingConstants.CENTER
)

footer.setPreferredSize(
  new Dimension(1000, 45)
)

// ================================================
// MAIN UI
// ================================================

val mainPanel = new JPanel(
  new BorderLayout()
)

mainPanel.setBackground(BACKGROUND)

mainPanel.add(
  header,
  BorderLayout.NORTH
)

mainPanel.add(
  tabs,
  BorderLayout.CENTER
)

mainPanel.add(
  footer,
  BorderLayout.SOUTH
)

// ================================================
// SHOW WINDOW
// ================================================

frame.setContentPane(mainPanel)

frame.setLocationRelativeTo(null)

frame.setVisible(true)