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 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
please support me