gpt4 book ai didi

java - 3D Java 编程教程不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 14:27:48 24 4
gpt4 key购买 nike

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);
}

}

提前致谢!

最佳答案

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

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

关于java - 3D Java 编程教程不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26487777/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com