Friday, April 12, 2013

Text Animation program

This is a simple Text Animation program. When the program starts the text "Happy New Year" moves from different locations on the frame window. To make the text move, we a need a label object to display this text. Then locations of the label object are changed by using a while loop.

TextAnimation source code:

1 import java.awt.*;
2 import javax.swing.*;

3 class  Animation extends JFrame{
4 JLabel lblText;
5 Animation(){
6 setTitle("Text Animation");
7 setLayout(null);
8       lblText=new JLabel("<html><p style='color: #ff00ff; font-size:20pt'>Happy New Year</p></html>");
9       lblText.setSize(200,50);
10 setSize(800,500);
11 setResizable(false);
12 setDefaultCloseOperation(EXIT_ON_CLOSE);
13 add(lblText);
14   setVisible(true);
15 doAnimation();
}

16   public void doAnimation(){
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...");}
}

}

17 public class TextAnimation{

public static void main(String args[]){
      new Animation();
 
}


}

Text Animation program in Java


Code Explanation:

1 Introduce classes in awt package to the program.
2 Introduce classes in swing package to the program.
The two packages are required in drawing the program interface. In this program, we need only one frame window and a label to show the text to be animated.
3 The Animation class contains Java code to draw the program interface and do animation.
4 Declare a JLabel lblText.
5 Animation is the constructor of the Animation class. When an object of Animation class is created, the code in this constructor is executed to draw program interface and do animation.
6 Set frame window title.
7 Set the layout of the frame window to null so that you can customize the location of the components on it.
8 Create the JLabel object LblText. This object contain the text "Happy New Year" to be animated.
9 Define size of the JLabel object lblText (width=200, height=50).
10 Define the size of frame window (width=850, height=450).
11 Disable frame resizing.
12 Allow the frame window to close when the user clicks the close button.
13 Add the JLabel lblText object to the frame window.
14 Make sure the frame window visible.
15 Invoke the doAnimation method to animate the lblText object.
16 The doAnimation method contains code to animate the JLabel object lblText. The animation on the object is performed by changing its location (x,y). 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)).
17 The TextAnimation class has the main method to start the program.

3 comments:

  1. Like your way of seeing things! Still you may do some things to expand on it. Thanks for sharing with us!

    HIMT

    ReplyDelete
  2. Take a gander at workmanship classes at your neighborhood junior college in the event that you need to enhance your drawing aptitudes. In the event that you need an easy route to draw a toon me confront you should simply locate the correct PC download program that does the majority of the diligent work for you. There are a few unique items that draw toons and most are free. this contact form

    ReplyDelete
  3. leaving the OS to do the last modification as the application is hurrying to mean the PC the summons. Java

    ReplyDelete