Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6036459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:55:39+00:00 2026-05-23T05:55:39+00:00

i have been trying to make a bouncing ball animation.I have got everything right

  • 0

i have been trying to make a bouncing ball animation.I have got everything right except for one thing.
The ball goes off the screen once it hits the lower deck of the frame and the right hand side of it’s frame.

I have set the condition like :

if( x_Pos > frameWidth - ballRadius)
  // turn the ball back
if( y_Pos > frameHeight - ballRadius)
  // turn the ball back

But the ball disappears for a while when it hits the lower deck and the right deck of the frame.
Here is what happens eventually :
enter image description here

enter image description here

In the second pic ball has hit the lower deck and has disappeared for a while. Why this happens ?

In case this is my complete code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class MovingBall2D extends JPanel{

 int x_Pos=0;
 int y_Pos=30;
 int speedX=1;
 int speedY=1;
 int diameter=30;
 int height=30;
 int frameX=700;
 int frameY=200;
 int radius=diameter/2;

 MovingBall2D() {
  this.setSize(frameX,frameY);
  ActionListener taskPerformer = new ActionListener() {
   public void actionPerformed(ActionEvent ae) {
     if(x_Pos < 0) {
      x_Pos = 0;
      speedX = 1;
     }
     else if( x_Pos >= ( frameX - radius ) ) {
      x_Pos =  frameX - diameter;
      speedX = -1; 
     }
     if(y_Pos < 0) {
      y_Pos = 0;
      speedY = 1;
     }
     else if( y_Pos >= ( frameY - radius ) ) {
      y_Pos =  frameY - radius; 
      speedY = -1;
     } 
     x_Pos = x_Pos + speedX;
     y_Pos = y_Pos + speedY;    
     repaint();
    }
   };
    new Timer(4,taskPerformer).start();
  }

   public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.black);
    g.fillRect(0,0,frameX,frameY);
    g.setColor(Color.red);
    g.fillOval(x_Pos , y_Pos , diameter , height);
   }
  }

   class Main2D {
    Main2D() {
     JFrame fr=new JFrame();
     MovingBall2D o = new MovingBall2D();
     fr.add(o);
     fr.setSize(600,200);
     fr.setVisible(true);
     fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    } 

     public static void main(String args[]) {
        new Main2D();
     }
     }  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T05:55:40+00:00Added an answer on May 23, 2026 at 5:55 am

    MovingBall2D.this.getHeight() is 173(because of panel padding,title,border etc.). That’s why

    Just replace the if conidion like so:

        else if( y_Pos >= ( MovingBall2D.this.getHeight()- radius ) )
        {
            y_Pos = MovingBall2D.this.getHeight() - radius;
            speedY = -1;
        }
    

    The advantage of this is that even if the user resizes the window, the ball will hit the new window boundaries. Do the same for X axis.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.