用鼠标在页面上绘制流畅的线条

时间:2016-12-29 15:37:46

标签: java swing

我试过四处寻找,但似乎没有什么能覆盖我正在寻找的东西。

我目前正在为我的课程制作一个迷你图形编辑器,它现在可以用于画笔工具,但是如果我太快移动鼠标它似乎根本不能很好地工作。

绘图是通过鼠标监听器完成的,在按住鼠标时触发“绘图”事件。然后每次循环滚动时,它会根据其他设置添加一个圆圈(大小和颜色)。该方法采用初始鼠标位置(MouseEvent e),然后使用PointerInfo更新当前鼠标位置,并在其周围进行一些计算,以确保它在当前位置绘制(画布使用空布局)。

一些代码显示它在做什么:

void setMouseDown(MouseEvent e){
    PointerInfo mousePos = MouseInfo.getPointerInfo();
    int mouseRelativeX = e.getX();
    int mouseRelativeY = e.getY();
    int startX = (int) mousePos.getLocation().getX();
    int startY = (int) mousePos.getLocation().getY();
    while(mouseDown){
        mousePos = MouseInfo.getPointerInfo();
        mouseMarginX = mouseRelativeX - (startX - (int) mousePos.getLocation().getX());
        mouseMarginY = mouseRelativeY - (startY -(int) mousePos.getLocation().getY());

        if(curTool == "drawPen"){
            DrawPen drawPen = new DrawPen(toolSize, Color.BLACK);       
            add(drawPen);
            drawPen.setBounds(mouseMarginX, mouseMarginY, toolSize, toolSize);
        }else if(curTool == ""){

        }else if(curTool == ""){

        }else if(curTool == ""){

        }
        revalidate();
        repaint();
    }
}

如果有人对最佳方法有任何建议,那么我们将不胜感激。感谢

0 个答案:

没有答案
相关问题