Animation Program in java
Create an animation with a single line, which changes its position in a
clock-wise direction. This line should produce an effect of a spinning line. In
the same example give option for clock-wise or anti-clock-wise spinning. Also provide
start and stop buttons to start and stop the animation. Demonstrate the
animation on the screen.
Packages
used:
1.
javax.swing.* : Provides a set of
"lightweight" (all-Java language) components that, to the maximum
degree possible, work the same on all platforms.
2.
java.awt.* : Contains all of the classes for
creating user interfaces and for painting graphics and images.
3.
java.awt.event.* : Provides interfaces and classes for
dealing with different types of events fired by AWT components.
4.
java.lang.* : Provides classes that are
fundamental to the design of the Java programming language.
Classes used:
- Graphics : Provides classes that are
fundamental to the design of the Java programming language.
·
JButton : An implementation of a "push"
button.
·
Thread : A thread is a thread of execution
in a program. The Java Virtual Machine allows an application to have multiple
threads of execution running concurrently.
Logical flow of the program:
·
A class
that extends JApplet and implements Runnable contains a getContentPane
container JPanel which holds two buttons.
·
Since
this class implements Runnable interface it defines two methods:-init() and
run().
·
init() method initializes the thread.
·
Two
methods are defined for clockwise spinning line and an anticlockwise spinning
line.
·
Each
method defines a variable “theta”.By randomly obtaining the values of x & y
using Math.random() method,lines are drawn clock-wise as well as anticlockwise.
·
repaint() method is called whenever the
JApplet is refreshed.
Important notes :
·
In all
swing program the package that should be imported is “javax.swing.*”.
·
If in the
program JApplet is inherited then
the “init()” method is used to
initialize the applet.
·
If in the
program JFrame is inherited then the
inheriting class’s constructor initializes the swing application.
1.
If
super() method is called ,it should be the first line inside the constructor.
2.
Frame’s
visibility and size must be set.
0 comments: