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 6625067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:43:45+00:00 2026-05-25T21:43:45+00:00

I have an image(ball.gif) that moves horizontally, the problem is how could I make

  • 0

I have an image(“ball.gif”) that moves horizontally, the problem is how could I make the ball bounce when it reach the end of the size of the panel? I know this is not really difficult but I’m just a little bit confuse on how to do it.

Could someone help me about this matter?

This what I’ve tried so far :

public void paint(Graphics g)
{
    super.paint(g);
    Graphics2D g2d = (Graphics2D)g;
   g2d.drawImage(ball, x, y, this);
   Toolkit.getDefaultToolkit().sync();

    g.dispose();
}


public void cycle()
{



    x += 1;
    y += 0;
    if (x >240)
    {


        x = 10;
        y = 10;
    }


}


public void run()
{

    long beforeTime, elapsedTimeDiff, sleep;

    beforeTime = System.currentTimeMillis();

    while (true)
    {

        cycle();
        repaint();

        elapsedTimeDiff = System.currentTimeMillis() - beforeTime;
        sleep = DELAY - elapsedTimeDiff;
        System.out.println(sleep);

        if (sleep < 0)
        {
            sleep = 2;
        }
        try
        {
            Thread.sleep(sleep);
        }
        catch (InterruptedException e)
        {
            System.out.println("interrupted");
        }

        beforeTime = System.currentTimeMillis();
    }
}
  • 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-25T21:43:45+00:00Added an answer on May 25, 2026 at 9:43 pm

    First, you need to hold your velocity in a field instead of hardcoding it:

    private static final int RIGHT_WALL = 240;
    private int x, y;
    private int xVelocity = 1;
    
    
    //...
    x += xVelocity;
    y += 0;  //change this later!
    

    Then when you do your bounds checking, flip your xVelocity:

    //...
    if ( x > RIGHT_WALL ) {
       x = RIGHT_WALL;
       xVelocity *= -1;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an image which moves in a random direction. My problem is everytime
I have image Array with two images out of that first image its showing
I have image map that can I move, but this map will be so
I have image and txt file of same size say 200 KB. Now i
i have a div that it have image content .i wanna implement animation when
I have a sprite(image of a ball) I can move it using touch and
I have a UIImageView that I have resized to fit a small ball. The
I have a character in an iPod Touch game I am making that moves
I have a ball image with 20 pixels width and 20 pixels height. I
I have a ball sprite that changes it's texture when a certain scenario occurs.

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.