[英]Show a line between first mouse click and cursor
I'm trying to write a program that will show a line between the first click and mouse position after the 1st click.我正在尝试编写一个程序,该程序将在第一次单击后显示第一次单击和鼠标 position 之间的一条线。 Then after the 2nd click, it shows a line.
然后在第二次单击后,它显示一条线。
I know I will have to use MouseListener
getX()
, getY()
to get the position where the mouse click is at, but the part where I am confused is to show a line between the first click and mouse position then after the second click it shows a line.我知道我将不得不使用
MouseListener
getX()
, getY()
来获得鼠标点击所在的 position ,但我感到困惑的部分是在第一次点击和鼠标 position 之间显示一条线,然后在第二次点击之后它显示一条线。 The tutorial I found online only show me how to draw a line between two mouse clicks.我在网上找到的教程只向我展示了如何在两次鼠标点击之间画一条线。
Would greatly appreciate if anyone can point me in the right direction.如果有人能指出我正确的方向,将不胜感激。
You need to know three points.你需要知道三点。
If both first and second click points are null
, then basically do nothing如果第一个和第二个点击点都是
null
,那么基本上什么都不做
If the first click point and current mouse point are not null
and the second point is null
, draw a line between the first and transient point如果第一个点击点和当前鼠标点不是
null
而第二个点是null
,在第一个点和瞬态点之间画一条线
If both the first and second click points are not null
, then draw a line between those two instead如果第一个和第二个点击点都不是
null
,则在这两者之间画一条线
To make this work, you'll need a MouseListener
and a MouseMotionListener
要完成这项工作,您需要一个
MouseListener
和一个MouseMotionListener
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Line2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private Point startPoint;
private Point endPoint;
private Point transientPoint;
public TestPane() {
MouseAdapter ma = new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (startPoint == null) {
startPoint = e.getPoint();
} else if (endPoint == null) {
endPoint = e.getPoint();
} else {
endPoint = null;
startPoint = e.getPoint();
}
repaint();
}
@Override
public void mouseMoved(MouseEvent e) {
if (startPoint != null && endPoint == null) {
transientPoint = e.getPoint();
repaint();
} else {
transientPoint = null;
}
}
};
addMouseListener(ma);
addMouseMotionListener(ma);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
if (startPoint != null && endPoint != null) {
g2d.setColor(Color.BLUE);
g2d.draw(new Line2D.Double(startPoint, endPoint));
} else if (startPoint != null && transientPoint != null) {
g2d.setColor(Color.RED);
g2d.draw(new Line2D.Double(startPoint, transientPoint));
}
g2d.dispose();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.