Q:

How to draw a line from 2 mouse points and an angle using mouseMoved

when I move the mouse I want to draw a line at the direction where the mouse is going, but I have no idea how to do so. I keep getting a line that rotates at random directions with random length when moving the mouse.( It should start drawing the moment I run the program and mouse points are taken at mouseMoved)

A:

Here's an example using mouseDragged. The difference between mouseMoved and mouseDragged is that a when a mouseButton is clicked, it mouseMoved is disabled, and mouseDragged takes over.
I'm not sure how you tell your application to start drawing a line, so I used a mouseClick and mouseDragged to produce an example that you can modify to resolve your issue.
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;

public class Angle extends JPanel implements MouseListener, MouseMotionListener {

    boolean pressed = false;
    int origX, origY;
    int currX, currY;
    JPanel p;

    public Angle()
    {
        this.addMouseListener(this);
        this.addMouseMotionListener(this);
    }

    @Override
    public void mousePressed(MouseEvent arg0) {
        System.out.println("Mouse pressed");
        pressed = true;
        origX = arg0.getX();
        origY = arg0.getY();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawLine(origX, origY, currX, currY);
    }

    @Override
    public void mouseReleased(MouseEvent arg0) {
        pressed = false;
    }

    @Override
    public void mouseDragged(MouseEvent arg0) {
        if (pressed)
        {
            currX = arg0.getX();
            currY = arg0.getY();
            repaint();
            System.out.println(getAngle());
        }
    }

    public double getAngle()
    {
        double xDiff = origX - currX; 
        double yDiff = origY - currY;
        return 180 - Math.toDegrees(Math.atan2(yDiff, xDiff));
    }

    @Override
    public void mouseMoved(MouseEvent arg0) { }
    @Override
    public void mouseClicked(MouseEvent arg0) { }
    @Override
    public void mouseEntered(MouseEvent arg0) { }
    @Override
    public void mouseExited(MouseEvent arg0) {  }

    public static void main(String...args)
    {
        JFrame frame = new JFrame("Frame");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(200, 200);
        frame.add(new Angle());
        frame.setVisible(true);
    }
}

Output:

EDIT: Update with mouseMoved instead.
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;

public class Angle extends JPanel implements MouseListener, MouseMotionListener {

    boolean origSet = false;
    int origX, origY;
    int currX, currY;
    JPanel p;

    public Angle()
    {
        this.addMouseListener(this);
        this.addMouseMotionListener(this);
    }

    @Override
    public void mousePressed(MouseEvent arg0) { }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawLine(origX, origY, currX, currY);
    }

    @Override
    public void mouseReleased(MouseEvent arg0) { }

    @Override
    public void mouseDragged(MouseEvent arg0) { }

    public double getAngle()
    {
        double xDiff = origX - currX; 
        double yDiff = origY - currY;
        return 180 - Math.toDegrees(Math.atan2(yDiff, xDiff));
    }

    @Override
    public void mouseMoved(MouseEvent arg0) {
        if (!origSet)
        {
            origX = arg0.getX();
            origY = arg0.getY();
            origSet = true;
        }
        currX = arg0.getX();
        currY = arg0.getY();
        repaint();
        System.out.println(getAngle());
    }
    @Override
    public void mouseClicked(MouseEvent arg0) { }
    @Override
    public void mouseEntered(MouseEvent arg0) { }
    @Override
    public void mouseExited(MouseEvent arg0) {  }

    public static void main(String...args)
    {
        JFrame frame = new JFrame("Frame");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(200, 200);
        frame.add(new Angle());
        frame.setVisible(true);
    }
}

