【问题标题】:How to draw a line between the previous click and the most recent click?如何在上一次点击和最近一次点击之间划一条线?
【发布时间】:2021-05-27 04:07:40
【问题描述】:
int previousX;
int previousY;
void  setup() {
   size(400, 400);
   stroke(255);
} 
void mousePressed(){
  previousX = mouseX;
  previousY = mouseY;
}
void  draw() {
   background(75, 55, 43);
   line(previousX,previousY,mouseX, mouseY);   
}

最终的结果应该是当用户点击鼠标时,从0,0到用户点击鼠标的地方会出现一条线,然后当用户再次点击鼠标时,从前一个鼠标开始绘制另一条线click 到新的鼠标点击。示例:line(0,0,50,43) line(50,43,25,67) line(25,67,99,77)。我目前没有显示任何永久行的代码,但它具有先前的鼠标单击。

【问题讨论】:

    标签: java processing


    【解决方案1】:

    最简单的解决方案是不要用background(...) 覆盖前面的行。 它看起来像这样:

    int previousX;
    int previousY;
    void  setup() {
       size(400, 400);
       stroke(255);
    } 
    void mousePressed(){
      line(previousX,previousY,mouseX, mouseY);   
      previousX = mouseX;
      previousY = mouseY;
    }
    void  draw() {
    }
    

    请注意,只有当您不需要使用 background(...) 清除画布时,它才会起作用。

    【讨论】:

      【解决方案2】:

      为了绘制先前的永久线,您需要存储所有先前的点。这可以通过使用ArrayList 来实现。这些点是使用PVector 存储的,用于对xy 组件进行分组。

      ArrayList<PVector> points = new ArrayList<PVector>(); 
      
      void  setup() {
         size(400, 400);
         stroke(255);
         
         // The first point is (0, 0)
         points.add(new PVector(0, 0));
      } 
      void mousePressed(){
        // Each time the mouse is pressed add a new point
        points.add(new PVector(mouseX, mouseY));
      }
      void  draw() {
         background(75, 55, 43);
         
         // Loop through the array list
         for(int i = 0; i < points.size(); i++){
           if(i == points.size()-1){
             // For the last point draw a line to the mouse
             line(points.get(i).x, points.get(i).y, mouseX, mouseY);
           }else{
             // Draw a line between each of the points
             line(points.get(i).x, points.get(i).y, points.get(i+1).x, points.get(i+1).y);
           }
         }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-07-03
        • 2011-11-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多