Thread: Rotate in java

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    65
    Rep Power
    2

    Rotate in java


    in paint method am drawing two rect but i want to rotate the 1st rect.



    Code:
    ....
    public void paint(...)
    {
    ...
           g2d.translate(180, -50); //rotate
          g2d.rotate(Math.PI/4);
    
    g.drawRect(); //roate
    
    //i think i need to rotate the screen back? 
    //if so than how to do this?
    
    g.drawRect();//dont roate
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2007
    Posts
    765
    Rep Power
    929
    See Graphics2D.setTransform for an example.
    sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    65
    Rep Power
    2
    i tried that but for some reason the x,y postion of g.drawrectl(); is getting missed up.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2007
    Posts
    765
    Rep Power
    929
    Are g and g2d references to the game Graphics2D object? (If so, it would elimination some possible confusion if you only used g2d).

    Can you show a minimal example of the non-working code that uses getTransform/setTransform?
    sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    65
    Rep Power
    2
    here i am painting in Main class. i want to use rotate method in playerObject only.
    Code:
    public class Main ...
    {
       ....	
        public class Display extends JPanel
    	{
    		/**
    		 * SERIALVERSIONUID
    		 */
    		private static final long serialVersionUID = 1L;
    
    		public void paintComponent(Graphics g)
    		{	
    			super.paintComponent(g);      
    			
    				levelsObject.paint(g, topMenuObject);
    				playerObject.paint(g, lifeObject, topMenuObject);
                                    itemObject.paint(g);
                                    ...
    		}
    }
    here i have player class. i want to rotate image inside the if statment only.

    playerClass
    ...
    Code:
    public void paint(Graphics g, Life li, topMenu tm) 
    	{
    		Graphics2D g2d = (Graphics2D) g;
    		AffineTransform saveAT = g2d.getTransform();
    	
    		if(tm.getLife() <= 0)
    		{
    			 
    			g2d.rotate(Math.PI/4);  //rotate
    			g.drawImage(animationPlaneHit.getSprite(), (int)(x),(int)(y), width, height, null);
    		}
    		
    		if(flyUp)
    		{
    			g.drawImage(animationFlyUp.getSprite(), (int)x,(int)y, width, height, null);
    		}
    
    		g2d.setTransform(saveAT); //go back
    	}
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2007
    Posts
    765
    Rep Power
    929
    If you mean that the second sprite is being affected & you don't want that, the getTransform() / setTransform() should be at the beginning and end of that block where the transformation is to be applied.

    If you mean that the x,y coordinate of the sprite is wrong, a rotation transform rotates around the point (0,0). If you want to rotate around the center of your sprite you need to use the three-argument form of rotate & specify the center of the sprite.

    (Or, one trick I've used is to use translate() to first position the graphics object so that (0,0) is where I want to image drawn, then hand the graphics off to a paint routine that draws the image around the point (0,0). That way Player.paint() can be a single-purpose function to draw the player and the class that manages the position of objects can be dedicated to positioning.)
    sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    65
    Rep Power
    2
    If you mean that the second sprite is being affected &
    no only one sprite is beeing affected.

    If you mean that the x,y coordinate of the sprite is wrong,
    yes, this is the problem. sprite coordinate are wrong.


    some thing like this? sorry iam still new at java and i dont really understand transform set.

    Code:
    Graphics2D g2d = (Graphics2D) g;
    		
    		AffineTransform saveAT = g2d.getTransform();
    		g2d.getTransform();
    		g2d.setTransform(saveAT); //rotate
    		if(tm.getLife() <= 0)
    		{
    			 g2d.translate(0, 0);
    			g2d.rotate(Math.PI/4);
    			g.drawImage(animationPlaneHit.getSprite(), (int)(x),(int)(y), width, height, null);
    		}
    		........
    		g2d.getTransform();
    		g2d.setTransform(saveAT); //rotate
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2007
    Posts
    765
    Rep Power
    929
    "g" starts out referring to a coordinate space with 0,0 at the top left. If a box is drawn centered at 5,5 we get this:

    Code:
            0    5
           0+-------
            |    .
            |
            |    .
            |   +-+
           5|. .|.|
            |   +-+
    After a call to "g.rotate( 45 degrees )", g's space is rotated around the point 0,0. (The dotted-doubled lines are the original axis). Now the square appears at 5,5 on the rotated coordinates. (Think of the square sitting on the side of a wheel. If you turn the wheel 90 degrees the square as turn 90 degrees, but it's also now at the bottom of the wheel.)

    Code:
           0 0
            += = = =
           /"\
          / " \
         /  "  \
      5 /   "   \ 5
       /    "    \
        .       .
         
          . x .
           / \
          x . x
           \ /
            x
    To fix that, the square has to be at the center of the rotation. So first do a "g.translate( 5, 5 )" to move the 0,0 point to the center of the square:

    Code:
            += = = = 
            "     
            "
            "     
            "   +-+
            "   |+|----
            "   +-+
                 |
                 |
                 |
    Then do a g.rotate(45 deg). If the square is then drawn around 0,0 it will appear as expected. (I.e. in the example above, if we want to rotate the square without changing its position, it has to be at the center of the wheel.)

    Code:
            += = = = 
            "     
            "
            "    x
            "   / \
            "  x + x
            "   \ /
               / x \
              /     \
             /       \
    There's also a three-argument form of rotate which does the translate, rotate, and then translates back so you can still draw the squares around 5,5 and get the expected result.
    Last edited by OmegaZero; April 23rd, 2013 at 02:17 PM.
    sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    65
    Rep Power
    2
    thanks, got it.

    Code:
          ///paint method
           Graphics2D g2d = (Graphics2D) g;
           AffineTransform saveAT = g2d.getTransform();
        
           g2d.translate(x+(width/2), y+(height/2));   //set orgin in rect middle
           g2d.rotate(Math.PI/4);                           //rotate
        
           g.drawRect(0,0, width, height);   //rotate this rect
        
           g2d.setTransform(saveAT); //rotate back?
        
           g.drawRect(x,y,width,height);  //dont rotate this rect

IMN logo majestic logo threadwatch logo seochat tools logo