Results 1 to 3 of 3
  1. #1
    Leospaceman is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default Help with Image Rotating?

    I'm a novice please be gentle ;D

    I need to rotate an image about its center, by default it rotates around the top left corner.
    The image is 30x30 so ideally, I would like it to rotate about its 15x15 point.
    Right now I'm manually putting in an angle (in radians) but I would like to eventually use the inverse tangent of the mouse's x and y positions on the screen to find the angle, so the image is always facing the mouse pointer.


    Java Code:
    public class RotateImage extends JPanel{
    	
        // Declare an Image object for us to use.
        Image image;
        
        int mousex = (MouseInfo.getPointerInfo().getLocation().x);
    	int mousey = (MouseInfo.getPointerInfo().getLocation().y);
        double pos = (mousex/mousey);
        
        double angle = 1.57;
        double arctan = Math.atan(Math.toRadians(angle));
        // Create a constructor method
        public RotateImage(){
           super();
           // Load an image to play with.
           image = Toolkit.getDefaultToolkit().getImage("Player2.png");
        }
        
            public void paintComponent(Graphics g){
             Graphics2D g2d=(Graphics2D)g; // Create a Java2D version of g.
             g2d.translate(400, 400); // Translate the center of our coordinates.
             g2d.rotate(angle);  // Rotate the image by 1 radian.
             g2d.drawImage(image, 0, 0, 200, 200, this);
        }
    
    
    
    	public static void main(String arg[]){
           JFrame frame = new JFrame("RotateImage");
           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           frame.setSize(800,800);
           frame.setLocationRelativeTo(null);
           RotateImage panel = new RotateImage();
           frame.setContentPane(panel);  
           frame.setVisible(true);  
        }
    }
    -Thanks!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    The Graphics2D object has a rotate method that takes 3 doubles, the first for the angle in radians, and the next two for the coordinates of the center of rotation. It uses an AffineTransform to do the rotation. Why not try it?

    Also, you will need to add a MouseMotionListener to your JPanel to get the mouse's position as it moves. In the mouseMoved method, I'd calculate the angle of the mouse to the center point, use that to set a non-static class double field, say called theta, call repaint() on the JPanel, and have the paintComponent method use that theta in its rotation method call.
    Last edited by Fubarable; 05-31-2011 at 03:33 AM.

  3. #3
    Leospaceman is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default

    Thanks that helped, I realized the second 2 integers in g2d.drawimage were resizing my image, messing up all my measurements haha, the first 2 also set the coordinates relative to g2d.translate so I was able to tweak the position to rotate properly =D.

    here's the cleaned up code:

    Java Code:
    public class RotateImage extends JPanel{
    	
        Image player;
        /*
        int mousex = (MouseInfo.getPointerInfo().getLocation().x);
    	int mousey = (MouseInfo.getPointerInfo().getLocation().y);
        double pos = (mousex/mousey);
        */
        double angle = Math.toRadians(90);
            
        public RotateImage(){
           super();
           player = Toolkit.getDefaultToolkit().getImage("Player2.png");
        }
    
            public void paintComponent(Graphics g){
             Graphics2D g2d=(Graphics2D)g; 
             g2d.translate(400, 400); // define 0,0 
             g2d.rotate(angle, 0, 0);
             g2d.drawImage(player, -15, -15, 30, 30, this);//set location relative to translate, set size
        }
    
    
    
    	public static void main(String arg[]){
           JFrame frame = new JFrame("RotateImage");
           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           frame.setSize(800,800);
           frame.setLocationRelativeTo(null);
           RotateImage panel = new RotateImage();
           frame.setContentPane(panel);  
           frame.setVisible(true);  
        }
    }
    I'll try to get it aligned with the mouse next, thanks again

Similar Threads

  1. Rotating an image 90 Degrees
    By cvillejin in forum New To Java
    Replies: 4
    Last Post: 04-03-2011, 08:56 PM
  2. Rotating Buffered Image distorts image
    By VortexSpin in forum Java 2D
    Replies: 1
    Last Post: 02-13-2011, 06:54 AM
  3. Rotating an image
    By lackofcolor in forum Java 2D
    Replies: 3
    Last Post: 02-28-2009, 12:54 AM
  4. Rotating Image?
    By sciguy77 in forum Java Applets
    Replies: 9
    Last Post: 02-17-2009, 02:47 AM
  5. Rotating and flipping an image in SWT
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-02-2008, 09:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •