在使用处理库的 Java 应用程序中使用 updatePixels() 时遇到问题
我正在尝试用 Java 编写一个简单的游戏,它使用处理来渲染图形。但是,我在使用 updatePixels() 渲染任何更改时遇到问题。我可以成功设置背景颜色并绘制基本的 2d 形状,但我从编辑 Pixels[] 变量或使用 set(x, y, color) 中得不到任何结果。
这是我的(删节的)代码:
import processing.core.*;
public class GameController extends PApplet {
private int width, height;
private final static String RENDER_MODE = PConstants.P2D; //JAVA2D;
public GameController(int width, int height) {
this.width = width;
this.height = height - this.getBounds().y;
}
@Override
public void setup() {
this.size(this.width, this.height, RENDER_MODE);
this.background(0);
}
@Override
public void draw() {
this.ellipse(50, 50, 100, 10);
this.loadPixels();
for (int p : this.pixels) {
p = this.color(255, 0, 0);
}
this.updatePixels();
}
}
当我 init() 这个类时,我在黑色屏幕上看到一个白色椭圆,而不是红色像素的屏幕(这是我所期望的)。 Pixels[] 数组肯定存在,因为我已经将其打印出来,并且没有收到任何错误。 我做错了什么?
I'm trying to write a simple game in Java that uses Processing to render graphics. However, I'm having trouble rendering any changes using updatePixels(). I can successfully set the background color and draw basic 2d shapes, but I get nothing from editing the pixels[] variable, or from using set(x, y, color).
This is my (abridged) code:
import processing.core.*;
public class GameController extends PApplet {
private int width, height;
private final static String RENDER_MODE = PConstants.P2D; //JAVA2D;
public GameController(int width, int height) {
this.width = width;
this.height = height - this.getBounds().y;
}
@Override
public void setup() {
this.size(this.width, this.height, RENDER_MODE);
this.background(0);
}
@Override
public void draw() {
this.ellipse(50, 50, 100, 10);
this.loadPixels();
for (int p : this.pixels) {
p = this.color(255, 0, 0);
}
this.updatePixels();
}
}
When I init() this class, I get a white ellipse on a black screen, not a screen of red pixels (which is what I'm expecting).
The pixels[] array is definitely there, as I've printed it out, and I'm getting no errors.
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这一行:
仅更改局部变量
p
,其中包含像素值的副本。您想要的是修改
pixels
数组内的值,如下所示:This line:
only changes the local variable
p
, which contained a copy if the pixel value.What you want is to modify the values inside the
pixels
array, as in: