[SOLVED]WHY DOES THIS WORK o.0

So I’ve been making 2D java games with the same setup for a long time but i don’t understand how it works

Here’s the code

package com.dazkins.alone;

import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.util.Random;

import javax.swing.JFrame;

public class AloneComponent extends Canvas implements Runnable{

	private static final int WIDTH = 256;
	private static final int HEIGHT = 256;
	private static final int SCALE = 3;
	
	private boolean running = false;
	
	private BufferedImage img = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_RGB);
	private int[] pixels = ((DataBufferInt)img.getRaster().getDataBuffer()).getData();
	
	public static void main(String args[]){
		AloneComponent c = new AloneComponent();
		JFrame frame = new JFrame("Alone");
		
		frame.add(c);
		frame.setVisible(true);
		frame.setResizable(false);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
		
		c.start();
	}
	
	public AloneComponent(){
		Dimension d = new Dimension(WIDTH*SCALE,HEIGHT*SCALE);
		
		setPreferredSize(d);
		setMaximumSize(d);
		setMinimumSize(d);
	}
	
	public void start(){
		running = true;
		Thread t = new Thread(this);
		t.start();
	}
	
	public void stop(){
		running = false;
	}
	
	public void tick(){
		
	}
	
	public void render(){
		BufferStrategy bs = getBufferStrategy();
		
		if(bs==null){
			createBufferStrategy(3);
			return;
		}
		
		Graphics g = bs.getDrawGraphics();
		
		for(int i=0;i<pixels.length;i++){
			pixels[i] = new Random().nextInt(0x0000FF) << 16;
		}
		
		g.drawImage(img, 0, 0, getWidth(), getHeight(), null);
		
		g.dispose();
		bs.show();
	}
	
	public void run() {
		while(running){
			tick();
			render();
		}
	}
	
}

My question is about that the array of pixels that is used to store colour data for each pixel. I understand how it represents colours, but i don’t understand atall at what part in the code that pixel array is asigned to the BufferedImage or rendered to the screen

The pixel array is acquired from the image in line 22, modified in lines 69-71 and rendered to the screen in line 73.
The pixel array stores the image data, so changing the pixels automatically changes the image.


public void render(){
      BufferStrategy bs = getBufferStrategy();
      
      if(bs==null){
         createBufferStrategy(3);
         return;
      }
      
      Graphics g = bs.getDrawGraphics();
      
      for(int i=0;i<pixels.length;i++){
         pixels[i] = new Random().nextInt(0x0000FF) << 16;
      }
      
      g.drawImage(img, 0, 0, getWidth(), getHeight(), null);
      
      g.dispose();
      bs.show();
   }

Is this not what your looking for? I’m sorry if I misunderstand the question.

Where did you learn that setup? If you got it from some tutorial, I propose you read it again. If it doesen`t say there, then look for some simmilar tutorials, which might explain what you want to know. :slight_smile:

Let me rephrase it slightly, I want to know at what point in the program does the program know to render that pixel array to the screen, because if i rename it, it still works.

In the render function the pixel array is never called apart from to edit it, so how does the program know to use it?

Take a look at this: http://lmgtfy.com/?q=java2d+tutorials

If you can find me a page that tells me let me know because ive tried that many times :S

Do you see that line [icode]private int[] pixels = ((DataBufferInt)img.getRaster().getDataBuffer()).getData();[/icode]

You are telling the program to use [icode] int[] pixels [/icode] to store the image pixels. Everything in Java acts like a pointer reference. So the pixel data for your array is referenced to the same pixel data in your image, therefore all changes you make will be instantly translated. It does not matter where you edit that array, or if you change the name to that array, the pointer will stay the same until you write something like [icode] int[] pixels = new int[4] [/icode].

and the line where it draws the image…

[icode] g.drawImage(img, 0, 0, getWidth(), getHeight(), null); [/icode]

Does that clear anything up?

YES

thank you so much dude that was exactly what i was looking for :slight_smile: