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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:28:53+00:00 2026-06-02T13:28:53+00:00

I am implementing a java swing application which has a JPanel that serves as

  • 0

I am implementing a java swing application which has a JPanel that serves as a drawing surface. The surface renders (active) different elements. Each element has its own shape and an affine transform which is used for rendering and collision detection (each element is drawn with local coordinates and than transformed with the affine transform – like opengl).

The elements can be moved and rotated on the surface (this is done through transforms). Every time a transform is applied a Area object is created with the shape and the transformation (for accurate collision detection).

The problem is when I rotate the element (for 45 degrees) and then move it by 10 px. When I move it the element moves in the rotated direction which I don’t want.

Is there any simple way I can overcome this?
(If my description isnt enough I’ll post some example code).

EDIT:

 class Element
 {
     private AffineTransform transform = new AffineTransform();
     private Shape shape = new Rectangle(0,0,100,100);       
     private Area boundingBox;       

     public void onMouseDrag(MouseEvent e)
     {
        translate(dx,dy); // dx,dy are calculated from event
     }

     public void onMouseMove(MouseEvent e)
     {
        rotate(Math.atan2(dx/dy); // dx,dy are calculated from event
     }

     public void translate(int dx,int dy)
     {
        transform.translate(dx,dy);
        boundingBox = new Area(shape);
        boundingBox.transform(transform);
     }

     public void rotate(double angle)
     {
         transform.rotate(Math.toRadians(angle));
         boundingBox = new Area(shape);
         boundingBox.transform(transform);
     }

     public void draw(Graphics2D g2d)
     {
         g2d.setTransform(transform); 

         g2d.draw(shape); 
        ...
      }

 } 
  • 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-02T13:28:59+00:00Added an answer on June 2, 2026 at 1:28 pm

    I’ve modified your code by giving Element position and orientation fields, which you might want to expose through getters/setters (probably defensively copying the Point instance before returning/setting it). updateTransform() simply re-builds the AffineTransform based on the Element‘s current position and orientation.

    public class Element {
    
        private Point position = new Point();
        private float orientation;
    
        private AffineTransform transform = new AffineTransform();
        private Shape shape = new Rectangle(0, 0, 100, 100);
        private Area boundingBox;
    
        public void onMouseDrag(MouseEvent e) {
            translate(dx, dy);
        }
    
        public void onMouseMove(MouseEvent e) {
            rotate(Math.atan2(dx / dy));
        }
    
        public void translate(int dx, int dy) {
            position.translate(dx, dy);
            updateTransform();
        }
    
        public void rotate(double angle) {
            orientation += Math.toRadians(angle);
            updateTransform();
        }
    
        private void updateTransform() {
            transform.setToIdentity();
            transform.translate(position.x, position.y);
            transform.rotate(orientation);
            // ... update bounding box here ...
        }
    
        public void draw(Graphics2D g2d) {
            g2d.setTransform(transform);
            g2d.draw(shape);
        }
    
    }
    

    Also, consider the following approach:

    • Change draw(Graphics2D) to draw(Graphics2D, AffineTransform transform)
    • Create AffineTransform Element.getCompositeTransform() which builds the current AffineTransform and returns it (like updateTransform()).
    • Remove the transform field from Element.
    • In your render method, do something like:

    .

    for (Element element : elements) {
        AffineTransform transform = element.getCompositeTransform();
        element.draw(graphics, transform);
    }
    

    Why? This gives you the flexibility to control Element‘s drawing transform externally. This is useful if you’ll build a node graph (like when an Element can have Element children) and you’ll recursively render the graph, concatenating transforms. Btw, this approach is pretty much standard in 3D programming.

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

Sidebar

Related Questions

So I've got a JPanel implementing MouseListener and MouseMotionListener : import javax.swing.*; import java.awt.*;
I am implementing some keyboard code for an existing java swing application, but I
I am implementing a Java EE based Hospital Management System that has a web
(Using Java) I am implementing a generic class which is a B-Tree. When the
I am implementing a basic board game in Java and that I am having
Suppose that I am implementing a remote proxy in Java to an object that
I have created in my Java Swing application a main window with a JButton.
lets say i have a method which has two parameters. i have been implementing
I'm implementing a Java interface with a lot of methods with Object parameters, which
I am programming in Java, using Swing. I am currently working with an application

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.