处理绘图问题

时间:2016-09-17 14:48:29

标签: java eclipse processing

我对处理有点新意,所以请耐心等待。我正在创建一个基本的绘图程序,您可以在其中单击彩色框以获得该颜色,然后您可以绘制和填充。好吧,我已经创建了一个红色的盒子颜色和一个橡皮擦,所以我决定创建一个蓝色的盒子,但是当我点击它时,它不会将颜色改为蓝色。我试过没有运气对此进行故障排除。

这是代码(注意这最适合Eclipse并导入处理核心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 个答案:

答案 0 :(得分:0)

首先,您不应使用名为color的变量。如果您正在使用eclipse,这可能不会导致错误,但它会让人感到困惑,因为它与Processing的特殊color数据类型发生冲突。

其次,请查看此if声明:

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

您只是一遍又一遍地设置color。也许您打算使用color2color3

if语句位于两个不同的位置。如果我将其更改为color1color2color3,则代码可以正常运行。