How to Build Wishing Happy Birthday Graphics Program by Java

Happy Birthday Graphics Program by Java

Introduction :

Birthdays are precious moments which bring joy and happiness in our life. Wishing or celebrating someone’s happy birthdays is a time-honored tradition that brings joy and warmth to our lives. In this digital age, you have the tools and technology to create unique and memorable birthday wishes. Using Java language, you can create a birthday wishing animated graphics program with displaying images and play a happy birthday song.

In this article, I shall explore you how to wish someone a happy birthday using the Swing library of the Java programming language with source code. Here, I shall show you how to create a Java graphics animation program to wish someone a happy birthday in a truly special way that’s not only heartfelt but also visually appealing.

What is Birthday :

A birthday is the anniversary of the day a person was born, typically celebrated with some form of gathering, party or special event. Common birthday traditions include singing “Happy Birthday song” blowing out candles on a birthday cake and making a wish to birthday person. It’s a special occasion where the birthday person often receives well-wishes, gifts and spend time with friends and family to celebrate the anniversary of their birth.

Birthdays are an important part of many cultures around the world. Birthdays are celebrated in various ways around the world depending on cultural and personal preferences. Overall, birthdays are a time for people to celebrate their life and the passing of another year.

About the program :

This Java program is creating a simple GUI program that displays a birthday-wishes graphical interface with images, text, lines and plays an audio file (happy birthday song). When you run this program, it will open a graphical window displaying the various elements on the screen. Here, the background and foreground colors of the panel will be randomly generated. This means that the background color of the panel and the color of the text will change every time you run the program.

There are four lines originating from a random point display in the screen to create designs in the graphics. You can see four images displayed at specific locations on the panel. You have to keep the image files in the same directory where your Java file is situated. You can also see three texts (“TO ARPITA”, “HAPPY BIRTHDAY” and “FROM PUSKAR”) will be displayed on the panel with different fonts and colors. Additionally, the program attempts to play an audio file named “original-happy-birthday-song-1-48291.wav” when the program starts. If the file exists in the specified location (same directory as the Java file), you will hear the happy birthday song in the background.

Explanation of the program :

In the program, you have to import necessary Java packages for GUI components, file handling, audio playback and more. Here, import “awt” package for “Graphics”, “Image”, “Color” and “Font” classes. Then, import “sound.sampled” for “AudioInputStream”, “AudioSystem”, “Clip” classes. You have to import “io” and “util” for “File” and “Random” classes respectively. Lastly, you can import “swing” for “JPanel”, “ImageIcon”, “JFrame”.

After that, define the birth class which extends JPanel and is responsible for drawing the graphical elements on the panel. It overrides the paintComponent method to define how the graphical elements should be drawn. Next, call the super.paintComponent(g) method to clear the panel and prepare it for drawing. Using the Random class, creates an instance for generate random values.

Now, store four images using ImageIcon objects and converts them to Image objects for display on the screen. After that, generates random RGB color values for background and foreground colors. Then, display lines connecting from a random point in different directions using drawLine() method. Next, drawImage() method use for display images on specific locations on the panel. The setFont() and drawString() methods are used for draws text strings on the panel. Using Thread.sleep() delay the animation for better view.

After creating birth class, you have to define birthday class that extends JFrame and represents the main frame of the application or program. Here, create an instance of birth class. In the constructor of birthday class, add the instance of birth class like add(b). Now, sets the title, size, background color and default close operation of frame using setTitle(), setBounds(), setBackground() and setDefaultCloseOperation() methods respectively. To make the frame visible use setVisible() method.

The main method is the entry point of the program. In the main method, creates an instance of the birthday class, which triggers the creation of the GUI and frame. Lastly, play an audio file named “original-happy-birthday-song-1-48291.wav” using the Java Sound API.

How run the program :

Before you begin, make sure you have Java Development Kit (JDK) installed on your pc. You have to also install a Java Integrated Development Environment (IDE) like Eclipse, VS Code or NetBeans. In this article, I have used the VS Code to run my program. If you want to run java program in VS Code follow my link.

First, open VS Code and create a new Java project and name it “Birthday_app”. In “Birthday_app” directory, create a java file with a .java extension like “birthday.java”. Now copy the below source code and paste in the “birthday.java” file and save it. After that, you can run wishing happy birthday program on your pc.
Note that, java file name and the main public class name must be same.

Source code of happy birthday program by java :

The below code is the source code of wishing happy birthday program using the swing library by java language. You can copy the source code and use in your java program.

/*Developed by Puskar Jasu*/
import java.awt.*;
import javax.sound.sampled.*;
import java.io.*;
import java.util.*;
import javax.swing.*;

class birth extends JPanel {
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Random r = new Random();
        ImageIcon im[] = new ImageIcon[5];
        im[0] = new ImageIcon("P1080778.JPG");
        im[1] = new ImageIcon("P1080779.JPG");
        im[2] = new ImageIcon("P1080781.JPG");
        im[3] = new ImageIcon("P1080783.JPG");
        Image img1 = im[0].getImage();
        Image img2 = im[1].getImage();
        Image img3 = im[2].getImage();
        Image img4 = im[3].getImage();
        int c1 = r.nextInt(255);
        int c2 = r.nextInt(255);
        int c3 = r.nextInt(255);
        int x = r.nextInt(600);
        int y = r.nextInt(600);
        setBackground(new Color(c1, c2, c3));
        setForeground(new Color(c2, c1, c3));
        g.setColor(new Color(c3, c2, c1));
        int x1 = r.nextInt(100);
        int y1 = r.nextInt(100);
        g.drawLine(x, y, x + x1, y + y1);
        g.drawLine(x, y, x - x1, y + y1);
        g.drawLine(x, y, x + x1, y - y1);
        g.drawLine(x, y, x - x1, y - y1);
        g.drawImage(img1, 50, 60, 150, 150, this);
        g.drawImage(img2, 350, 60, 150, 150, this);
        g.drawImage(img3, 50, 290, 150, 150, this);
        g.drawImage(img4, 350, 290, 150, 150, this);
        g.setFont(new Font("Elephant", Font.ITALIC, 35));
        g.drawString("TO   ARPITA", 20, 50);
        g.drawString("HAPPY    BIRTHDAY", 80, 260);
        g.drawString("FROM   PUSKAR", 200, 500);
        try {
            Thread.sleep(500);
        } catch (Exception e) {
        }
    }
}

public class birthday extends JFrame {
    birth b = new birth();

    birthday() {
        add(b);
        setTitle("HAPPY BIRTH DAY OF ARPITA");
        setBounds(200, 100, 600, 600);
        setBackground(Color.red);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String args[]) {
        new birthday();
        try {
            AudioInputStream audioInputStream = AudioSystem
                    .getAudioInputStream(new File("original-happy-birthday-song-1-48291.wav").getAbsoluteFile());
            Clip clip = AudioSystem.getClip();
            clip.open(audioInputStream);
            clip.start();
        } catch (Exception ex) {
            System.out.println("Error with playing sound.");
            ex.printStackTrace();
        }
    }
}

Output :

When you run the above program on your pc, you can see the output of wishing happy birthday program using java graphics programming language. You can also see the output of the happy birthday program by java in my YouTube channel.

Conclusion :

Finally, You have created a basic Java graphics program to wish someone a happy birthday. This is a very simple Java graphics program for wishing someone a happy birthday. You can further customize a happy birthday Java program by adding more graphics or animations depending on your programming skills and creativity. Thank you for visiting my site. Happy birthday wishes to all.

Scroll to Top