# 多彩矩形

go评论53阅读模式

Multi-Color Rectangles

# 问题

``````int[] numberOfChoices = {5, 10, 20, 25, 50, 100, 125, 250}; // 一个定义矩形大小的数组
int arrayNum;

void setup() {
size(500, 500);
}

void draw() {
for (int x = 0; x < width; x += numberOfChoices[arrayNum]) {
for (int y = 0; y < height; y += numberOfChoices[arrayNum]) {
fill(random(255), random(255), random(255));
rect(x, y, numberOfChoices[arrayNum], numberOfChoices[arrayNum]);
}
}
}

void mousePressed() {
if (mouseButton == LEFT) {
arrayNum += 1;
} else {
arrayNum -= 1;
}
}

void keyPressed() {
// 保证不超出索引范围
arrayNum = constrain(arrayNum, 0, numberOfChoices.length - 1);

// 修改颜色
for (int x = 0; x < width; x += numberOfChoices[arrayNum]) {
for (int y = 0; y < height; y += numberOfChoices[arrayNum]) {
fill(random(255), random(255), random(255));
rect(x, y, numberOfChoices[arrayNum], numberOfChoices[arrayNum]);
}
}
}
``````

My goal is to create Multi-color rectangles that gets bigger when "Left-Clicked" and smaller when "Right-Clicked". I also want to add a keyboard press that changes its color when pressed.

The problem with my code are:

1. "Out of bounds exceptions" - How can I possibly fix this? If it reaches the end of the index, I would like it to stay at the last index and not throw an out of bounds error

2. Rectangles keeps changing its color when "keyPressed()" is called inside the "draw()".

When I call the "keyPressed()" inside the "setup()", I losses the mousePressed() function.

How could I incorporate these two function having static rectangles that only changes the color when Keyboard is pressed and gets bigger or smaller when the mouse button is pressed?

``````int[] numberOfChoices = {5, 10, 20, 25, 50, 100, 125, 250}; //an array that defines the sizes of rectangles
int arrayNum;
void setup(){
size(500,500);

}

void draw(){

keyPressed();

}

void keyPressed(){
for(int x = 0; x &lt; width; x+=numberOfChoices[arrayNum]){
for(int y = 0; y &lt; height; y+=numberOfChoices[arrayNum]){
fill(random(255), random(255), random(255));
rect(x,y,numberOfChoices[arrayNum],numberOfChoices[arrayNum]);
}
}
}

void mousePressed(){
if(mouseButton==LEFT){
arrayNum +=1;
}
else arrayNum-=1;

}
``````

# 答案1

``````void mousePressed() {
arrayNum = (arrayNum + (mouseButton == LEFT ? 1 : numberOfChoices.length - 1)) % numberOfChoices.length;
}
``````

``````void mousePressed() {
if (mouseButton == LEFT) {
arrayNum++;
if (arrayNum >= numberOfChoices.length)
arrayNum = 0; // 回滚到第一个选择
} else {
arrayNum--;
if (arrayNum < 0)
arrayNum = numberOfChoices.length - 1; // 回滚到最后一个选择
}
}
``````

``````void mousePressed() {
if (mouseButton == LEFT) {
// 在最后一个选择之前递增
if (arrayNum < numberOfChoices.length - 1)
arrayNum++;
} else {
// 在第一个选择之后递减
if (arrayNum > 0)
arrayNum--;
}
}
``````

Use the `%` remainder operator to have `arrayNum` stay within the boundaries. For this to work, you need to replace add length when subtracting one.

``````void mousePressed() {
if (mouseButton == LEFT) {
arrayNum = (arrayNum + 1) % numberOfChoices.length;
} else {
arrayNum = (arrayNum + numberOfChoices.length - 1) % numberOfChoices.length;
}
}
``````

You can combine this using ternary conditional operator:

``````void mousePressed() {
arrayNum = (arrayNum + (mouseButton == LEFT ? 1 : numberOfChoices.length - 1)) % numberOfChoices.length;
}
``````

If you don't like the use of the remainder operator, you can always just do it with `if` statements:

``````void mousePressed() {
if (mouseButton == LEFT) {
arrayNum++;
if (arrayNum &gt;= numberOfChoices.length)
arrayNum = 0; // roll over to first choice
} else {
arrayNum--;
if (arrayNum &lt; 0)
arrayNum = numberOfChoices.length - 1; // roll over to last choice
}
}
``````

Or if you don't want rollovers:

``````void mousePressed() {
if (mouseButton == LEFT) {
// Increment if before last choice
if (arrayNum &lt; numberOfChoices.length - 1)
arrayNum++;
} else {
// Decrement if after first choice
if (arrayNum &gt; 0)
arrayNum--;
}
}
``````

• 本文由 发表于 2020年10月5日 10:18:23
• 转载请务必保留本文链接：https://go.coder-hub.com/64201690.html
• java
• processing

go 93

go 73

go 87

go 68