Results 1 to 3 of 3
Thread: Help with Image Rotating?
- 05-31-2011, 02:19 AM #1
Member
- Join Date
- May 2011
- Posts
- 2
- Rep Power
- 0
Help with Image Rotating?
I'm a novice please be gentle ;D
I need to rotate an image about its center, by default it rotates around the top left corner.
The image is 30x30 so ideally, I would like it to rotate about its 15x15 point.
Right now I'm manually putting in an angle (in radians) but I would like to eventually use the inverse tangent of the mouse's x and y positions on the screen to find the angle, so the image is always facing the mouse pointer.
-Thanks!Java Code:public class RotateImage extends JPanel{ // Declare an Image object for us to use. Image image; int mousex = (MouseInfo.getPointerInfo().getLocation().x); int mousey = (MouseInfo.getPointerInfo().getLocation().y); double pos = (mousex/mousey); double angle = 1.57; double arctan = Math.atan(Math.toRadians(angle)); // Create a constructor method public RotateImage(){ super(); // Load an image to play with. image = Toolkit.getDefaultToolkit().getImage("Player2.png"); } public void paintComponent(Graphics g){ Graphics2D g2d=(Graphics2D)g; // Create a Java2D version of g. g2d.translate(400, 400); // Translate the center of our coordinates. g2d.rotate(angle); // Rotate the image by 1 radian. g2d.drawImage(image, 0, 0, 200, 200, this); } public static void main(String arg[]){ JFrame frame = new JFrame("RotateImage"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(800,800); frame.setLocationRelativeTo(null); RotateImage panel = new RotateImage(); frame.setContentPane(panel); frame.setVisible(true); } }
-
The Graphics2D object has a rotate method that takes 3 doubles, the first for the angle in radians, and the next two for the coordinates of the center of rotation. It uses an AffineTransform to do the rotation. Why not try it?
Also, you will need to add a MouseMotionListener to your JPanel to get the mouse's position as it moves. In the mouseMoved method, I'd calculate the angle of the mouse to the center point, use that to set a non-static class double field, say called theta, call repaint() on the JPanel, and have the paintComponent method use that theta in its rotation method call.Last edited by Fubarable; 05-31-2011 at 02:33 AM.
- 05-31-2011, 02:48 PM #3
Member
- Join Date
- May 2011
- Posts
- 2
- Rep Power
- 0
Thanks that helped, I realized the second 2 integers in g2d.drawimage were resizing my image, messing up all my measurements haha, the first 2 also set the coordinates relative to g2d.translate so I was able to tweak the position to rotate properly =D.
here's the cleaned up code:
I'll try to get it aligned with the mouse next, thanks againJava Code:public class RotateImage extends JPanel{ Image player; /* int mousex = (MouseInfo.getPointerInfo().getLocation().x); int mousey = (MouseInfo.getPointerInfo().getLocation().y); double pos = (mousex/mousey); */ double angle = Math.toRadians(90); public RotateImage(){ super(); player = Toolkit.getDefaultToolkit().getImage("Player2.png"); } public void paintComponent(Graphics g){ Graphics2D g2d=(Graphics2D)g; g2d.translate(400, 400); // define 0,0 g2d.rotate(angle, 0, 0); g2d.drawImage(player, -15, -15, 30, 30, this);//set location relative to translate, set size } public static void main(String arg[]){ JFrame frame = new JFrame("RotateImage"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(800,800); frame.setLocationRelativeTo(null); RotateImage panel = new RotateImage(); frame.setContentPane(panel); frame.setVisible(true); } }
Similar Threads
-
Rotating an image 90 Degrees
By cvillejin in forum New To JavaReplies: 4Last Post: 04-03-2011, 07:56 PM -
Rotating Buffered Image distorts image
By VortexSpin in forum Java 2DReplies: 1Last Post: 02-13-2011, 05:54 AM -
Rotating an image
By lackofcolor in forum Java 2DReplies: 3Last Post: 02-27-2009, 11:54 PM -
Rotating Image?
By sciguy77 in forum Java AppletsReplies: 9Last Post: 02-17-2009, 01:47 AM -
Rotating and flipping an image in SWT
By Java Tip in forum SWTReplies: 0Last Post: 07-02-2008, 08:01 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks