我试图使我的三角形在单击鼠标时重绘,但它们仅在单击鼠标时才重绘

问题描述

当我运行该程序时,单击绿色按钮时,整个地方都会出现绿色三角形,我希望它重新定位到随机位置并删除原始绿色三角形

private HttpClient apiclient;
private List<Api> Endpoints;
[...]
foreach(Api api in this.Endpoints)
{
    this.apiclient = new HttpClient();
    this.apiclient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(api.mediaType));
    this.apiclient.Timeout = TimeSpan.FromMinutes(api.timeout);
    this.apiclient.DefaultRequestHeaders.TryAddWithoutValidation("Authorization",api.credentials);
    
    foreach (string url in api.urls)
    {
        # retrieve data from APIs and do something with it
    }
}

解决方法

然后在您的设置中添加noLoop(),这样您就不会每秒绘制60次相同的东西,而是使用mouse click handling并通过redraw进行调用,因此您只在有是要绘制的新内容:

float x,y,triangleSideLength;

void setup() {
  size(300,300);
  noLoop();
  triangleSideLength = 30; // or whatever value it needs to be of course
}

void draw() {
  x = random(0,width - triangleSideLength);
  y = random(0,height - triangleSideLength);
  drawTriangleAt(x,y);
}

void drawTriangleAt(float x,float y) { 
  //...triangle drawing code here...
}

void mouseClicked() {
  redraw();
}

还请注意,您通常希望约束x / y坐标,以便三角形始终“适合屏幕”,而不是获得恰好具有x = width的x / y坐标,现在您的三角形基本上为100 %看不见。