Difference between these 2 ways of rendering

So, I saw two ways to draw render.

CODE 1

private BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();

Then in the render method,
for (int i = 0; i < pixels.length; i++) {
pixels[i] = 0;
}

g.drawImage(image, 0, 0, getWidth(), getHeight(), null);

CODE 2

private BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

In the render method,

g.drawImage(image, 0, 0, WIDTH, HEIGHT, null);

So what I basically want to understand is, what is the difference between the two and which one is better.
I know this is a very confusing question, but someone please help.

Some tips: use code tags to make clearer where your code examples are.

Both ways are identical, the first one just has some nonsensical loop that does nothing at all and probably doesnt even compile:

for (int i = 0; i < pixels.length; i++) {
     pixels = 0;
}

If pixels is an array and you try to do

pixels = 0

, I highly doubt you get anything interesting other than a compile error.

That is not all the code, there is to it. There is a lot more code.

Rendering the player with the pixel array way.


	public void renderPlayer(int xp, int yp, Sprite sprite) {
		xp -= xOffset;
		yp -= yOffset;
		for (int y = 0; y < 16; y++) {
			int ya = y + yp;
			for (int x = 0; x < 16; x++) {
				int xa = x + xp;
				if (xa < -16 || xa >= width || ya < 0 || ya >= height) break;
				if (xa < 0) xa = 0;
				int col = sprite.pixels[x + y * 16];
				pixels[xa + ya * width] = col;
			}
		}
	}

Rendering the player the other way.


g.drawImage(player_image, x, y, player_size, player_size, null);

Take what theCherno says with a grain of salt. He doesn’t know much, he’s just a copycat.

Anyway. They both have their advantages and disadvantages. The “better” way allows you to edit the pixels individually.

So you recognized this as Cherno’s code, huh ?
:smiley:
Cherno copied this from Notch.

So how do you render ?

I think I am gonna go back to basics now.

Both of these ways are basic… It doesn’t really matter which one you use. They are both really slow and not worth using for a bigger game.

Say which way should I use, I am making a Top Down Survival game with random dungeons to explore. How should I go about doing this ?

First off, if you want to talk about Java2D I would recommend talking to my friend Kpars on here. He’s good at utilizing Java2D.

I would just use the first way because its easier and you most likely won’t need to modify the pixels while the game is running.

You should really stick to the standardized method: drawImage(), especially if you don’t have enough experience with low-level graphics manipulation. Accessing pixel arrays directly in Java2D is really a last resort if you just have to be able to read and write every pixel individually. Here that is not the case, you are just rendering an image onto another, so use the API provided to do that.

It’s also much more readable and concise: 1 line vs. 14+ lines, and will execute faster as Java2D can perform the operation in VRAM, as opposed to that loop, which runs on the CPU and behind the scenes continuously has to tell the graphics card to fetch the modified data.