Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: MouseMoved and MouseDragged problem

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default MouseMoved and MouseDragged problem

    hey guys,
    so heres the problem, i have a stick rotating around the center of the ball when the mouse moves. here are portions of the code:
             public void paintComponent(Graphics g){
    		super.paintComponent(g);
    	             g.drawImage(Ball.i,Ball.x,Ball.y,null);
    		Graphics2D g2 = (Graphics2D)g;
    		g2.transform(AffineTransform.getRotateInstance(theta, Ball.x+6, Ball.y+6));
    		g2.drawImage(stick,stickx,sticky,null);
              }

    here are the mouseMoved and mouseDragged methods:
              public void mouseMoved(MouseEvent e) {
    		o =-e.getY()+ cBall.y+6  ;
    		a =-e.getX()  + cBall.x+6  ;
    		h = Math.hypot(o, a);
    		theta = Math.atan2(o, a);
    		repaint();
    	//record where mouse stopped
    		MouseInX= e.getX();
    		MouseInY= e.getY();
     
                // this is where the stick is originally 	
    		origStickX = Ball.x+6; 
    		origStickY = Ball.y +5;
     
    	          }
     
               //mouse dfragged method
               public void mouseDragged(MouseEvent e) {
                     hyp = (int) Math.hypot((MouseInX-e.getX()),(MouseInY-e.getY()));
                     stickx = (int) (origStickX +((hyp)*(Math.cos(theta))));
    	         sticky = (int) (origStickY +((hyp)*(Math.sin(theta))));
    	         repaint();
     
    	         }

    now the stick rotates towards the mouse so far so good, but in my MouseDragged method i want to pull the stick back according to its angle of rotation in a straight line, like in pool, but with what ive written so far it doesnt always go back in a straight line, sometimes it deviates sideways sometimes it goes in the wrong direction and ive tried many things but i am stuck, just wondering if anyone has any idea how i may be able to approach this, the code above does not include the instance variables but they are all there. any help would be greatly appreciated.
    Last edited by brigadier90; February 23rd, 2011 at 06:11 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: MouseMoved and MouseDragged problem

    If you want help, you'll probably want to provide an SSCCE that demonstrates the problem, so we can run it and play with it ourselves. This doesn't mean paste in your whole program.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: MouseMoved and MouseDragged problem

    sorry if its a bit long, this is as short as i could make it, thx again for taking a look:

    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.AffineTransform;
    import javax.swing.*;
     
     
    public class SSCCE extends JPanel   {
     
    	public static void main(String args[]){
    		SSCCE s = new SSCCE();
    		JFrame f = new JFrame("SSCCE");
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		f.setVisible(true);
    		f.setSize(500,500);
    		f.add(s);
    	}
     
    	public SSCCE(){
    		Handler handler = new Handler();
    		addMouseMotionListener(handler);
    		addMouseListener(handler);
    		ballx =200;
    		bally=200;
    		stickx = ballx+6;
    		sticky = bally+5;
    		}
     
    	public void paintComponent(Graphics g){
     
    		super.paintComponent(g);
    		g.fillOval(ballx, bally, 13, 13);
    		Graphics2D g2 = (Graphics2D)g;
    		// rotate with anchor point in the middle of the ball
    		g2.transform(AffineTransform.getRotateInstance(theta, ballx+6, bally+6));
    		//draw the stick at the center of the ball
    		g2.fillRect(stickx, sticky, 200, 3);
    	}
     
    	private class Handler implements MouseMotionListener, MouseListener{
     
    	private int MouseInX;
    	private int MouseInY;
    	private int hyp;
    	private int origCueX;
    	private int origCueY;
     
     
    	public void mouseMoved(MouseEvent e) {
    		o =-e.getY()+ ballx+6  ;
    		a =-e.getX()  + bally+6  ;
    		theta = Math.atan2(o, a);
    		repaint();
    		//record where mouse stopped
    		MouseInX= e.getX();
    		MouseInY= e.getY();
     
    		origCueX = ballx+6;
    		origCueY = bally +5;
     
    		}
     
    	public void mouseDragged(MouseEvent e) {
     
    		hyp = (int) Math.hypot((MouseInX-e.getX()),(MouseInY-e.getY()));
    		stickx = (int) (origCueX +((hyp)*(Math.cos(theta))));
    		sticky = (int) (origCueY +((hyp)*(Math.sin(theta))));
    		repaint();
     
    		}
     
    	public void mouseClicked(MouseEvent e) {
     
     
     
    	}
     
    	public void mouseEntered(MouseEvent arg0) {
    		// TODO Auto-generated method stub
     
    	}
     
    	public void mouseExited(MouseEvent arg0) {
    		// TODO Auto-generated method stub
     
    	}
     
    	public void mousePressed(MouseEvent e) {
     
     
    	}
     
    	public void mouseReleased(MouseEvent arg0) {
    		stickx = origCueX;
    		sticky= origCueY;
    		repaint();
     
    	}
     
    }
    	double o; // used for stick rotation
    	double a; //used for stick rotation
    	double theta; // rotates stick
    	private int stickx;
    	private int sticky;
    	private int ballx;
    	private int bally;
     
     
    }