Results 1 to 2 of 2
  1. #1
    AvivC is offline Member
    Join Date
    Dec 2013
    Posts
    26
    Rep Power
    0

    Default How to rotate graphic object and have it stay at it's place?

    I have a class DrawingPanel, extending JPanel and added to a JFrame.

    Java Code:
    import java.awt.*;
    import java.util.*;
    import javax.swing.*;
    
    public class DrawingPanel extends JPanel {
    
    	public void paintComponent(Graphics g){
    		
    		super.paintComponent(g);
    		Graphics2D g2d = (Graphics2D)g;
    		
    		g2d.setFont(new Font("Arial",Font.BOLD,24));
    		g2d.drawString("A",250,250);
    		g2d.rotate(30 * Math.PI/180);
    		g2d.drawString("B",250,250);
    		
    	}
    	
    }
    "B" is not only rotated 30 degrees, but also apears in a far different location than "A".

    I think I understand why that is. It's because rotating and scaling means rotating and scaling the entire x and y system too, or something like that.

    But my question is, how could I rotate a graphic object, and have it stay at the place that it "was" before I rotated it? (Or before I scaled it, got the same problem there).

    Thanks :)
    Last edited by AvivC; 12-31-2013 at 10:55 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: How to rotate graphic object and have it stay at it's place?

    When you rotate an object, you rotate it around a fixed point, or the center of rotation. So if you draw a square and want to rotate it, you need to translate the center of rotation to the center of the square.

    Regards,
    Jim
    Last edited by jim829; 01-01-2014 at 12:13 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. GridBagLayout - forcing all components to stay on top
    By Serpenthelm in forum AWT / Swing
    Replies: 3
    Last Post: 01-05-2012, 07:52 PM
  2. Trying to display a copied Graphic object - SSCCE
    By AcousticBruce in forum New To Java
    Replies: 11
    Last Post: 03-03-2011, 04:18 AM
  3. Forcing the mouse to stay quiet
    By ProspectiveDeveloper in forum AWT / Swing
    Replies: 4
    Last Post: 05-03-2010, 12:17 AM
  4. Replies: 5
    Last Post: 04-25-2009, 09:00 PM

Tags for this Thread

Posting Permissions

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