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

  • Home
  • SEARCH
  • 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 8368391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:22:45+00:00 2026-06-09T13:22:45+00:00

So far I have a java app where I draw a circle(player) and then

  • 0

So far I have a java app where I draw a circle(player) and then draw a green rectangle on top(gun barrel). I have it so when the player moves, the barrel follows with it. I want it to find where the mouse is pointing and then rotate the barrel accordingly. For an example of what I mean look at this video I found http://www.youtube.com/watch?v=8W7WSkQq5SU See how the player image reacts when he moves the mouse around?

Here’s an image of what the game looks like so far:

My Progress

So how do I rotate it like this? Btw I don’t like using affinetransform or Graphics2D rotation. I was hoping for a better way. Thanks

  • 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-09T13:22:46+00:00Added an answer on June 9, 2026 at 1:22 pm

    Using the Graphics2D rotation method is indeed the easiest way. Here’s a simple implementation:

    int centerX = width / 2;
    int centerY = height / 2;
    double angle = Math.atan2(centerY - mouseY, centerX - mouseX) - Math.PI / 2;
    
    ((Graphics2D)g).rotate(angle, centerX, centerY);
    
    g.fillRect(...); // draw your rectangle
    

    If you want to remove the rotation when you’re done so you can continue drawing normally, use:

    Graphics2D g2d = (Graphics2D)g;
    AffineTransform transform = g2d.getTransform();
    
    g2d.rotate(angle, centerX, centerY);
    
    g2d.fillRect(...); // draw your rectangle
    
    g2d.setTransform(transform);
    

    It’s a good idea to just use Graphics2D anyway for anti-aliasing, etc.

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

Sidebar

Related Questions

So far I have my app taking a picture AndroidCamera.java using the camera and
I have a simple Java SWT app in Java so far but the weird
I have a java server app that download CSV file and parse it. The
I have a java app that creates a socket to talk to a server
I am new to Android and Java. I have constructed an app using HTML/Javascript
So far I have my app taking a picture creating a new folder on
I am attempting to incorporate admob ads in my app. So far I have
I have both java and .net applications running on an app server using Microsoft
I have a working Java app that can find the most recently created file
I have a loading screen which closes after 5 secounds and then the app

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.