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?
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
pixelsarray, as in: