gpt4 book ai didi

java - 处理绘图问题

转载 作者:太空宇宙 更新时间:2023-11-04 12:14:36 25 4
gpt4 key购买 nike

我对处理有点陌生,所以请耐心等待。我正在创建一个基本的绘图程序,您可以在其中单击一个彩色框来获取该颜色,然后您就可以进行绘图和其他操作。好吧,我已经创建了一个红色框颜色和一个橡皮擦,所以我决定创建一个蓝色框,但是当我单击它时,它不会将颜色更改为蓝色。我尝试过对此问题进行故障排除,但没有成功。

这是代码(请注意,这最适合 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;
}
}

}

最佳答案

首先,您不应该使用名为 color 的变量。如果您使用的是 eclipse,这可能不会导致错误,但它会令人困惑,因为它与处理的特殊 color 数据类型冲突。

其次,看看这个 if 语句:

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

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

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

关于java - 处理绘图问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39548126/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com