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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:47:04+00:00 2026-06-16T16:47:04+00:00

I am trying to draw a rectangle in JPanel that would translate and then

  • 0

I am trying to draw a rectangle in JPanel that would translate and then rotate itself to mimic the movement of a car. I have been able to make the rectangle translate and rotate, however it rotates around the origin of (0,0). I’m very pleased that I was able to have the rectangle move and rotate as I am very new to Java GUI, but I can not seem to get how to have the rectangle rotate around itself, because I experimented more with it, and when I initialized the rectangle and rotate it 45 degrees it’s position was changed, which I would assume is the transform matrix that is appended from the rotate method.

I checked through the site on how would I solve this, however I only found how to rotate a rectangle and not on how to rotate and move like the movement of a simulated car. I would presume it is concerning about its transform matrix, but I’m only speculating. So my question is how would I be able to have the rectangle be able to rotate and move around itself and not against a point in JPanel.

Here’s the code that I have come up so far:

public class Draw extends JPanel implements ActionListener {


private int x = 100;
private int y = 100;
private double theta = Math.PI;

Rectangle rec = new Rectangle(x,y,25,25);

Timer timer = new Timer(25,this);

Draw(){
    setBackground(Color.black);
    timer.start();
}

public void paintComponent(Graphics g){
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D)g;     
    g2d.setColor(Color.white);
    rec.x = 100;
    rec.y = 100;
    g2d.rotate(theta);
    g2d.draw(rec);
    g2d.fill(rec);

}

public void actionPerformed(ActionEvent e) {
    x = (int) (x + (Math.cos(theta))*1);
    y = (int) (y + (Math.sin(theta))*1);
    theta = theta - (5*Math.PI/180);
    repaint();
}
  • 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-16T16:47:05+00:00Added an answer on June 16, 2026 at 4:47 pm

    One of two approaches are commonly used:

    • Rotate the graphics context around the center (x, y) of the Shape, as shown here.

      rotate(double theta, double x, double y)
      
    • Translate to the origin, rotate and translate back, as shown here.

      g2d.translate(this.getWidth() / 2, this.getHeight() / 2);
      g2d.rotate(theta);
      g2d.translate(-image.getWidth(null) / 2, -image.getHeight(null) / 2);
      

    Note the apparent reverse order of concatenation in the second example.

    Addendum: Looking more closely at your example, the following change rotates the Rectangle around the panel’s center.

    g2d.rotate(theta, getWidth() / 2, getHeight() / 2);
    

    Also, use the @Override annotation, and give your panel a reasonable preferred size:

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(640, 480);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok I've been trying to rotate a Rectangle that sits on top of an
I am trying to just draw a Rectangle on a PictureBox that is on
I'm trying to draw a rectangle that is rotated 45° behind the text of
I trying to create a JPanel that draws rectangles. The Panel needs to draw
I am trying to draw a rectangle shape in XNA using spritebatch. I have
I have been trying to draw an annulus (ring with thickness) with a transparent
I am simply trying to draw a rectangle inside of a panel using flex4.
I'm new with OpenGLES and I was trying to draw a rectangle with alpha
I'm trying to draw an open rectangle using a Polygon: int[] xPoints = {1,1,3,3};
I am trying to draw a red rectangle on my HTML5 canvas. Here is

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.