Tuesday, December 31, 2013

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 :)































No comments:

Post a Comment