绘图线与睡眠命令滞后

时间:2015-11-22 22:55:49

标签: java graphics drawing line

我的目标是得到这样的结果: enter image description here

灰线是鼠标跟随的路径,黑线是您在屏幕上看到的线条,但我有两个问题

  1. 新行之前的行消失了(我想留下来)
  2. 线条绘制是滞后的,只有当我的鼠标是静态时才会绘制

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

    }

1 个答案:

答案 0 :(得分:0)

执行此操作的方法是使用(缓冲)图像,在绘画中绘制图像,以及在鼠标事件处理程序中绘制图像。 Here is an example