"Happy Valentine Day" Animation Program Using Eclipse IDE

in #utopian-io7 years ago (edited)

Hello and Happy Valentine everyone. This is a short Java program written using Eclipse IDE. This program uses a single classe to display Happy Valentine message on the output Frame screen. The program run with dialog boxes and a JFrame. The first dialog box is used to prompt a user to input his name while the second dialog box shows a message before the Frame appears.

Let's take a look at the program from source code. Some screenshots are here to help you understand this faster and better.

val 1.jpg

val 2.jpg

This program shows how a user can make use of a single class to write a program that involves GUI dialog boxes and JFrame. Let's run the program and see how it works.

val 3.jpg
As the program is run, it first prompts a user to input his/her name.

val 4.jpg
A message dialog then shows up telling the user that it's gotten some sort of surprise.

val 5.jpg
Behold, the surprise shows up and it's very awesome. The "Happy Valentine Day" is an animation texts that miove around the edges on the JFrame screen.

See some screenshot of its movement around the screen below.

val 6.jpg

val 7.jpg

Source code on GistHub

val 8.jpg

val 9.jpg

Now, let me explain each line of the code.

Line 1. Package name.

Line 2. Import statement which introduces the classes from awt package to the program.

Line 3. Import statement which introduces the classes from swing package to the system.

Line 5. The Valentine class contains Java code to draw the program interface and do animation.

Line 6. Declare a JLabel lblText.

Line 7. Valentine here is the constructor of theValentine class. When an object of Valentine class is created, the code in this constructor is executed to draw program interface and do animation.

Line 8. This is used to set the frame window title. In this program, the set title is "Love Celebration".

Line 9. This sets the layout of the frame window.

Line 10. This sets a background image and includes the location source for the background image.

Line 11. Creates the JLabel object LblText. This object contain the text "Happy Valentine Day, Honey" to be animated.

Line 12. Defines properties of the JLabel object lblText giving it a width of 600 and height 70.

Line 13. Defines the size of the frame as width 800 and height 500.

Line 14. Sets the frame to a resizable manner. User can choose to minimise or maximise the frame.

Line 15. This closes the frame window when the user clicks the close button.

Line 16. Add the JLabel lblText object to the frame window

Line 17. This ensures that the frame window is visible.

Line 18. Invokes the doValentine method to animate the lblText object.

Line 19. This is the closing brace for the Valentine construction.

Line 22. The doValentine method contains code to animate the JLabel object lblText.

Line 23. Declares the initial value of X.

Line 24. Declares the initial value of Y.

Line 25 - 39. A while is used to change the location of the object by increasing the values of x-axis and y-axis of the object by 20. When these values are nearly equal to the width and height of the frame window, they are reset to the initial values (x=1,y=5). The sleep method of the Thread class is invoked to delay the action of the lblText object (chaning from one location to another location). We delay the action by 200 milliseconds (sleep(200)).

Line 40. Here is the HappyVal class main method to start the program.

Line 44. An input dialog that prompts the user to enter his name.

Line 45. A message dialog box that shows "May I surprise you" alongside the name entered by the user.

Code

package FirstPrograms;
import java.awt.*;
import javax.swing.*;
class  Valentine extends JFrame{
JLabel lblText;
Valentine(){
setTitle("Love Celebration");
setLayout(new BorderLayout());
setContentPane(new JLabel(new ImageIcon("C:\\Users\\TeeKingTV.Com\\Downloads\\love2.gif")));
lblText=new JLabel("<html><p style='color: #ffffff; font-family:algerian; font-size:30pt'>Happy Valentine Day, Honey!</p></html>");
lblText.setSize(600,70);
setSize(800,500);
setResizable(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(lblText);
setVisible(true); 
>doValentine();
}
public void doValentine(){
int x=1;
int y=5;
try{
while(x<800){
lblText.setLocation(x,y);
Thread.sleep(200);
x+=20;
y+=20;
if(x>800) x=1;
if(y>500) y=5;
}       
}catch(InterruptedException ie){System.out.println("Interrupted...");}
}
}
public class HappyVal{
public static void main(String args[]){
String name = JOptionPane.showInputDialog(null, "Enter your name");
JOptionPane.showMessageDialog(null, "May I surprise you, " + name + "?");
new Valentine();
}  
}

Full source code is available on Github. Link here



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Your contribution cannot be approved because it does not follow the Utopian Rules.

This has been already submitted and rejected. You removed your previous entry.

You will be banned for such behabiour.

You can contact us on Discord.
[utopian-moderator]

I removed my precious entry to make corrections on why it was rejected. I have no other intention aside that