I am following this YouTube tutorial by TheCherno to make 3D graphics using Java Standard Library.
Display.java
package aritro;
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import javax.swing.JFrame;
public class Display extends Canvas implements Runnable {
private static final long serialVersionUID = -1545751462933395031L;
static int HEIGHT = 500, WIDTH = 800;
static String title = "Minefront Pre-Alpha 0.0.1";
private Thread thread;
private boolean running = false;
//private Render render;
private Screen screen;
private BufferedImage img;
private int pixels[];
public Display() {
screen = new Screen(WIDTH, HEIGHT);
img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
pixels = ((DataBufferInt) img.getRaster().getDataBuffer()).getData();
}
private void start() {
if (running)
return;
running = true;
this.thread = new Thread(this);
System.out.println("Working...");
}
@Override
public void run() {
while (running) {
tick();
render();
}
}
private void render() {
BufferStrategy bs = getBufferStrategy();
if (bs == null) {
createBufferStrategy(3);
return;
}
screen.render();
for (int i = 0; i < WIDTH * HEIGHT; i++) {
pixels[i] = screen.pixels[i];
}
Graphics g = bs.getDrawGraphics();
g.drawImage(img, 0, 0, WIDTH, HEIGHT, null);
g.dispose();
bs.show();
}
private void tick() {
}
public void stop() {
if (!running)
return;
running = false;
try {
thread.join();
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
public static void main(String[] args) {
Display game = new Display();
JFrame frame = new JFrame(title);
frame.add(game);
frame.setResizable(false);
frame.pack();
frame.setSize(WIDTH, HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
game.start();
}
}
This class is the main class which renders to the JFrame. The two helper classes Render.java
and Screen.java
.
Render.java
package aritro.graphics;
public class Render {
public int height, width;
public int pixels[];
public Render(int _width, int _height) {
width = _width;
height = _height;
pixels = new int[width * height];
}
public void draw(Render render, int xOffset, int yOffset) {
for (int y = 0; y < render.height; y++) {
int yPix = y + yOffset;
for (int x = 0; x < render.width; x++) {
int xPix = x + xOffset;
pixels[xPix + yPix * width] = render.pixels[x + y * width];
}
}
}
}
Screen.java
package aritro.graphics;
import java.util.Random;
public class Screen extends Render {
private Render test;
public Screen(int _width, int _height) {
super(_width, _height);
Random random = new Random();
test = new Render(256, 256);
for (int i = 0; i < 256 * 256; i++) {
test.pixels[i] = random.nextInt();
}
}
public void render() {
draw(test, 0, 0);
}
}
But unlike the the end of the video, I just get the blank grey screen instead of a black one with a pitch of randomly colored pixels.
Can someone point the mistake in the code which is causing the problems?
EDIT : Someone pointed out that I had initialized running to false in void start which did not initiate the game loop. but even after setting it to true, it doesn't work.
this is the blank screen. Java version used - Java14.0.0.1