Cj1m
Cj1m

Reputation: 815

Make a line point towards the mouse

How do I make a line point towards the mouse?

I am trying to make a line(Graphics2d) point towards the mouse cursor in java. Instead of pointing towards the cursor, it points the opposite way (if the cursor is to the left, the line points to the right, same for the Y axis). Here is my code:

import java.awt.Graphics;
import java.awt.Graphics2D;
public void paintChildren(Graphics g){
Graphics g2d2 = (Graphics)g;
int centerX = 16;
int centerY = 16;
deltaX = mouseX - centerX;
deltaY = mouseY - centerY;
double angle=Math.atan2(deltaY, deltaX);

g2d2.drawLine(16, 16, (int)Math.floor(deltaX + Math.cos(angle)), (int)Math.floor(deltaY + Math.sin(angle)));
}

Upvotes: 0

Views: 995

Answers (1)

An SO User
An SO User

Reputation: 24998

Ok, pick a point on the screen of your application. Say, the center. mark that point as the anchor.

Now, add a MouseMotionListener to your screen and look for whenever the mouse is moved. You need this point to draw a line. The first point being the anchor.
Now, call repaint() every time the mouse moves.

In the paintComponent(Graphics g) , use the g.drawLine(anchorX,anchorY,mouseX,mouseY) to draw the line. Since paint() is called before repaint(), you need to give some initial value to these 4 variables.

SSCCE:

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Toolkit;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionListener;

    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;

    public class LineFollower extends JFrame{

        int anchorX = 0;
        int anchorY = 0;
        int mouseX = 0;
        int mouseY = 0;

        JPanel canvas = new JPanel(){

                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(500, 500);
                }

                @Override
                public void paintComponent(Graphics g){
                        super.paintComponent();
                        g.setColor(Color.RED);
                        g.drawLine(anchorX, anchorY, mouseX, mouseY);
                }
        };

        public LineFollower(){
                anchorX = 500 / 2;
                anchorY = 500 / 2;

                this.getContentPane().add(canvas);
                this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                canvas.addMouseMotionListener(new MouseMotionListener(){
                        @Override
                        public void mouseDragged(MouseEvent e) {
                                mouseX = e.getX();
                                mouseY = e.getY();
                                repaint();
                        }

                        @Override
                        public void mouseMoved(MouseEvent e) {
                                mouseX = e.getX();
                                mouseY = e.getY();
                                repaint();
                        }
                });
                this.pack();
                //this.setSize(500,500);
                this.setVisible(true);
        }
        public static void main(String[] args) {
                SwingUtilities.invokeLater(new Runnable(){

                        @Override
                        public void run() {
                                new LineFollower();
                        }
                });
        }
}

Upvotes: 2

Related Questions