我的目标是得到这样的结果: enter image description here
灰线是鼠标跟随的路径,黑线是您在屏幕上看到的线条,但我有两个问题
线条绘制是滞后的,只有当我的鼠标是静态时才会绘制
public class Press extends Applet实现MouseMotionListener {
public int count;
public int x1, y1, x2, y2;
public void init(){
this.addMouseMotionListener(this);
}
public Press(){
count = 0;
}
public void paint (Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.draw(new Line2D.Double(x1, y1, x2, y2));
repaint();
}
public void clickup(){
count++;
}
public void mouseDragged(MouseEvent e){
x1 = x2;
y1 = y2;
x2 = e.getX();
y2 = e.getY();
System.out.println("x: " + x2 + " y: " + y2 + " Count: " + count);
clickup();
try {
Thread.sleep(100);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
public void mouseMoved(MouseEvent e) {
}
public static void main(String[] args) {
Press mouse = new Press();
}
}