将对象绘制到 canvas(在 Eclipse 中使用 Processing 库),returns 出现意外错误
Drawing object to canvas (using Processing library, in Eclipse), returns an unexpected error
我在 Eclipse IDE 中使用处理库,我想使用 p.show() 方法将 'Player' 对象 'p' 绘制到屏幕上.所有其他处理函数都有效,但是当 this.show() 函数为 运行 时,它会 returns 出现如下所示的错误。如何解决?!
这个程序:
import processing.core.PApplet;
public class Game extends PApplet {
public static void main(String[] args){
PApplet.main("Game");
}
public Player p = new Player();
public static int cW = 1000, cH = 600;
public void settings(){
size(cW,cH);
}
public void setup(){
background(51);
}
public void draw(){
p.show();
}
}
//PLAYER.JAVA CLASS
import processing.core.PApplet;
public class Player extends PApplet{
public int x,y,pSize = 30;
public Player(){
this.x = Game.cW/2;
this.y = Game.cH/2;
}
public void show(){
noStroke();
rect(this.x,this.y,this.pSize,this.pSize);
}
}
Returns错误:
java.lang.NullPointerException
at processing.core.PApplet.noStroke(PApplet.java:13982)
at Player.show(Player.java:12)
at Game.draw(Game.java:21)
at processing.core.PApplet.handleDraw(PApplet.java:2418)
at processing.awt.PSurfaceAWT.callDraw(PSurfaceAWT.java:1540)
at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:316)
您不应该有两个不同的 class 都扩展 PApplet
。将 PApplet
的每个扩展视为一个单独的草图。所以你真的只想要一个扩展 PApplet
的 class。要在其他 classes 中访问 Processing 的功能,您必须传入对主草图的引用。它看起来像这样:
public class Player{
private int x;
private int y;
private int pSize = 30;
public Player(int x, int y, PApplet sketch){
this.x = x;
this.y = y;
this.sketch = sketch;
}
public void show(){
sketch.noStroke();
sketch.rect(x, y, pSize, pSize);
}
}
然后在主草图 class 中,您将使用 this
关键字将自引用传递给 Player
class:
public class Game extends PApplet {
private Player p;
private int cW = 1000;
private int cH = 600;
public void settings(){
size(cW,cH);
p = new Player(cW/2, cH/2, this);
}
//rest of your code
我在 Eclipse IDE 中使用处理库,我想使用 p.show() 方法将 'Player' 对象 'p' 绘制到屏幕上.所有其他处理函数都有效,但是当 this.show() 函数为 运行 时,它会 returns 出现如下所示的错误。如何解决?!
这个程序:
import processing.core.PApplet;
public class Game extends PApplet {
public static void main(String[] args){
PApplet.main("Game");
}
public Player p = new Player();
public static int cW = 1000, cH = 600;
public void settings(){
size(cW,cH);
}
public void setup(){
background(51);
}
public void draw(){
p.show();
}
}
//PLAYER.JAVA CLASS
import processing.core.PApplet;
public class Player extends PApplet{
public int x,y,pSize = 30;
public Player(){
this.x = Game.cW/2;
this.y = Game.cH/2;
}
public void show(){
noStroke();
rect(this.x,this.y,this.pSize,this.pSize);
}
}
Returns错误:
java.lang.NullPointerException
at processing.core.PApplet.noStroke(PApplet.java:13982)
at Player.show(Player.java:12)
at Game.draw(Game.java:21)
at processing.core.PApplet.handleDraw(PApplet.java:2418)
at processing.awt.PSurfaceAWT.callDraw(PSurfaceAWT.java:1540)
at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:316)
您不应该有两个不同的 class 都扩展 PApplet
。将 PApplet
的每个扩展视为一个单独的草图。所以你真的只想要一个扩展 PApplet
的 class。要在其他 classes 中访问 Processing 的功能,您必须传入对主草图的引用。它看起来像这样:
public class Player{
private int x;
private int y;
private int pSize = 30;
public Player(int x, int y, PApplet sketch){
this.x = x;
this.y = y;
this.sketch = sketch;
}
public void show(){
sketch.noStroke();
sketch.rect(x, y, pSize, pSize);
}
}
然后在主草图 class 中,您将使用 this
关键字将自引用传递给 Player
class:
public class Game extends PApplet {
private Player p;
private int cW = 1000;
private int cH = 600;
public void settings(){
size(cW,cH);
p = new Player(cW/2, cH/2, this);
}
//rest of your code