moving a square in a specified direction

Alright, pretty simple concept but has been a challenge to code...

i have a square with (sx,sy), and the mouse can click(mx,my) anywhere on the screen. then, by subtracting the mouse's position by the squares position, it moves. Here's where the problem occurs - the further away you click from the square the bigger the velocity will be, and the faster it moves. I want one smooth speed.

i tried implementing Euclid's algorithm to find the gcd of the velocity and dividing it, but not all fractions divide, and if the gcd is negative it divides both by a negative number and flips the direction.

is my way completely wrong, is there an easier way, a different way, or a way to fix this?

Thanks in advance :)

Code:

`import java.applet.Applet;`

import java.awt.Graphics;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

public class Main extends Applet implements Runnable,MouseListener{

private static final long serialVersionUID = 1L;

int sx = 20; //square x

int sy = 20;

int mx; //mouse x

int my;

boolean flag = false; //has mouse been clicked?

long vx; //velocity of x

long vy;

public void init(){

setSize(400,300);

addMouseListener(this);

}

public void start(){

Thread th = new Thread(this);

th.start ();

}

public void run(){

Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

while(true){

repaint();

if((sx < 400 && sy < 300) && (flag == true)){ //is it on the screen?

moveSquare();

}

try{

Thread.sleep(20);

}catch (InterruptedException ex){}

Thread.currentThread().setPriority(Thread.MAX_PRIORITY);

}

}

public void paint(Graphics g){

g.fillRect(sx, sy, 10, 10);

}

public void mouseClicked(MouseEvent e){

flag = true;

mx = e.getX();

my = e.getY();

vx = mx - sx;

vy = my - sy;

int divisor = (int) gcd(vx,vy);

vx = vx / divisor;

vy = vy / divisor;

}

public void moveSquare(){

double speed = 0.1; //just slows it down a bit

sx += vx * speed;

sy += vy * speed;

}

public static long gcd(long a, long b) {

if (b==0)

return a;

else

return gcd(b, a % b);

}

public void mouseEntered(MouseEvent arg0){}

public void mouseExited(MouseEvent arg0){}

public void mousePressed(MouseEvent arg0){}

public void mouseReleased(MouseEvent arg0){}

}