0

我一直在观看有关如何使用 Eclipse 在 Java 中制作 3D 游戏的教程。我已经逐字复制了所有代码,但没有得到相同的结果,这非常令人沮丧。目前我要做的就是创建一个随机生成的像素小方块,而我得到的只是一个空白窗口。这是我正在使用的代码和类。

Class1 = 显示

package com.mime.testgame2;

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;
import com.mime.testgame2.graphics.Render;
import com.mime.testgame2.graphics.Screen;

public class Display extends Canvas implements Runnable {
    private static final long serialVersionUID = 1L;

    public static final int width = 800;
    public static final int height = 600;
    public static String title = "3D Game 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;
        thread = new Thread(this);
        thread.start();
    }

    private void stop() {
        if(!running) return;
        running = false;
        try {
            thread.join();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(0);
        }
    }

    public void run() {
        while (running) {
            tick();
        render();


        }

    }

    private void tick() {

    }

    private void render() {
        BufferStrategy bs = this.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();

    }

    public static void main(String[] args) {
        Display game = new Display();
        JFrame frame = new JFrame();
        frame.add(game);
        frame.setResizable(false);
        frame.setVisible(true);
        frame.setSize(width, height);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setTitle(title);

    }

}

Class2 = 渲染

package com.mime.testgame2.graphics;

public class Render {
public final int Width;
    public final int Height;
    public final int[] pixels;

    public Render(int Width, int Height) {
        this.Width = Width;
        this.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 * rend            er.Width];
                }
            }
    }

}

Class3 = 屏幕

package com.mime.testgame2.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);
    }

}    

谁能帮我?

4

0 回答 0