在处理中绘制程序的问题

时间:2016-09-27 01:54:29

标签: eclipse processing

我正在Java处理中创建一个基本的绘图程序,我有两种颜色(以及橡皮擦),我刚刚创建了一种新颜色(绿色)。出于某种原因,当我点击绿色时,它不会改变颜色。谢谢!

(注意,我在Eclipse中运行了这个程序并导入了处理,如果你打算只使用处理,将int颜色改为color1。摆脱主函数,使用PApplet.main(“main”) ;,你应该好的)如果你不想在日食中运行它(就像我一样),这里有一篇文章: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;

String file = "";

char letter;

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

boolean saving = 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() {

    if (keyPressed) {

        if (key == 'c') {
            background(255, 255, 255);
        }

        if (key == 's') {
            save("Drawing.tif");

        }

    }
    else {
        color = 0;
    }
    fill(0);
    text("Press 'c' to clear the screen", 50, 700, 200, 50);
    text("Press 's' to save", 250, 700, 200, 50);

    fill(255, 0, 0);
    // red square
    rect(0, 50, 50, 50);
    fill(0, 10, 255);
    // blue square
    rect(0, 100, 50, 50);
    fill(0, 255, 40);
    // green square
    rect(0, 150, 50, 50);
    fill(255, 255, 0);
    // yellow square
    rect(0, 200, 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;
        color2 = 10;
        color3 = 255;
    }
    if(green){
        color = 0;
        color2 = 255;
        color3 = 40;
    }
    else{
        fill(0);
    }
    // check if mouse is in drawing area
    if (mouseX >= 50 && mouseX <= 1280 && mouseY >= 0 && mouseY <= 680) {
        // 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;
        green = false;
        red = true;
    }
    // if eraser (note: in top left corner)
    if (mouseX >= 0 && mouseX <= 50 && mouseY >= 0 && mouseY <= 50) {
        red = false;
        blue = false;
        green = false;
        eraser = true;
    }
    // if blue
    if (mouseX >= 0 && mouseX <=50 && mouseY >= 100 && mouseY <= 150) {
        eraser = false;
        red = false;
        green = false;
        blue = true;
    }
    // if green
    if (mouseX >= 0 && mouseY <= 50 && mouseY >= 150 && mouseY <= 200) {
        eraser = false;
        red = false;
        blue = false;
        green = 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;
        color2 = 10;
        color3 = 255;
    }
    if(green){
        color = 0;
        color2 = 255;
        color3 = 40;
    }
    else{
        fill(0);
    }
    // check if mouse is in drawing area
    if (mouseX >= 50 && mouseX <= 1280 && mouseY >= 0 && mouseY <= 680) {
        // 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;
        green = false;
        red = true;
    }
    // if eraser (note: in top left corner)
    if (mouseX >= 0 && mouseX <= 50 && mouseY >= 0 && mouseY <= 50) {
        red = false;
        blue = false;
        green = false;
        eraser = true;
    }
    // if blue
    if (mouseX >= 0 && mouseX <=50 && mouseY >= 100 && mouseY <= 150) {
        eraser = false;
        red = false;
        green = false;
        blue = true;
    }
    // if green
    if (mouseX >= 0 && mouseY <= 50 && mouseY >= 150 && mouseY <= 200) {
        eraser = false;
        red = false;
        blue = false;
        green = true;
    }
}

}

1 个答案:

答案 0 :(得分:0)

就像我在your previous question中所说,你真的不应该使用名为color的变量。在日食中它可能适合你,但它令人困惑。至少它让我们更难帮助你。当你处理它时,你应该处理像未使用的import语句这样的事情。另见:How to create a Minimal, Complete, and Verifiable example

要弄清楚这样的问题,你必须做一些调试。我要检查的第一件事是:代码是否在if函数中输入了绿色mousePressed()语句?所以我在println()语句中添加if语句,如下所示:

  if (mouseX >= 0 && mouseY <= 50 && mouseY >= 150 && mouseY <= 200) {
    println("here");
    eraser = false;
    red = false;
    blue = false;
    green = true;
  }

运行程序并单击绿色方块,我们看到永远不会输入此if语句。这肯定是可疑的,所以让我们仔细看看if声明本身:

  if (mouseX >= 0 && mouseY <= 50 && mouseY >= 150 && mouseY <= 200) {

注意什么?如果你没有,请大声朗读。

第二项检查是比较mouseY。您似乎打算使用mouseX