为什么窗口没有出现?

时间:2016-09-14 06:06:35

标签: java canvas jframe pixels

我看到了另一个问题,想要帮助一个人堆栈,他正在制作3D游戏,我感觉像帮助他所以他有这个代码,使窗口和随机像素出现我复制他的coe试图解决他早先的疑问,但现在有我自己的怀疑为什么我的窗口显示为空白?一切都很好,调试没有问题,伙计们请帮助我

我的(他的)代码如下:

显示CLass

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 = 1L;

public static final int WIDTH = 800;
public static final int HEIGHT = 600;
public static final String TITLE = "Nexus Overload";

private Thread t;
private boolean running = false;
private int[] pixels;

private BufferedImage img;

private Render ren;
private Screen s;

public Display() {
    s = new Screen(WIDTH, HEIGHT);
    img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);

    pixels = ((DataBufferInt) img.getRaster().getDataBuffer()).getData();
}

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

public void tick() {

}

public void render() {
    BufferStrategy bs = this.getBufferStrategy();

    if(bs == null) {
        createBufferStrategy(3);
        return;
    }
    s.render();

    for (int i = 0; i < WIDTH * HEIGHT; i++) {
        pixels[i] = s.pixels[i];
    }

    Graphics g = bs.getDrawGraphics();
    g.drawImage(img, WIDTH, HEIGHT, null);
    g.dispose();
    bs.show();
}

private void start() {
    if (running)
        return;
    running = true;
    t = new Thread(this);
    t.start();
}

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

public static void main(String[] args) {
    Display dis = new Display();
    JFrame frame = new JFrame();

    frame.add(dis);
    frame.pack();
    frame.setTitle(TITLE);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(WIDTH, HEIGHT);
    frame.setLocationRelativeTo(null);
    frame.setResizable(false);
    frame.setVisible(true);

    dis.run();
    dis.start();
}
}

渲染类:

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 ren, int xOff, int yOff) {
    for (int y = 0; y < ren.height; y++) {
        int yPixel = y + yOff;

        for (int x = 0; x < ren.width; x++) {
            int xPixel = x + xOff;

            pixels[xPixel + yPixel * width] = ren.pixels[x + y * ren.width];
        }
    }
}
}

最后屏幕CLASS:

import java.util.Random;

public class Screen extends Render {

private Render ren;

public Screen(int width, int height) {
    super(width, height);

    Random r = new Random();
    ren = new Render(256, 256);

    for (int i = 0; i < 256 * 256; i++) {
        ren.pixels[i] = r.nextInt();
    }
}

public void render() {
    draw(ren, 0, 0);
}

}

0 个答案:

没有答案