mousePressed后如何在fill()中保持颜色相同? [英] How to keep a color in a fill() the same after mousePressed?

查看:218
本文介绍了mousePressed后如何在fill()中保持颜色相同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

点击(当它发生变化时)然后取消点击并留下悬停时,保持 fill()相同的最简单最简单的方法是什么?

What would be the easiest and simplest way to keep the fill() the same after clicking (that's when it changes) and then unclicking, and leaving hover?

在这个项目中,我只是制作了一个网格。当鼠标悬停在特定的矩形上( x y )时,它会根据它所处的状态改变颜色。 fill(50)是默认值, fill(75)是鼠标悬停的时候, fill(100)是鼠标点击的时间。但是当鼠标未被点击时,它返回到悬停填充,直到鼠标离开矩形。谢谢。

In this project, I simply made a grid. When the mouse hovers over a specific rect (at x,y) it changes color based on the state it is in. fill(50) is the default, fill(75) is when the mouse is hovering, and fill(100) is when the mouse clicks. But here when the mouse is unclicked it returns to hover fill until the mouse leaves the rectangle. Thanks.

int cols, rows;
int scl = 20;

void setup() {
size(400, 400);
int w = 400;
int h = 400;
cols = w / scl;
rows = h / scl;
}

void draw() {
background(255);

  for (int x = 0; x < cols; x++) {
    for (int y = 0; y < rows; y++) {
      int xpos = x*scl;
      int ypos = y*scl;
      stroke(55);
      if((mouseX >= xpos && mouseX <= xpos+scl) &&
        (mouseY >= ypos && mouseY <= ypos+scl)){
        fill(75);
        if (mousePressed == true){
          println("Clicked at: " + xpos + " and " + ypos);
          fill(100);
        //here is the desired location for the fill to remain constant even 
        //after unclicking and leaving hover
        }
        println("Mouse at: " + xpos + " and " + ypos);
      }else{
        fill(50);
      }

      rect(xpos, ypos, scl, scl);
    }
  }
}


推荐答案

Stack Overflow并非真正设计用于一般的我该怎么做类型的问题。这是针对特定的我试过X,期待Y,但得到Z而不是类型的问题。但我会尝试在一般意义上提供帮助:

Stack Overflow isn't really designed for general "how do I do this" type questions. It's for specific "I tried X, expected Y, but got Z instead" type questions. But I'll try to help in a general sense:

您需要将每个单元格的状态存储在数据结构中,然后使用该数据结构绘制您的场景。

You need to store the state of each cell in a data structure, and then use that data structure to draw your scene.

您可以使用2D数组执行此操作,其中数组中的每个单元格代表网格中的单元格。您可以直接存储单元格的状态或颜色。

You could do this with a 2D array, where each cell in the array represents a cell in the grid. You could store the state of the cell, or the color directly.

这篇关于mousePressed后如何在fill()中保持颜色相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆