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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:41:27+00:00 2026-05-27T10:41:27+00:00

Im trying to get a rectangle to move from a fixed location, to a

  • 0

Im trying to get a rectangle to move from a fixed location, to a click point in my game. I have it working to the point that im getting good coordinates from the e.getX and e.getY methods in the mouse listener class. I also have the rectangle moving in straight lines to the side or up and down by increasing and decreasing the positionX and Y values but i need a way to move it to any point that i click on, basically i need to set the change in x and y. The way i was thinking is to compute the rise and run of the line from the click points and the start point and then set the x and y values to the rise and run. If i do it this way, i need a way to decrease the rise and run values down to their lowest terms so it moves fluidly. Or if there is a way im not thinking of that you can offer any guidence that would be helpfull.

  • 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-27T10:41:28+00:00Added an answer on May 27, 2026 at 10:41 am

    How are you drawing it at the moment? Below is a method I used to shoot bullets in a little game that I made/really should finish sometime.

    This is by no means all the code you will need, and in some places there are better ways to do things (which I’m sure people will start to tell me in the comments section after 🙂 ) but it should give you a basis to work from.

    The one thing missing from this is regulating the speed at which it repaints (fps), sorry but I can’t remember the simple way to do this right now (need to get back to my actual job!) Hope it helps.

    The barebones of it will be something like this:

    //these set out the current position of your rectangle
    double recX, reYy, recH, recW;
    
    //this gives something to manage speed
    int speed;
    
    public void paintComponent(Graphics g) {
        g.drawRectangle(recX,rexY,recH,recW);
    }
    
    //here we work out the movement
    public void clickedScreen(double x, double y){
        double newX = x;
        double newY = y;
        //calculate the speed to move at
        vX = newX - recX;
        vY = newY - recY;
        //get the distance
        length = Math.sqrt((v1*v1)+(v2*v2));
        //make it a unit vector
        v1 = v1/length;
        v2 = v2/length;
    }
    
    public moveRec(){
        recX = recX+(v1*speed);
    recY = recY+(v2*speed);
    }
    
    while(true) {
        moveRec();
        repaint();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to get this example working from http://www.munna.shatkotha.com/blog/post/2008/10/26/Light-box-effect-with-WPF.aspx However, I can't seem to get
Trying to get comfortable with jQuery and I have encountered some sample code that
We are trying to get a Rectangle that represents the exact* boundary of the
I'm trying get values from a GridView using the following code: foreach (GridViewRow row
Trying to get parameters from a PUT request using HttpServlet#doPut: public void doPut(HttpServletRequest request,
I'm trying to have a WPF canvas with rounded rectangles on that I can
I'm trying to make an infinite round rectangle, but I cant seem to get
I have the coordinates of the top left point of a rectangle as well
I am trying to make a Texture2D from a Bitmap. I have Texture2D BitmapToTexture(Bitmap
Ok I've been trying to rotate a Rectangle that sits on top of an

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.