gpt4 book ai didi

java - 处理中的面向对象编程错误

转载 作者:行者123 更新时间:2023-12-02 05:55:07 25 4
gpt4 key购买 nike

我知道我做错了什么,但我似乎无法修复它。

class Draw {

int x,y;
int xp,yp;
PImage image;

Draw(int dragx, int dragy, int movex, int movey, PImage a) {
x = dragx;
y = dragy;
xp = movex;
yp = movey;
image = a;
}

void display() {
smooth();
background(255);
fill(255);
rect (0,180,40,40);
fill(0);
rect (0,240,40,40);
image = get();
stroke(0);
fill(255);
}

void drawing() {
background(image);
float sizex = xp - x;
float sizey = yp - y;
if (mousePressed && mouseButton == LEFT) {
rect(x, y, sizex, sizey);
}
}

void press() {
x = mouseX;
y = mouseY;
}

void release() {
xp = mouseX;
yp = mouseY;
noLoop();
image = get();
loop();
}

void drag() {
xp = mouseX;
yp = mouseY;
}
}
<小时/>
Draw rect;

void setup() {
size (900,600);
//rect.display();
}

void draw() {
background(255);
//rect.drawing();
}
void mousePressed() {
//rect.press();
}
void mouseReleased() {
//rect.release();
}
void mouseDragged() {
//rect.drag();
}

注释或“//”中的区域是我收到的错误,给我“NullPointerException”错误。我想以某种方式了解如何将“void display()”放在“绘制矩形”下的“void setup()”下;没有任何错误。

最佳答案

您尚未在任何地方初始化rect。您应该初始化它:

void setup() {
rect = new Draw(0, 0, 0, 0, new PImage());
size (900,600);
//rect.display();
}

关于java - 处理中的面向对象编程错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23179211/

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