3D Java编程教程不起作用

时间:2014-10-21 13:21:05

标签: java user-interface render pixel pixels

http://www.youtube.com/watch?v=iH1xpfOBN6M我已经按照本教程进行了第四集,并且他的窗口中有像素,我的完全是空白。我想知道在eclipse中有没有3D编程经验的人是否可以看到有什么东西看起来不适合你。

显示:

package com.mine.minefrost;

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

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

public static final int WIDTH = 800;
public static final int HEIGHT = 600;
public static final String TITLE = "Minefrost Pre-Alpha 0.01";

private Thread thread;
private Screen screen;
private BufferedImage img;
private Render render;
private boolean running = false;
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();
    thread.start(); 
    }

private void stop() {
    if (!running) return;
    running = false;
    try {
        thread.join();

    } catch (Exception e) {
        e.printStackTrace();
        System.exit(0);
    }
}

private 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.pack();
    frame.setTitle(TITLE);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setSize(WIDTH, HEIGHT);
    frame.setResizable(false);
    frame.setVisible(true);

    game.start();
}

}

渲染:

package com.minefrost.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*render.width];
        }

    }
}

}

屏幕:

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

}

提前致谢!

1 个答案:

答案 0 :(得分:0)

您在Display类中的主要方法需要调用game.run()。有了它,你会在左上角显示一些随机像素“雪”。我不确定这是不是你想要的,但这就是发生的事情!

另一个小问题是Render中对Display类的引用未使用。此外,stop()run()是私有的,这很奇怪。