处理:如何根据单击的鼠标按钮为形状赋予值?

Processing: How do you give a shape a value based on the mouse button clicked?

我正在尝试制作一个 Processing 程序,该程序根据单击的按钮(左按钮或右按钮)绘制矩形或椭圆形,但我正在努力解决如何将值保存在变量 currentShape 中的问题在 class mousePressed。我应该在按下鼠标时获取一个值并将其保存到 currentShape 中,然后使用 mouseDragged 中的值来拖动和调整形状大小。这是我的代码:

int startX;
int startY;
int currentColor;
float currentShape;

float[] firstcornerX = {};
float[] firstcornerY = {};
float[] secondcornerX = {};
float[] secondcornerY = {};
color[] colors = {};
float[] shapes = {};

void setup() {
    size(500, 500);
    rectMode(CORNERS);
    ellipseMode(CORNERS);
}

void draw() {}

void mousePressed() {
    startX = mouseX;
    startY = mouseY;
    currentColor = color(random(255), random(255), random(255));
    if (mouseButton == LEFT) {
        ellipse(mouseX, mouseY, 100, 100);
    } else if (mouseButton == RIGHT) {
        rect(mouseX, mouseY, 100, 100);
    }
}

void mouseReleased() {
    firstcornerX = append(firstcornerX, startX);
    firstcornerY = append(firstcornerY, startY);
    secondcornerX = append(secondcornerX, mouseX);
    secondcornerY = append(secondcornerY, mouseY);
    colors = append(colors, currentColor);
    shapes = append(shapes, currentShape);
}

void mouseDragged() {
    background(255);
    for (int i = 0; i < firstcornerX.length; i++) {
        fill(colors[i]);
        rect(firstcornerX[i], firstcornerY[i], secondcornerX[i], secondcornerY[i]);
    }

    fill(currentColor);
    rect(startX, startY, mouseX, mouseY);
}

您确实在附加 currentShape,但是您没有在 mousePressed() 中更改椭圆和矩形之间的形状类型,因此 currentShape 将始终是 0.0你的代码。此外,您需要使用形状类型来检查您将在屏幕上呈现的形状(在您直接使用 rect()ellipse() 的代码中的任何地方)

个人而言,我会使用一个整数和几个常量作为形状类型(或 enum),但是 float currentShape; 也可以。假设 0.0 代表椭圆,1.0 代表矩形。您可以存储这些常量,以便轻松记住哪个是哪个:

final float SHAPE_TYPE_ELLIPSE = 0.0;
final float SHAPE_TYPE_RECT    = 1.0;

由于您需要在 draw() 中渲染形状,而且在 mouseDragged() 中,您可以将功能封装到可重用的函数中(而不是重复代码):

void drawShape(float x1, float y1, float x2, float y2, float shapeType){
  if(shapeType == SHAPE_TYPE_ELLIPSE){
    ellipse(x1, y1, x2, y2);
  }
  if(shapeType == SHAPE_TYPE_RECT){
    rect(x1, y1, x2, y2);
  }
}

条件可能是 if(shapeType == 0.0) ... else ...,但是上面的条件更容易 read/understand 并且可以在将来扩展以支持更多形状。

还剩下 3 个需要仔细检查:

  1. 根据鼠标按钮更新mousePressed()中的形状类型
  2. mouseReleased() 中附加形状类型(您已经这样做了)
  3. 相应地在 mouseDragged()draw()
  4. 中调用 drawShape()

完整代码清单:

int startX;
int startY;
int currentColor;
float currentShape;
// constants for the supported shape types 
final float SHAPE_TYPE_ELLIPSE = 0.0;
final float SHAPE_TYPE_RECT    = 1.0;

float [] firstcornerX = {};
float [] firstcornerY = {};
float [] secondcornerX = {};
float [] secondcornerY = {};
color [] colors = {};
float [] shapes = {};

void setup () {
  size(500, 500);
  rectMode(CORNERS);
  ellipseMode(CORNERS);
}

void draw() {
  background (255);
  for (int i=0; i < firstcornerX.length; i++) {
    fill(colors[i]);
    // draw the shape from memory
    drawShape(firstcornerX[i], firstcornerY[i], secondcornerX[i], secondcornerY[i], shapes[i]);
  }
}

void drawShape(float x1, float y1, float x2, float y2, float shapeType){
  if(shapeType == SHAPE_TYPE_ELLIPSE){
    ellipse(x1, y1, x2, y2);
  }
  if(shapeType == SHAPE_TYPE_RECT){
    rect(x1, y1, x2, y2);
  }
}

void mousePressed () {
  startX = mouseX;
  startY = mouseY;
  currentColor = color(random(255), random(255), random(255));
  if(mouseButton == LEFT) {
   currentShape = SHAPE_TYPE_ELLIPSE; 
  }else if(mouseButton == RIGHT) {
   currentShape = SHAPE_TYPE_RECT; 
  }
}

void mouseReleased () {
  firstcornerX = append(firstcornerX, startX);
  firstcornerY = append(firstcornerY, startY);
  secondcornerX = append(secondcornerX, mouseX);
  secondcornerY = append(secondcornerY, mouseY);
  colors = append(colors, currentColor);
  shapes = append(shapes, currentShape);
}

void mouseDragged () {
  fill(currentColor);
  // preview the shape live
  drawShape(startX, startY, mouseX, mouseY, currentShape);
}

我使用的是旧版本的 Processing,在使用 mouseDragged() 时出现了一些闪烁。或者,可以在 draw():

中使用 mousePressed 布尔值
int startX;
int startY;
int currentColor;
float currentShape;
// constants for the supported shape types 
final float SHAPE_TYPE_ELLIPSE = 0.0;
final float SHAPE_TYPE_RECT    = 1.0;

float [] firstcornerX = {};
float [] firstcornerY = {};
float [] secondcornerX = {};
float [] secondcornerY = {};
color [] colors = {};
float [] shapes = {};

void setup () {
  size(500, 500);
  rectMode(CORNERS);
  ellipseMode(CORNERS);
}

void draw() {
  background (255);
  for (int i=0; i < firstcornerX.length; i++) {
    fill(colors[i]);
    // draw the shape from memory
    drawShape(firstcornerX[i], firstcornerY[i], secondcornerX[i], secondcornerY[i], shapes[i]);
  }
  // preview the shape live if mouse is dragged:
  if(mousePressed){
    fill(currentColor);
    drawShape(startX, startY, mouseX, mouseY, currentShape);
  }
}

void drawShape(float left, float top, float right, float bottom, float shapeType){
  if(shapeType == SHAPE_TYPE_ELLIPSE){
    ellipse(left, top, right, bottom);
  }
  if(shapeType == SHAPE_TYPE_RECT){
    rect(left, top, right, bottom);
  }
}

void mousePressed () {
  startX = mouseX;
  startY = mouseY;
  currentColor = color(random(255), random(255), random(255));
  if(mouseButton == LEFT) {
   currentShape = SHAPE_TYPE_ELLIPSE; 
  }else if(mouseButton == RIGHT) {
   currentShape = SHAPE_TYPE_RECT; 
  }
}

void mouseReleased () {
  firstcornerX = append(firstcornerX, startX);
  firstcornerY = append(firstcornerY, startY);
  secondcornerX = append(secondcornerX, mouseX);
  secondcornerY = append(secondcornerY, mouseY);
  colors = append(colors, currentColor);
  shapes = append(shapes, currentShape);
}