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)
当我移动鼠标时,我想在鼠标移动的方向画一条线,但我不知道该怎么做。当移动鼠标时,我不断得到一条随机长度随机旋转的线。(它应该在我运行程序时开始绘制,鼠标点在mouseMoved处获取)
1 个解决方案
#1
3
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.
这是使用mouseDragged的示例。 mouseMoved和mouseDragged之间的区别在于,单击mouseButton时,将禁用mouseMoved,并且mouseDragged将接管。
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.
我不确定你如何告诉你的应用程序开始绘制一条线,所以我使用了mouseClick和mouseDragged来生成一个可以修改以解决问题的示例。
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);
}
}
EDIT: Update with mouseMoved instead.
编辑:改为使用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);
}
}
#1
3
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.
这是使用mouseDragged的示例。 mouseMoved和mouseDragged之间的区别在于,单击mouseButton时,将禁用mouseMoved,并且mouseDragged将接管。
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.
我不确定你如何告诉你的应用程序开始绘制一条线,所以我使用了mouseClick和mouseDragged来生成一个可以修改以解决问题的示例。
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);
}
}
EDIT: Update with mouseMoved instead.
编辑:改为使用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);
}
}