绘制直线,如MS画图

问题描述 投票:1回答:1

我正在尝试绘制直线(在处理3.5.4中),就像在MS Paint工具中所做的一样(通过单击鼠标左键选择一个点并拖动指针,然后释放它以获取直线)。我尝试使用mousePressed()mouseReleased()函数,它会创建直线,但是当我拖动它而不释放它时,它不会实时显示该直线,这是正常的,因为我没有使用过draw()在这种情况下起作用。

void mousePressed() {
  x1 = mouseX;
  y1 = mouseY;
}
void mouseReleased() {
  line (x1, y1, mouseX, mouseY);
}

[我还试图实现在draw()函数内部创建线,以便我可以实时获取未发布直线的运动,但是通过绘制多条直线也会失败。

void draw () {
  if(mousePressed) {
    line (x1, y1, mouseX, mouseY);
  }
}

I've marked (x1, y1) and (mouseX, mouseY) points as mouse's pressing and releasing points

而且我正在尝试实时实现这样的功能(同时拖动鼠标)-

I've marked the points for the understanding purpose

processing
1个回答
0
投票

如果线条未完成,则必须从起点到mouseX中的当前鼠标位置(mouseXmouseY)画一条线。

使用mouseY个对象的draw()存储点:

ArrayList

每次单击鼠标按钮,然后将一个点添加到列表中:

ArrayList

在循环中画出点之间的线。如果列表中的点数为奇数,则从最后一个点到当前鼠标位置画一条线:

PVector

请参见示例:

PVector

ArrayList<PVector> points = new ArrayList<PVector>();
© www.soinside.com 2019 - 2024. All rights reserved.