Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2011
    Posts
    10
    Rep Power
    0

    Default Image Rotating Around Mouse

    I'm trying to make an overhead shooter and I made some code to test my getAngle() method but its not working. It would be awesome if anybody could shed some light as to why the image doesn't rotate correctly.



    Java Code:
    public void paint(Graphics g) {
    		super.paint(g);
    		Graphics2D g2 = (Graphics2D)g;
    		g2.setColor(Color.BLUE);
    		AffineTransform t = new AffineTransform(); 
    		t.rotate(turn,origin.x,origin.y);
    		g2.transform(t);
    		g2.fillRect(origin.x-25,origin.y-25,50,50);
    		g2.fillRect(origin.x+25,origin.y-5,50,10);
    	}
    
            public void mouseMoved(MouseEvent e) {
    		turn=getAngle(new Point(e.getX(),e.getY()));
    		repaint();
    	}
            
           
    
            public double getAngle(Point p) {
    		double rotate;
    		int quad = 0;
    		int width = 0, height = 0;
    		if(p.x==origin.x && p.y>origin.y) {
    			rotate = 90;
    		}else if(p.x==origin.x && p.y<origin.y) {
    			rotate = -90;
    		}else if(p.x>origin.x && p.y==origin.y) {
    			rotate = 0;
    		}else if(p.x<origin.x && p.y==origin.y) {
    			rotate = 180;
    		}else {
    			if(p.x<origin.x && p.y<origin.y) {
    				quad = 1;
    				width = origin.x-p.x;
    				height = origin.y-p.y;
    			}else if(p.x>origin.x && p.y<origin.y) {
    				quad = 2;
    				width = p.x-origin.x;
    				height = origin.y-p.y;
    			}else if(p.x<origin.x && p.y>origin.y) {
    				quad = 3;
    				width = origin.x-p.x;
    				height = p.y-origin.y;
    			}else if(p.x>origin.x && p.y>origin.y) {
    				quad = 4;
    				width = p.x-origin.x;
    				height = p.y-origin.y;
    			}
    			rotate = Math.atan(height/width);
    			if(quad==1) {
    				rotate -= (rotate*2) + 90;
    			}else if(quad==2) {
    				rotate -= (rotate*2);
    			}else if(quad==3) {
    				rotate += 90;
    			}else if(quad==4) {
    			}
    		}
    		turn = rotate;
    		return rotate;
    	}
    Here's the code.
    <Daniel>P.</Berry>

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default

    why the image doesn't rotate correctly
    Can you describe what the program does that is incorrect?

    Also how about adding some comments to describe the logic you are using in the getAngle method

    Hard to test this without a class and a main method.

  3. #3
    cultclassic is offline Member
    Join Date
    Mar 2011
    Posts
    64
    Rep Power
    0

    Default

    Just from the code, it is tedious to understand what exactly your are trying to do.
    Add some comments, describing your logic.

Similar Threads

  1. Help with Image Rotating?
    By Leospaceman in forum Java 2D
    Replies: 2
    Last Post: 05-31-2011, 02:48 PM
  2. Problem in rotating an image
    By newtoapplets in forum Java Applets
    Replies: 0
    Last Post: 02-28-2011, 06:12 AM
  3. Rotating Buffered Image distorts image
    By VortexSpin in forum Java 2D
    Replies: 1
    Last Post: 02-13-2011, 05:54 AM
  4. Rotating an image
    By lackofcolor in forum Java 2D
    Replies: 3
    Last Post: 02-27-2009, 11:54 PM
  5. Rotating Image?
    By sciguy77 in forum Java Applets
    Replies: 9
    Last Post: 02-17-2009, 01:47 AM

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
  •