我在Java處理中創建了一個基本的繪圖程序,我有兩種顏色(以及橡皮擦),並且我剛創建了一種新顏色(綠色)。由於某些原因,當我點擊綠色時,它不會改變顏色。謝謝! (注意,我在Eclipse中運行這個程序時導入了處理,如果你打算只使用處理,把int顏色改爲color1。除去主函數,用PApplet.main(「main」 ),和你應該是好),如果你wan't在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;
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;
}
}
}
刪除Java標記:這是一個處理問題,而不是Java問題。是的,語言是相關的,但它們並不相同。 –