如何使用mouseMoved从2个鼠标点和一个角度绘制一条线

时间:2016-02-25 13:50:04

标签: java swing awt

当我移动鼠标时,我想在鼠标移动的方向画一条线,但我不知道该怎么做。当移动鼠标时,我不断得到随机长度随机旋转的线条。(它应该在我运行程序时开始绘制,鼠标点在mouseMoved处获取)

1 个答案:

答案 0 :(得分:3)

以下是使用mouseDragged的示例。 mouseMovedmouseDragged之间的区别在于,当点击mouseButton时,mouseMoved被禁用,mouseDragged接管。

我不确定您如何告诉您的应用程序开始绘制线条,因此我使用mouseClickmouseDragged生成了一个示例,您可以修改该示例来解决您的问题。

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);
    }
}

输出: Example output

编辑:改为使用mouseMoved更新。

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);
    }
}