gpt4 book ai didi

java - 创建 PShape 时,处理库中出现 NullPointerException

转载 作者:行者123 更新时间:2023-12-03 07:14:52 26 4
gpt4 key购买 nike

我有以下处理程序:

//using Papplet instead of STDraw to visually represent my grid, created by Mahmed Ibrahim
import java.awt.Color;

import processing.core.*;

import processing.core.PApplet;

public class C4Grid extends PApplet {
PShape s;
PShape[][] circleSpaces;
boolean[][] circleSpacesFilled;
boolean[][] circleHasYelowPiece;
boolean[][] circleHasRedPiece;
final float SPACES_BETWEEN_ROWS = 110;
final float SPACES_BETWEEN_COLUMNS = 130;


public C4Grid(){}

public void setup() {
System.out.println("it got to here where it breaks");

size(1000, 1000, P2D);

// Making the shape of the grid using vertices
// so I'm manually drawing my polygon.
s = createShape();
s.beginShape();
s.fill(34, 56, 100);
s.tint(34, 56, 100);
s.stroke(0);
s.strokeWeight(5);
s.vertex(400, 400);
s.vertex(400, -440);
s.vertex(360, -440);
s.vertex(360, -400);
s.vertex(-360, -400);
s.vertex(-360, -440);
s.vertex(-400, -440);
s.vertex(-400, 420);
s.vertex(-420, 420);
s.vertex(-420, 440);
s.vertex(-360, 440);
s.vertex(-360, 420);
s.vertex(-380, 420);
s.vertex(-380, 400);
s.vertex(380, 400);
s.vertex(380, 420);
s.vertex(360, 420);
s.vertex(360, 440);
s.vertex(420, 440);
s.vertex(420, 420);
s.vertex(400, 420);
s.vertex(400, 420);
s.vertex(400, -440);
s.vertex(400, 400);
s.endShape();
System.out.println("it got to here where it breaks");

// using a 2D array to create a grid of circles
// which will represent the spaces on the grid
circleHasYelowPiece = new boolean[7][6];
circleHasRedPiece = new boolean[7][6];
circleSpacesFilled = new boolean[7][6];
circleSpaces = new PShape[7][6];
for (int row = 0; row < 7; row++) {
for (int column = 0; column < 6; column++) {
circleSpaces[row][column] = createShape(ELLIPSE, -380 + (row) * SPACES_BETWEEN_ROWS,
-370 + (column) * SPACES_BETWEEN_COLUMNS, 100, 100);
circleSpaces[row][column].disableStyle();
stroke(0);
strokeWeight(5);
circleSpacesFilled[row][column] = false;
circleHasRedPiece[row][column] = false;
circleHasYelowPiece[row][column] = false;
}
}

}

public void draw() {
translate(width / 2, height / 2);
shape(s);
for (int row = 0; row < 7; row++) {
for (int column = 0; column < 6; column++) {
shape(circleSpaces[row][column]);
}
}
}

public boolean piecePlaced(int column, Color pieceColor) {
column = column - 1; // the choice are form 1-7 but in an array its 0-6;
boolean moveDone = false;
int i = 5;
Color red = new Color(255, 0, 0);
while (i >= 0) {
if (circleSpacesFilled[column][i] == false) {
circleSpacesFilled[column][i] = true;
if (pieceColor.equals(red)) {
circleHasRedPiece[column][i] = true;
circleSpaces[column][i].fill(255, 0, 0);
circleSpaces[column][i].tint(255, 0, 0);
} else {
circleHasYelowPiece[column][i] = true;
circleSpaces[column][i].fill(255, 255, 0);
circleSpaces[column][i].tint(255, 255, 0);
}
return true;
}
}

return false;
}

}

当我运行它时,我得到这个NullPointerException。请注意,异常来自处理的库内 - 它不是由我自己的代码直接引起的!

enter image description here

可疑的 3 行是:

  1. currentGame = new C4Game(player1Is,player2Is,player1Color,player2Color);
  2. theGrid = new C4Grid(); theGrid.setup();
  3. s= createShape(); 靠近 setup() 顶部

currentGametheGrids 均为非空(我已经检查过无数次)。

即使我单独测试每一行,与 PShape 类相关的任何内容都会出现错误。我摆脱了每个 PShape 对象并且它起作用了,但是有没有办法修复它,以便我可以使用 PShape 作为我的代码的一部分?

最佳答案

当我运行您的代码时,我没有收到NullPointerException。我收到一条错误消息:

When not using the PDE, size() can only be used inside settings(). Remove the size() method from setup(), and add the following:

public void settings() {
size(1000, 1000, "processing.opengl.PGraphics2D");
}

错误说明了一切。当您将Processing用作库时,您无法从setup()函数调用size()函数。请从 settings() 函数调用它。

如果我进行更改,您的代码将运行良好:

关于java - 创建 PShape 时,处理库中出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36602275/

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