Processing Drawing Problems

不羁岁月 提交于 2019-12-11 05:08:45

问题


I'm kind of new to processing so bear with me. I'm creating a basic drawing program where you click on a colored box to get that color and then you can draw and stuff. Well I already created a red box color and an eraser so I decided to create a blue box but when I click it, it doesn't change the color to blue. I have tried troubleshooting this with no luck.

Here's the code (note this works best with Eclipse and importing the processing core https://processing.org/tutorials/eclipse/):

// note: many imports aren't used yet
import java.util.ArrayList;
import java.util.Scanner;
import processing.core.PApplet;
import processing.core.PShape;

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Main extends PApplet{

PShape rectangle;

int color;
int color2;
int color3;
boolean red = false;
boolean blue = false;
boolean green = false;
boolean eraser = false;

// needed to create this in order for Eclipse to work
public static void main(String[] args) {
    PApplet.main("Main");
}

public void settings(){
    size(1280, 720);
}

public void setup() {
    size(1280, 720);
    smooth();
    background(255, 255, 255);
    noStroke();

}

public void draw() {
    // nothing here yet
    if (keyPressed) {

    }
    else {
        color = 0;
    }
    fill(0);

    fill(255, 0, 0);
    // red square
    rect(0, 50, 50, 50);
    fill(0, 10, 255);
    // blue square
    rect(0, 100, 50, 50);
    fill(0);


}

public void mousePressed() {
    if(red) {
        color = 255;
        color2 = 0;
        color3 = 0;
    }
    if(eraser) {
        color = 255;
        color2 = 255;
        color3 = 255;
    }
    if(blue) {
        color = 0;
        color = 10;
        color = 255;
    }
    else{
        fill(0);
    }
    // check if mouse is in drawing area
    if (mouseX >= 50 && mouseX <= 1280 && mouseY >= 0 && mouseY <= 720) {
        // change the drawing color 
        fill(color, color2, color3);
        rect(mouseX, mouseY, 50, 50);
    }
    // if red
    if (mouseX >= 0 && mouseX <= 50 && mouseY >= 50 && mouseY <= 100) {
        eraser = false;
        blue = false;
        red = true;
    }
    // if eraser (note: in top left corner)
    if (mouseX >= 0 && mouseX <= 50 && mouseY >= 0 && mouseY <= 50) {
        red = false;
        blue = false;
        eraser = true;
    }
    // if blue
    if (mouseX >= 0 && mouseX <=50 && mouseY >= 100 && mouseY <= 150) {
        eraser = false;
        red = false;
        blue = true;
    }
}

// basically the same code for mousePressed
public void mouseDragged() {
    if(red) {
        color = 255;
        color2 = 0;
        color3 = 0;
    }
    if(eraser) {
        color = 255;
        color2 = 255;
        color3 = 255;
    }
    if(blue) {
        color = 0;
        color = 10;
        color = 255;
    }
    if (mouseX >= 50 && mouseX <= 1280 && mouseY >= 0 && mouseY <= 720) {
        fill(color, color2, color3);
        rect(mouseX, mouseY, 50, 50);
    }
    if (mouseX >= 0 && mouseX <= 50 && mouseY >= 50 && mouseY <= 100) {
        eraser = false;
        blue = false;
        red = true;
    }
    if (mouseX >= 0 && mouseX <= 50 && mouseY >= 0 && mouseY <= 50) {
        red = false;
        blue = false;
        eraser = true;
    }
    if (mouseX >= 0 && mouseX <=50 && mouseY >= 100 && mouseY <= 150) {
        eraser = false;
        red = false;
        blue = true;
    }
}

}

回答1:


First of all, you shouldn't use a variable named color. That might not cause an error if you're using eclipse, but it's going to be confusing because it clashes with Processing's special color data type.

Secondly, look at this if statement:

if(blue) {
    color = 0;
    color = 10;
    color = 255;
}

You're just setting color over and over again. Maybe you meant to use color2 and color3?

That if statement is in two different places. If I change that to color1, color2, and color3, your code works okay.



来源:https://stackoverflow.com/questions/39548126/processing-drawing-problems

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!