0
\$\begingroup\$

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.

enter image description here

this is the blank screen. Java version used - Java14.0.0.1

\$\endgroup\$
1
  • \$\begingroup\$ Do not edit your solution into the question. Instead, add it as an answer and accept it. \$\endgroup\$
    – user35344
    Commented Jul 16, 2021 at 14:42

2 Answers 2

2
\$\begingroup\$

In start you do running = false; which means that when run gets called, the gameloop will never activate and hence your actual rendering code will never run. In the linked video the code in start sets running to true.

\$\endgroup\$
3
  • \$\begingroup\$ ok. will correct \$\endgroup\$ Commented Jul 15, 2021 at 13:01
  • \$\begingroup\$ @AritroShome if this solved your problem, you should consider marking it as the accepted answer. \$\endgroup\$
    – Pikalek
    Commented Jul 15, 2021 at 16:28
  • \$\begingroup\$ @Tyyppi_77 didn't solve \$\endgroup\$ Commented Jul 16, 2021 at 14:16
0
\$\begingroup\$

Add the line thread.start() to void start() else the thread doesn't start

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .