• We just launched and are currently in beta. Join us as we build and grow the community.

Bouncing Ball Animation in JAVA using MutiThreading

Hots

Realtime Debugging Master
H Rep
0
0
0
Rep
0
H Vouches
0
0
0
Vouches
0
Posts
64
Likes
177
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 500 XP
TUTORIAL NO 8




In this tutorial you will learn:
1.Multi Threading
2.Inner Classes
3.Swing Animations
4.JAVA awt
5.JAVA swing
6.Adapters
A lot of newbies ask this question how to make a bouncing ball animation in java using graphic elements so I decided to address this problem and today I am going to teach you how to make a very simple bouncing ball animation in java.
In this tutorial we will be working in JAVA SWING. The first thing that we are going to do is setting up a JPanel and adding the required Components after that adding that JPanel in the JFrame.
Basic step:
Download and install ECLIPSE and set up a JAVA PROJECT. Then create a new class and name it Animation. Then follow the steps

1.IMPORT STATEMENTS
First of all write these import statements in your .java file

  1. import

    javax.swing.*

    ;
  2. import

    java.awt.*

    ;

Here we require swing import for drawing the image and including the working with the animation by including the paintComponent.

2. SETTING UP THE PANEL CLASS
  1. class

    MoveBall extends

    JPanel

    {

  2. Image

    football =

    new

    ImageIcon

    (

    "football//football.png"

    )

    .getImage

    (

    )

    ;

    //including the football

  3. int

    xpos =

    10

    , ypos =

    50

    ;

    //for x and y coordinates of football
  4. int

    xinc =

    2

    , yinc =

    2

    , xtop =

    20

    , ytop =

    20

    , xbot =

    900

    , ybot =

    450

    ;
  5. moveball b =

    new

    moveball(

    )

    ;

    //making an object of inner class to pass on

First of all we will include the football image from our football folder and save it in the object which we named football. Then we created the variables for the x and y coordinate so that we can move the football here and there. After that we declared increment variable and set it to certain values depends on how much we want to increment on each collision. After that set the xtop ,ytop variables to certain values which will be our minimum values of x and y coordinates and then we set the xbot and ybot values which will be our maximum values for x and y coordinates. After that we just made an object of our inner class which is extending from thread class to implement multi threading.

3. WRITING THE CONSTRUCTOR
  1. MoveBall(

    )

    {
  2. Thread

    mb =

    new

    Thread

    (

    b)

    ;
  3. mb.start

    (

    )

    ;
  4. }

    //end constructor

In the constructor we simply started a thread by making an instance of thread class and passing the inner class object to it.

4. WRITING THE PAINTCOMPONENT
  1. public

    void

    paintComponent(

    Graphics

    g)

    {
  2. super

    .paintComponent

    (

    g)

    ;
  3. Graphics2D

    g2d =

    (

    Graphics2D

    )

    g;
  4. setBackground(

    Color

    .WHITE

    )

    ;

  5. g2d.drawImage

    (

    football,xpos,ypos,null

    )

    ;

    //drawing the football

  6. }

Here the paint component is quite simple we first called the super class paintComponent to include its features in our base class and then we type casted our graphics object graphics 2D object so that we can work with swing elements. After that we set our background color and simply draw the football image.

5. WRITING THE INTERNAL CLASS
  1. public

    class

    moveball extends

    Thread

    {
  2. public

    void

    move(

    )

    {
  3. if

    (

    xpos >

    xbot)

    {
  4. xinc =

    -

    10

    ;

    //whenever xcoordinate is greater than maximum bounce back
  5. }
  6. else

    if

    (

    xpos <=

    xtop)

    {
  7. xinc =

    10

    ;

    //otherwise set increment to 10
  8. }
  9. if

    (

    ypos >

    ybot)

    {
  10. yinc =

    -

    10

    ;
  11. }
  12. else

    if

    (

    ypos <=

    ytop)

    {
  13. yinc =

    10

    ;
  14. }
  15. xpos +=

    xinc;
  16. ypos +=

    yinc;
  17. }



Now in the internal class we make a move function to move the football and bounce against the edges. First of all we compare the current position with the lower limit if it is greater than the lower limit than we set the increment to -10 other wise we check if current position is less than max position if this is the case then we set the increment value to 10. After that we check the current position of y and compare it with the minimum limit of y if it is greater then we move upwards by decrementing 10 and otherwise we check if current position is less than or equal to minimum y coordinate if that is the case then we increment current position of y by 10. After that we simply add the current position with the values of x and y increment which are set according to the above conditions.
  1. public

    void

    run(

    )

    {

  2. while

    (

    true

    )

    {
  3. move(

    )

    ;
  4. try

    {
  5. Thread

    .sleep

    (

    20

    )

    ;
  6. }

    catch

    (

    Exception

    e)

    {
  7. }
  8. repaint(

    40

    )

    ;
  9. }
  10. }
  11. }
  12. }

    //end panel

Now we write the implementation for run function. In this function we created an infinite loop and call the move function to move the football according to the conditions. After that we simply sleep our animation for 20ms and then repaint after 40 to slow down the speed of our animation.

6. WRITING THE PUBLIC CLASS:
  1. public

    class

    BallThread extends

    JFrame

    {
  2. MoveBall mb =

    new

    MoveBall(

    )

    ;
  3. BallThread(

    )

    {
  4. add(

    mb)

    ;
  5. setTitle(

    "Moving Football Animation"

    )

    ;
  6. setDefaultCloseOperation(

    JFrame

    .EXIT_ON_CLOSE

    )

    ;
  7. setSize(

    950

    , 550

    )

    ;
  8. setVisible(

    true

    )

    ;
  9. }
  10. public

    static

    void

    main(

    String

    args[

    ]

    )

    {

  11. new

    BallThread(

    )

    ;
  12. }
  13. }

In the end we write our public class which is extending from JFrame and we make a panel object in it and add this object to the frame. After that we set the title ,close operation , size and visibility and write the main function in the end. Now our application is complete.

Output:
output_30.png



Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,526

323,535

Top