Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: rotate in java

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default rotate in java

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



    ....
    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
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    976
    Thanks
    7
    Thanked 205 Times in 181 Posts

    Default Re: rotate in java

    //i think i need to rotate the screen back?
    //if so than how to do this?
    You can do the second rotation the same way you did the first.

  3. #3
    Member
    Join Date
    Mar 2013
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: rotate in java

    i just change some stuff. the problem is that the 1st rect x, y, postion is getting messed up. iam not sure why. this look righ tto me.

    ///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(x,y,width,height);   //rotate this rect
     
                g2d.setTransform(saveAT); //rotate back
     
                g.drawRect(...);  //dont rotate this rect

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    976
    Thanks
    7
    Thanked 205 Times in 181 Posts

    Default Re: rotate in java

    Perhaps you could post a SSCCE to illustrate the problem - not code with all your program logic, but something brief and compilable that shows the problem. It also might pay to explain what you mean by "messed up". That is, describe what the output should be as well as what does appear.

    ---

    Personally I would call drawRect() using the variable g2d - I just think the intent is clearer, but it comes to the same thing.

Similar Threads

  1. API/Tool to rotate Rtf document
    By sreelakshmi.p in forum Java Theory & Questions
    Replies: 4
    Last Post: February 4th, 2013, 08:49 AM
  2. Replies: 3
    Last Post: May 5th, 2012, 09:33 AM