Java 3D引擎像素绘图问题

时间:2018-02-14 21:54:08

标签: java

我的3个程序似乎单独工作但是当我尝试一起运行时它似乎给了我一个出界的错误但是我不知道究竟是否有人可以告诉我它究竟在哪里以及如何修复错误会很棒,我想使用一个调试器,但我有一些问题让它工作。

这是我的3个类的代码:     Render.java     包com.mime.Game.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 x = 0; x < this.height; x++) {
        int yPix = x + yOffset;
        for(int y = 0; y < this.width; y++) {
            int xPix = y + xOffset;

            pixels[xPix + yPix * width] = render.pixels[x + y * 
render.width];
        }
    }
}
}

Display.java     包com.mime.Game;

import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;

import com.mime.Game.graphics.Render;
import com.mime.Game.graphics.Screen;
import java.awt.image.DataBufferInt;

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

public static final int WIDTH = 800;
public static final int HEIGHT = 600;

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

    System.out.println("Working");
}
public 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.setResizable(false);
    frame.setSize(WIDTH, HEIGHT);
    frame.add(game);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setTitle("Chronicles of Walshy Alpha 0.01");

    game.start();
}

}

screen.java:     包com.mime.Game.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)

Render.draw方法中,您可以复制其他Render对象中的像素,并使用this.widththis.height来访问这些像素。当其他Render对象的像素大小较小时,这将失败,这在您的代码中就是这种情况。从ArrayOutOfBoundsException阅读时,您会收到render.pixels[x + y * render.width],因为您正试图从256*256数组中读取像素,就好像它是800*600数组一样。

此外,如果您使用偏移值来呼叫draw,则会获得另一个ArrayOutOfBoundsException,因为您只需将偏移添加到xy在写入pixels[xPix + yPix * width]时,不检查结果索引是否太大的值。

要解决此问题,您必须跳过任何超出pixels数组宽度或高度的值:

if (xPix > this.width || yPix > this.height || x > render.width || y > render.height)
{
  continue;
}