Results 1 to 9 of 9
  1. #1
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Graphics rotation in Swing

    I've searched online and cannot find an answer. Do any of you know how to rotate a shape in Swing?
    The only thing I found was something involving this, but I have not seen something like this before:
    Java Code:
        Graphics2D g2d = (Graphics2D)g;
    I see this is an object reference, but what is on the right side? Does not seem like a method, as it would not equal a method nor would the parentheses be on the left. Why are parentheses there? Thanks

    EDIT: Disregarding the above code, I would like to know how to rotate without Graphics 2d
    Also, with G2D you it will not allow for setting x coords
    Last edited by MW130; 02-03-2014 at 04:58 AM.

  2. #2
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Graphics rotation in Swing

    Java Code:
    Graphics2D g2d = (Graphics2D)g;
    The variable 'g' is a Graphics Object, you are making a new Graphics2D object called 'g2d', since Graphics and Graphics2D are abstract classes, you cannot make new instances of them, so the only way to make a new Graphics2D object is to 'cast' Graphics2D to 'g' which is type Graphics. This will only work if the object you are casting to extends the object you are casting from, e.g Graphics2D extends Graphics, so you can cast Graphics to Graphics2D. Technically, you can cast any object to any other object, but it will only work correctly in the case stated above.
    1000011 1100001 1101110 100000 1111001 1101111 1110101 100000 1110010 1100101 1100001 1100100 100000 1000010 1101001 1101110 1100001 1110010 1111001 111111

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,458
    Blog Entries
    7
    Rep Power
    20

    Default Re: Graphics rotation in Swing

    Read about AffineTransformations; a Graphics2D object can handle them ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,540
    Rep Power
    5

    Default Re: Graphics rotation in Swing

    Here is a suggestion that might save you some heartache. If you are going to do other things with your graphics context that you do not want affected by your transformations, then you should create a copy of your active graphics context like this:

    Java Code:
    Graphics2D g2d = (Graphics2D) g.create();
    Then when you are done transforming your objects, you may dispose of the copy.
    Java Code:
    g2d.dispose();
    How and when it is necessary to use this is dependent on what you are doing in your painting method.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: Graphics rotation in Swing

    But Jim, once you create the G2D object, how do you actually make a circle with it and then set the x and y for that circle, and finally rotate it?

    EDIT: Display Image, not circle
    Last edited by MW130; 02-03-2014 at 04:53 PM.

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,540
    Rep Power
    5

    Default Re: Graphics rotation in Swing

    What you really need to do is try the Graphics2D.rotate() methods. You may also want to investigate translate so you can change the center of rotation (which one of the rotate methods does for you). Just read the JDK API and then try some examples of just rotating rectangles or even images. In some cases you may want to append various AffineTransforms to the graphics context. Discovering how all this and other things work is at least half the fun (it's about the aha moment). :)

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,458
    Blog Entries
    7
    Rep Power
    20

    Default Re: Graphics rotation in Swing

    Of course my reply, mentioning AffineTransforms, wasn't understood (nor checked in the API documentation), so it was completely ignored again. Sometimes I feel like not replying here at all anymore ...

    kind regards,

    Jos (<-- the inventor of invisible ink)
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,540
    Rep Power
    5

    Default Re: Graphics rotation in Swing

    You forgot to tell him to hold his laptop over a flame to make the ink appear. (or is that lemon juice)?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,458
    Blog Entries
    7
    Rep Power
    20

    Default Re: Graphics rotation in Swing

    Quote Originally Posted by jim829 View Post
    You forgot to tell him to hold his laptop over a flame to make the ink appear. (or is that lemon juice)?
    No, in his case it's hydrochloric acid and the laptop should be submerged in it ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Centering graphics on the graphics window
    By janyc71876 in forum New To Java
    Replies: 5
    Last Post: 11-25-2013, 07:25 PM
  2. Question about Graphics in Swing
    By MW130 in forum New To Java
    Replies: 15
    Last Post: 01-15-2013, 05:21 AM
  3. swing mixed with awt graphics
    By weddy69 in forum AWT / Swing
    Replies: 2
    Last Post: 08-31-2009, 08:01 AM
  4. Log rotation in ubuntu
    By sarah11 in forum New To Java
    Replies: 0
    Last Post: 11-04-2008, 07:46 AM
  5. affineTransform rotation
    By MichYer in forum AWT / Swing
    Replies: 0
    Last Post: 07-18-2007, 08:55 AM

Posting Permissions

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