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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:38:35+00:00 2026-06-13T14:38:35+00:00

Using this code as an example, how would I add a velocity component to

  • 0

Using this code as an example, how would I add a velocity component to an object?

class CircleFrame extends JPanel {
    static int x = 20;
    static int y = 20;
    int radius = 20;

    CircleFrame() {
        setSize(400,400);
    }

    @Override
    protected void paintComponent(Graphics g) {
        repaint();
        g.drawOval(x,y,radius,radius);
    }
}

This code allows the user to take control of a small circle in a JFrame using the left, right, up and down arrow keys. How can I add in velocity? For instance, while they are moving it to the right, the xvelocity becomes larger. Once they stop moving it, the velocity slowly decreases until the object has stopped. I was thinking threads are the answer here but I still don’t understand them much.

  • 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-06-13T14:38:37+00:00Added an answer on June 13, 2026 at 2:38 pm

    Each move is a unique key press? I.e. if i hold the key down, i only get one move? If that’s the case and you just want to roughly calculate velocity, then you can call
    System.currentTimeMillis() on each of your press event handlers, keep it in a variable somewhere, and call it again on the next, then just compute the total displacement divided by the difference in time… if this is what you’re trying to do comment and i’ll write some code.

    Another (more likely) possibility is you want the velocity to increase while they hold the key down… in that case, figure out the acceleration you want to impose.. use a second thread to increase the velocity and notify that action to decelerate to a stop when the keyUp event occurs.

    int accel = 1;
    boolean accelerating = false;
    public void keyPressed(KeyEvent e)
    {
       ...
       accelerating = true;
       new Thread() //anonymous inner class 
       { 
         public void run()
         {
           while (accelerating) { x += vel; vel += 1; }
           //no longer accelerating
           x = 0; //or decelerate gracefully with a similar loop as accel.
         }
      }.run();
    }
    public void keyUp(KeyEvent e) //or however you get a key release event
    {
       accelerating = false;
    }
    

    modify this a bit to adapt it to your up/down/left/right, and figure out the acceleration that makes sense for you. you might even consider only accelerating up to a limit, and not any past a certain velocity

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

Sidebar

Related Questions

I'm using this simple node.js code example but I don't understand why FireBug doesn't
I'm using this plugin: http://www.jeremymartin.name/projects.php?project=kwicks And my code follows this example: http://www.jeremymartin.name/examples/kwicks.php?example=7 I'm using
I saw this C# using statement in a code example: using StringFormat=System.Drawing.StringFormat; What's that
This is the example code, I'm using these functions for a program, but scanf
Using this code var html='<div class=somediv></div>'; var target=document.getElementById('contentArea'); target.appendChild(html); I'm getting Uncaught Error: NOT_FOUND_ERR:
I'm using this code to add an item to the code window right click
I am using the apple example code SpleakHere . in this code there is
I was browsing over the following code example: public class GenericTest { public static
Using this code, i am able to send a notification to my own device.
Using this code to zip a folder and it works perfect on small files

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.