处理:如何根据鼠标点击的按钮赋予形状一个值?

huangapple go评论69阅读模式
英文:

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

问题

这是你提供的代码:

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);
}

请注意,我已经将代码部分翻译为中文,如上所示。

英文:

I'm trying to make a program on Processing that draws either a Rectangle or an Ellipse based on the button thats clicked (Left or Right button) and I'm struggling with how to save the value in the variable currentShape in the class mousePressed. I'm supposed to get a value when the mouse is pressed and save it into currentShape and then use the value in mouseDragged to drag and mess with the shapes size. This is the code I have:

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 &lt; firstcornerX.length; i++) {
        fill(colors[i]);
        rect(firstcornerX[i], firstcornerY[i], secondcornerX[i], secondcornerY[i]);
    }

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

答案1

得分: 1

以下是翻译好的部分:

你确实在 mousePressed() 中添加了 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 ...,但是上述代码更易于阅读/理解,并且可以扩展以支持将来的更多形状。

这还剩下 3 个地方需要双重检查:

  1. 根据鼠标按钮在 mousePressed() 中更新形状类型。
  2. mouseReleased() 中添加形状类型(您已经在这里执行)。
  3. mouseDragged()draw() 中相应地调用 drawShape()

完整的代码清单:

// ...(之前的代码)

我正在使用旧版本的 Processing,在 mouseDragged() 中遇到了一些闪烁问题。或者,可以在 draw() 中使用 mousePressed 布尔值:

// ...(之后的代码)

希望这能帮助您理解代码中的内容。如果您有任何问题,请随时提问。

英文:

You are indeed appending currentShape, however you're not changing the shape type between ellipse and rectangle in mousePressed(), hence currentShape will always be 0.0 in your code. Additionally you need to use the shape type to check what shape you'll render on screen (everywhere in your code where you directly use rect() and ellipse())

Personally I would've used an integer and a couple of constants for the shape type (or an enum), however float currentShape; will do. Let's say 0.0 represents an ellipse and 1.0 represents a rectangle. You can store these constants so it's easy to remember which one's which:

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

Since you need to render the shapes in draw(), but also while mouseDragged(), you can encapsulate a functionality into a reusable function (instead of duplicating code):

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);
}
}

The conditions could've been if(shapeType == 0.0) ... else ..., however the above is easier to read/understand and can be expanded to support more shapes in the future.

That leaves 3 left to double check:

  1. update the shape type in mousePressed() based on the mouse button
  2. append the shape type in mouseReleased() (which you already do)
  3. calling drawShape() accordingly in mouseDragged() and draw()

The full code listing:

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 &lt; 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);
}

I'm using an older version of Processing and experiencing some flickering with mouseDragged(). Alternatively the mousePressed boolean can be used in draw():

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 &lt; 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);
}

huangapple
  • 本文由 发表于 2020年9月21日 01:44:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63981932.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定