I made my pixels[][] array to work

I just cant understand bufferedimage pixel array so i made my own array
it is just pixels[][] array put x and y and you are ready to go :smiley: i think i will make i lib
so do you want to know how it works ??

MAIN :


import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;

public class Main extends Canvas implements Runnable {

	private static final long serialVersionUID = 1L;
	public Thread thread;
	// my way to get width and height :D
	public int width = 150, height = (width / 10) * ((10 / 3) * 2), scale = 6;
	public static String title = "Pixels[][]";
	public boolean run = false;
	public int finalFrames = 0;
	public int[][] pixels;
	public Render r;
	public BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

	public Main() {
		setPreferredSize(new Dimension(width * scale, height * scale));
		pixels = new int[width][height];
		for (int x = 0; x < pixels.length; x++) {
			for (int y = 0; y < pixels[0].length; y++) {
				pixels[x][y] = 0xffffff;
			}
		}
		r = new Render(this, pixels, scale);
	}

	public void tick() {
		r.tick();
		pixels = r.getPixels();
		for (int x = 0; x < pixels.length; x++) {
			for (int y = 0; y < pixels[0].length; y++) {
				img.setRGB(x, y, pixels[x][y]);
			}
		}
	}

	public void render(Graphics g) {
		//r.render(g);
		g.drawImage(img, 0, 0, getWidth(), getHeight(), null);
	}

	public void draw() {
		BufferStrategy bs = getBufferStrategy();
		if (bs == null) {
			createBufferStrategy(3);
			return;
		}

		Graphics g = bs.getDrawGraphics();
		g.clearRect(0, 0, getWidth(), getHeight());
		render(g);
		g.setFont(new Font("Arial", Font.BOLD, 12));
		g.setColor(Color.BLACK);
		g.drawString("FPS : " + finalFrames, 0, 10);

		bs.show();
		g.dispose();
	}

	public void run() {
		long lastTime = System.nanoTime();
		final double amountOfTicks = 60.0;
		double ns = 1000000000 / amountOfTicks;
		double delta = 0;
		int ticks = 0;
		int frames = 0;
		long timer = System.currentTimeMillis();

		while (run) {
			long now = System.nanoTime();
			delta += (now - lastTime) / ns;
			lastTime = now;
			if (delta >= 1) {
				tick();
				ticks++;
				delta--;
			}
			draw();
			frames++;
			if (System.currentTimeMillis() - timer > 1000) {
				timer += 1000;
				finalFrames = frames;
				frames = 0;
				ticks = 0;
			}
		}
	}

	public synchronized void start() {
		run = true;
		thread = new Thread(this);
		thread.start();
	}

	public synchronized void stop() {
		run = false;
		try {
			thread.join();
		} catch (InterruptedException e) {
			System.out.println("ERROR can not stop the thread!");
		}
		System.exit(0);
	}

	public static void main(String[] args) {
		JFrame jf = new JFrame();
		Main m = new Main();
		jf.add(m);
		jf.pack();
		jf.requestFocus();
		jf.setTitle(title);
		jf.setVisible(true);
		jf.setResizable(false);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf.setLocationRelativeTo(null);
		m.start();
	}
}

RENDER:

import java.awt.Color;
import java.awt.Graphics;

public class Render {

	private int[][] pixels;
	private Main m;
	private int scale;
	
	public Render(Main main, int[][] pixels, int pixScale) {
		m = main;
		this.pixels = pixels;
		scale = pixScale;
	}

	public void tick() {
		pixels[10][10] = 0x000000;
	}
	
	public int[][] getPixels(){
		return pixels;
	}
	
	public void render(Graphics g) {
		for (int x = 0; x < pixels.length; x++) {
			for (int y = 0; y < pixels[0].length; y++) {
				g.setColor(new Color(pixels[x][y]));
				g.fillRect(x * scale, y * scale, scale, scale);
			}
		}
	}
}
}


This is version 1.2 and will be adding more features later :smiley: :smiley:
please support me

Moved to shared code :slight_smile:
Go ahead and paste in your code into the first post.

How horribly inefficient :smiley:

Why is it inefficient? From what I see it should work fine. Granted its not really worth putting it on the forum as mostly everyone knows how to do this, and it is Java2D so it would be rather slow, but other than that I don’t see a problem!

Using setRGB() instead of directly setting the value is comparatively inefficient since it jumps through a few hoops.

Not understanding how a 1d array can represent a 2d array shouldn’t really be a legitimate problem since they’re just different spectacles used to look at exactly the same data.

Yea it is not Java2D lol i dont use libs and for beginners it is simple to use
it is harder for them to understand if it is 1d array
ok lets try this
put a image on x 29 and y 4 the size of 32 x 32

2d array



int x1 = 29, y1 = 4;
for (int x = 0; x < 32; x++) {
	for (int y = 0; y < 32; y++) {
		pixels[x1 + x][y1 + y] = /* IMAGE PIXELS */pixelsImg[x][y];
	}
}


1d array

IDK because i dont understand how it works and 2d array i do so let me be

and if everyone knows how to do this why dont they ???

and by the way i get from 1600 to 1750 fps !!! and it never changes what ever i do it stays the same !!!

Not understanding something isn’t a good reason to give up.

Would you drive a car with your feet on the steering wheel if you could do it?

Here’s 1d form:

int x1 = 29, y1 = 4;
for (int x = 0; x < 32; x++) {
   for (int y = 0; y < 32; y++) {
      pixels[(x1 + x) + (y1 + y) * screen.width] = /* IMAGE PIXELS */pixelsImg[x + y * img.width];
   }
}

Ah, sorry I didn’t even see he was using a 2D array. Nevermind, I do see problems. Never look at code on a smartphone, you always miss something!